在他们中如果我使用相同的代码为一个gridview有1 gridview然后它的确定和代码工作正常,但当我在另一个页面使用它有两个gridview然后代码不工作我陷入谷歌搜索请帮助我我的代码是:
<div id="id1" style="overflow:scroll; height:155px; float:left; width:40%; " > <asp:GridView ID="dgvclass" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="ClassName" EmptyDataText="No record found !!" CellPadding="4" ForeColor="#333333"> <RowStyle BackColor="#E3EAEB" /> <EmptyDataRowStyle CssClass="gvEmpty" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" Width="40px" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Class Name" ItemStyle-Wrap="false" DataField="ClassName" /> <asp:BoundField HeaderText="ID" ItemStyle-Wrap="false" DataField="ClassID" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div> <div id="id2" style="overflow:scroll; height:155px; float:right; width:40%; margin-top:-16%; " > <asp:GridView ID="dgvcatogory" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="CategoryName" EmptyDataText="No record found !!" CellPadding="4" ForeColor="#333333"> <RowStyle BackColor="#E3EAEB" /> <EmptyDataRowStyle CssClass="gvEmpty" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Category Name" ItemStyle-Wrap="false" DataField="CategoryName" /> <asp:BoundField HeaderText="Category ID" ItemStyle-Wrap="false" DataField="CategoryID" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div>我的c#代码是:
foreach (GridViewRow row in dgvclass.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkrow = (row.Cells[0].FindControl("chkRow") as CheckBox); if (chkrow.Checked) { foreach (GridViewRow row1 in dgvcatogory.Rows) { if (row1.RowType == DataControlRowType.DataRow) { CheckBox chkrow1 = (row1.Cells[0].FindControl("chkRow1") as CheckBox); if (chkrow1.Checked) { //some condition } } } } } }它始终将chkRow值显示为false,如果我在gridview中将值设置为true,则它始终将其显示为true
in them if I use same code for a page having 1 gridview then its ok and code is working fine but when I am using it in another page where there is two gridview then code is not working I got stuck through googling please help me my code is:
<div id="id1" style="overflow:scroll; height:155px; float:left; width:40%; " > <asp:GridView ID="dgvclass" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="ClassName" EmptyDataText="No record found !!" CellPadding="4" ForeColor="#333333"> <RowStyle BackColor="#E3EAEB" /> <EmptyDataRowStyle CssClass="gvEmpty" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" Width="40px" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Class Name" ItemStyle-Wrap="false" DataField="ClassName" /> <asp:BoundField HeaderText="ID" ItemStyle-Wrap="false" DataField="ClassID" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div> <div id="id2" style="overflow:scroll; height:155px; float:right; width:40%; margin-top:-16%; " > <asp:GridView ID="dgvcatogory" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="CategoryName" EmptyDataText="No record found !!" CellPadding="4" ForeColor="#333333"> <RowStyle BackColor="#E3EAEB" /> <EmptyDataRowStyle CssClass="gvEmpty" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Category Name" ItemStyle-Wrap="false" DataField="CategoryName" /> <asp:BoundField HeaderText="Category ID" ItemStyle-Wrap="false" DataField="CategoryID" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div>and my c# code is:
foreach (GridViewRow row in dgvclass.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkrow = (row.Cells[0].FindControl("chkRow") as CheckBox); if (chkrow.Checked) { foreach (GridViewRow row1 in dgvcatogory.Rows) { if (row1.RowType == DataControlRowType.DataRow) { CheckBox chkrow1 = (row1.Cells[0].FindControl("chkRow1") as CheckBox); if (chkrow1.Checked) { //some condition } } } } } }it is always showing the chkRow value as false, if I set the value true in gridview then it always show it as true
最满意答案
因此,根据comments部分的讨论, !IsPostBack是导致问题的原因。
所以明显的解决方案是
if(!IsPostBack) { //Bind the gridviews }So, as per the discussion in the comments section, !IsPostBack is what causing the problem.
So the obvious solution is
if(!IsPostBack) { //Bind the gridviews }更多推荐
gridview,<asp,code,电脑培训,计算机培训,IT培训"/> <meta name="descrip
发布评论