我在asp.net中使用两个gridview,我可以获得检查哪一行(i am using two gridview in asp.net and i canot get which row is ch

编程入门 行业动态 更新时间:2024-10-14 06:20:26
我在asp.net中使用两个gridview,我可以获得检查哪一行(i am using two gridview in asp.net and i canot get which row is checked)

在他们中如果我使用相同的代码为一个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

本文发布于:2023-08-07 01:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   可以获得   两个   net   asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!