Event Hander for OnCheckChanged Event for a CheckBox Template Field

D

David Johnston

First, let me say I am relatively new to ASP.NET ... most .NET
experience is with C# (Winforms).

Now, I have a web page with a gridview that contains a checkbox
control in a template field (see definition below):

<asp:TemplateField
ConvertEmptyStringToNull="False"
HeaderStyle-CssClass="header"
HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectAgent"
runat="server"
OnCheckedChanged="chkSelectAgent_CheckChanged" />
</ItemTemplate>
</asp:TemplateField>

On the same page, I have a list box control. When the check box on a
given row is clicked, I want to populate a row in the list box. My
event handler is listed below:

Public Sub chkSelectAgent_CheckChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles chkSelectAgent.CheckedChanged
Dim sAgentId As String
Dim sFirst As String
Dim sLast As String
Dim chkSelectedAgent As CheckBox
Dim gvrSelectedAgent As GridViewRow

If TypeOf sender Is CheckBox Then
chkSelectedAgent = DirectCast(sender, CheckBox)
If chkSelectedAgent.Checked Then
gvrSelectedAgent =
DirectCast(chkSelectedAgent.Parent.Parent, GridViewRow)
sAgentId = gvrSelectedAgent.Cells(1).Text
sFirst = gvrSelectedAgent.Cells(2).Text
sLast = gvrSelectedAgent.Cells(3).Text
lbSelectedAgents.Items.Add(sFirst + " " + sLast + " (Agent
ID: " + sAgentId + ")")
End If
End If
End Sub

When I set a breakpoint in the event handler, it does hit the
breakpoint, but only when I go to another page, not when I click in a
check box. Do I need to set AutoPostBack="true" for the checkbox
column? When I tried that, my page no longer renders. I'm guessing
that is because I haven't handled the postback in the Page_Load
subroutine.

So, I have two questions:

1) Why isn't the event hander being fired when I click the checkbox?
2) If AutoPostBack is needed, why doesn't anything get rendered on the
postback (not even labels and text boxes)?

I have googled this like crazy and haven't found a definitive answer.
If you have any good informational links, I not only want to solve the
problem, but also to learn in the process. Thanks in advance for any
help you can provide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top