Parsing value of a checkbox!

JHatman

Banned
Joined
Oct 28, 2008
Messages
2,008
Ok its pretty simple what I'm trying to achieve, yet its just not working hehe!

I've got a number of checkboxes. The value behind each checkbox corresponds to a particular colour ID in the database. When the user clicks the checkbox, I want to fire off a procedure and parse the value of the clicked checkbox to that procedure.

Here's the checkbox code:

<itemtemplate>
<tr>
<td width="25%" class="TableRowsAlt"><%#Container.dataitem("Colour")%></a></td>
<td width="25%" class="TableRowsAlt"><asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="UpdateCheckboxStatus('<%# DataBinder.Eval(Container.DataItem, "ID") %>')" value='<%# DataBinder.Eval(Container.DataItem, "ID") %>'/></td>
</itemtemplate>

And for test purposes I've created an UpdateCheckboxStatus procedure which will pop up a message box of the colour selected:

Code:
    Sub UpdateCheckboxStatus(ByVal iColour As Integer)
        
        MsgBox(iColour)
        
    End Sub

I'm pretty new to asp.net and haven't got a foggiest clue how to send the value of the checkbox ticked! Anyone make any suggestions please :)
 
Last edited:

JHatman

Banned
Joined
Oct 28, 2008
Messages
2,008
Found this sample code on the web, its basically what I'm trying to achieve but in asp.net, not c#, just got no clue how to convert it hehe

Code:
<asp:CheckBox ID="chbActive" OnCheckedChanged="chbActive_OnCheckedChanged" AutoPostBack="true" runat="server" Checked="<%# Container.DataItem.Active %>" />

Code:
Below is the code for chbActive_OnCheckedChanged which is the main part you might be looking for.

protected void cbbActive_OnCheckedChanged(object sender, EventArgs e) 
{

CheckBox chbActive = (CheckBox)sender; 
HtmlGenericControl li = (HtmlGenericControl)chbActive.NamingContainer.FindControl("CustID");

int CustID = Convert.ToInt32(li.InnerText.ToString()); 
bool active = chbActive.Checked;

}
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Ummm... ASP.NET is the framework for building web applications in, whilst C# or VB.NET is the server-side language in which the assemblies would be compiled. Just follow the C# -> VB.NET converter specified in the first post of the .NET Knowledge Sharing thread (in my sig, also stickied in this forum) and you should be OK.
 

JHatman

Banned
Joined
Oct 28, 2008
Messages
2,008
Ok scrap everything I said before. This is what I'm trying to accomplish but I think the syntax is wrong because I'm getting the error: "The server tag is not well formed"\

Code:
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="checkClicked('<%# DataBinder.Eval(Container.DataItem, "ID") %>')" value='<%# DataBinder.Eval(Container.DataItem, "ID") %>'/>



Code:
    Sub checkClicked(ByVal iColour As Integer)
        
        MsgBox(iColour)
    End Sub
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
You usually have to specify an event handler WITHOUT any parameters. Server-side event handlers accept 2 parameters, an object that initiated the call to the event handler and any arguments that were specified for the call. Your event handler in the server-side code should look something like:
Code:
protected void CheckClicked(object sender, EventArgs args)
{
    CheckBox chk = (CheckBox)sender;
    int color = chk.Value;
}

You should then associate the event handler to your checkbox's OnCheckChanged event WITHOUT any parameters, i.e.:
Code:
<asp:CheckBox ID="chkBox" runat="server" AutoPostBack="true" OnCheckedChanged="CheckClicked" value='<%# DataBinder.Eval(Container.DataItem, "ID") %>'/>

EDIT: Remember to set the AutoPostBack property of the CheckBox to "true" so that the data gets posted back to the server when you check the box.
 
Last edited:

JHatman

Banned
Joined
Oct 28, 2008
Messages
2,008
Well after battling with this for over a week and hours and hours of googling, I'm beginning to realise that there is actually no way to do this in vb.net. Nearly every example has been in C#

This is about the closest I could get based on your example.
Code:
    Protected Sub chkItem_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim icounter As Integer
        icounter = 0
        For icounter = 0 To MyRepeater.Items.Count - 1
            Response.Write(MyRepeater.Controls.Count.ToString)
            icounter = icounter + 1
        Next
    End Sub

What I find extremely daft is there is no interface to access the "Value" that I assigned to the checkbox as set here:

Code:
<asp:CheckBox ID="chkItem" AutoPostBack="True" OnCheckedChanged="chkItem_CheckedChanged" runat="server" value='<%#DataBinder.Eval(Container.DataItem, "ID")%>' />

To dynamically load a bunch of checkboxes and simply save into a database the users selection of those checkboxes seems like such a simple thing yet microsoft seemed to have failed I'm afraid...
 
Last edited:

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Well after battling with this for over a week and hours and hours of googling, I'm beginning to realise that there is actually no way to do this in vb.net. Nearly every example has been in C#
I think its just because C# is more popular than VB.NET these days... You should be able to do anything in VB.NET that you can do in C# and vice versa.

I altered your code a bit to reflect the code below. Note that I haven't done VB.NET in several years, so please excuse any syntax errors.
Code:
Protected Sub chkItem_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim checkbox As CheckBox
        checkbox = CType(CheckBox,sender)
        Dim value As String
        value = checkbox.Value
        Response.Write("Selected value: " & value)
End Sub
That is basically what you should aim at.

What I find extremely daft is there is no interface to access the "Value" that I assigned to the checkbox as set here:
Again, use the code I specified above. The event handler has an object called "sender" that is received as parameter. This object refers to the object (checkbox, button, whatever) that initiated the call to the event. In your case, it would be the checkbox. So the object that you are receiving as "sender" is the checkbox. In order to access the value of the checkbox, you need to cast the generic Object type to a type CheckBox, using CType. That way you can just say "checkbox.Value" to access the value, as data-bound to the checkbox.

To dynamically load a bunch of checkboxes and simply save into a database the users selection of those checkboxes seems like such a simple thing yet microsoft seemed to have failed I'm afraid...
You are so very wrong there! The .NET Framework is incredibly powerful and every single site I build contains dynamically created controls at some point. You just need to study up on the life-cycle of a page in ASP.NET to learn how events are handled and when the event handler will be called.

Good luck! ;)
 
Top