DataGrid Data

J

Jim Heavey

Hello, I am starting to learn how to use the Datagrid and I have a couple of
questions.

My datagrid as a checkbox in it. It looks like the following in the
datagrid...
<asp:TemplateColumn HeaderText="Check In">
<ItemStyle HorizontalAlign="Center" Width="30px"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="chkReturn" runat="server"
OnCheckedChanged="CheckItIn"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

I had expected that when I click the checkbox, that the "OnCheckedChanged"
event specified would fire. It does not. I added the "AutoPostBack="True"
and I can see that it is making a trip back to the server, but the event
specified is not fired. How do I get the event to fire?

I wrote a little routine to interogate the values in the datagrid when a
button is clicked. The problem is that all the values for the checkbox are
always false. I must be doing something wrong in looking at these values, but
I am not sure what it is. Here a portion of the routine:

Dim item As DataGridItem
Dim itemCnt As Integer = dgCheckIn.Items.Count
For Each item In dgCheckIn.Items
Dim cnt3 As Integer = 0
Dim chkBox As CheckBox
For cnt3 = 0 To item.Cells(3).Controls.Count - 1
If TypeOf item.Cells(3).Controls(cnt3) Is CheckBox Then
Console.WriteLine("Found a Checkbox Control at instance of {0}",
cnt3)
chkBox = CType(item.Cells(3).Controls(cnt3), CheckBox)
End If
Next
Dim bol As Boolean = chkBox.Checked
Next

Any Ideas what I am doing wrong?

Thanks in advance for your assistance!!!!!!!!!
 
A

Alex Davis

Jim,

Here is what I created and the post back works fine...
<asp:DataGrid id="dgCheckBoxTest" runat="Server">
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="chkBox" runat="Server"
OnCheckedChanged="CheckChanged" AutoPostBack="True"/>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>

Notice the "runat=server" in the tags.... here is my code behind the
scenes...

public void CheckChanged(object sender, System.EventArgs e)
{
Label1.Text = "The dgCheckBox just changed";
}
Now granted this is C# vs VB but the concepts are still the same.
 
J

Jim Heavey

I determined that my real problem was that in the Page Load event, I was doing
a databind on the grid the first thing and once this is done, then events
fire, but since the databind of the datagrid reset all of the chekcbox values,
this in effect cancelled the events which were associated with checkbox.
 

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

Top