简体   繁体   English

Datagrid中的复选框保留VB.NET中回发时的检查值,但不保留C#

[英]Checkbox within Datagrid retaining checked value on postback in VB.NET but not C#

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. 嘿,我在VB.NET中有一个工作正常的项目,该项目实质上具有一个Datagrid,该Datagrid包含一个TemplateColumn,这是Checkboxes的一列。 The code to declare the datagrid is here... 声明数据网格的代码在这里...

<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>

as you can see the Checkbox has Autopostback="true" but there are other things on the page which produce postbacks as well. 如您所见,复选框具有Autopostback =“ true”,但是页面上还有其他东西也会产生回发。

My Page_load has this in it, being called on every load of the page, postbacks included... 我的Page_load里面有这个,每次页面加载时都会调用,包括回发...

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 以及我后面的代码对gridItemDataBound具有以下代码

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. 你看到的一切sbove工作完全正常......在这个意义上,当我点击其中一个复选框,在Page_Load火灾,上的DataBind的gridItemDataBound火灾(),当一切完成后,该复选框保留用户点击的价值复选框。

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?? 将所有这些完全相同的代码转换为C#....所有事件均以相同顺序触发,但是复选框选择的值始终清除...任何想法?

I'd say the problem is that you're are binding the grid on every postback (I can't explain the difference between VB and C#). 我要说的问题是,您在每次回发中都绑定了网格(我无法解释VB和C#之间的区别)。 This clears your selections. 这会清除您的选择。 Why are you doing this? 你为什么做这个?

You bind the grid on every page_load, this causes checkbox to be always the same. 您在每个page_load上绑定网格,这将导致复选框始终相同。

Try to attach a checkedchange event on ItemDataBound and when event fires, store checkbox value on a session variable. 尝试在ItemDataBound上附加一个checkedchange事件,并在事件触发时,将复选框值存储在会话变量中。

On ItemDataBound, check session variable and if its null, run this code, otherwise read checkbox's value from your session variable 在ItemDataBound上,检查会话变量,如果它为null,则运行此代码,否则从会话变量中读取复选框的值

And On Page_Load, if not IsPostBack (page is loading first time), set your session value to null 然后在Page_Load上,如果不是IsPostBack(页面正在第一次加载),则将会话值设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM