Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there, I have a project in VB.NET which is working fine which essentially has a Datagrid that has a TemplateColumn included which is a column of Checkboxes. The code to declare the datagrid is here...

VB
<asp:datagrid id="dgDates" OnItemCommand="gridEventHandler" BorderColor="Black" BorderWidth="1px"
    CellPadding="3" runat="server" AutoGenerateColumns="False" HorizontalAlign="Left" AllowSorting="True"
    OnSortCommand="SortData" OnItemDataBound="gridItemDataBound">
    <HeaderStyle Font-Underline="True" Font-Bold="True" HorizontalAlign="Center" ForeColor="Black"
        BackColor="#D4D0C8"></HeaderStyle>
    <Columns>
        <asp:BoundColumn DataField="strParameterName" SortExpression="strParameterName" HeaderText="Parameter Name"></asp:BoundColumn>
        <asp:BoundColumn DataField="dtParameterValue" SortExpression="dtParameterValue" HeaderText="Parameter Value"></asp:BoundColumn>
        <asp:TemplateColumn HeaderText="Constant" SortExpression="blnStatic" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="cbStaticRolling" Checked="False" Runat="server" AutoPostBack="true"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:datagrid>



as you can see the Checkbox has Autopostback="true" but there are other things on the page which produce postbacks as well.

My Page_load has this in it, being called on every load of the page, postbacks included...


VB
Dim strGUID As String
strGUID = Session("strGUID")
dgDates.DataSource = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings(Web.[Global].CfgKeyConnStringADMIN), "dbo.spRptGetSchedulingDates", strGUID)
dgDates.DataBind()
intNumberOfDates = dgDates.Items().Count


as well my code behind has the following code for the gridItemDataBound

VB
Protected Sub gridItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
    'hide the intRptSchedulingDatesID for each row in the checkbox's content style variable
    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
        CType(e.Item.FindControl("cbStaticRolling"), CheckBox).Style("Content") = CType(e.Item.DataItem, System.Data.DataRowView).Item("intRptSchedulingDatesID")
    End If
End Sub



everything you see sbove is working perfectly fine...in the sense that when I click one of the checkboxes, the page_load fires, the gridItemDataBound fires on DataBind() and when all is done, the checkbox retains the value that the user clicked the checkbox.

With all this exact same code converted to C#....the events all fire in the same order, but the checkbox selected value always clears...any thoughts??
Posted
Updated 23-Feb-11 10:25am
v2
Comments
Magnus Gudmundsson 23-Feb-11 17:07pm    
Maybe it's just that it's way past my bedtime, but where is the code that saves the checkbox.checked value? Because you apparently bind the grid everytime you load the page, that should be where the problem is. Have you verified that the checked value gets saved to the database in the c# version?
Ankur\m/ 24-Feb-11 0:15am    
Can you show the idem data bound code in C# that you have used?

1 solution

Hi
You need to include following code in IsPostBack

C#
if(!IsPostBack)
{
string strGUID = Session["strGUID"];
dgDates.DataSource = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings(Web.[Global].CfgKeyConnStringADMIN), "dbo.spRptGetSchedulingDates", strGUID);
dgDates.DataBind();
intNumberOfDates = dgDates.Items().Count;
}


Thanks,
Imdadhusen
 
Share this answer
 
Comments
[no name] 24-Feb-11 8:38am    
The way my page is setup (it's actually an ASCX control which is a child of another ASCX control) everytime the page loads, it is always a postback, even the very first time, IstPostBack is true...and plus on the VB side that is still true and there is no issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900