Display CheckBox value in Label control using C# and VB.Net in ASP.Net

Last Reply 20 days ago By dharmendr

Posted 20 days ago

Hello,

Please how can I make a value of checkbox to be displayed in label control when user checks the box? 

I want to do it in serverside i.e C#. If possible under SelectedIndexChanged

You are viewing reply posted by: dharmendr 20 days ago.
Posted 20 days ago

Hi RichardSa,

Check this example. Now please take its reference and correct your code.

HTML

<asp:CheckBox ID="chkFruit" Text="Mango" runat="server"
    OnCheckedChanged="OnSelectedIndexChanged" AutoPostBack="true" />
<br />
<asp:Label ID="lblFruit" runat="server" />

Code

C#

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    lblFruit.Text = chkFruit.Checked ? chkFruit.Text : "";
}

VB.Net

Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    lblFruit.Text = If(chkFruit.Checked, chkFruit.Text, "")
End Sub

Screenshot