如何按照单击的复选框从gridview获取值 [英] How to get the value from gridview as per checkbox clicked

查看:123
本文介绍了如何按照单击的复选框从gridview获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上面创建了Gridview和复选框,当我选中多个复选框时,我想获取所选复选框的移动编号.
网格上的字段是Name,MobileNo,并且每行都添加了复选框.
任何人都可以帮助我.

I Created Gridview and checkboxes on that and when I Check the multiple checkboxes I want to get the mobile nos for selected checkboxes.
Fields on Grid is Name ,MobileNo and Checkboxes is added on each row .
Anyone can help me plzzzzz.

推荐答案

您遇到的问题是什么?
这是一个示例:

//HTML/aspx页面
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="false" DataKeyNames ="ID">
<列>
< asp:TemplateField>
< ItemTemplate>
< asp:CheckBox ID ="chkSel" runat ="server"/>
</ItemTemplate>
</asp:TemplateField>
< asp:BoundField DataField ="ID" HeaderText ="EmployeeId" SortExpression ="EmployeeId"/>
< asp:BoundField DataField ="FName" HeaderText ="FirstName" SortExpression ="FirstName"/>
< asp:BoundField DataField ="MobileNo" HeaderText ="MNo" SortExpression ="Age"/>
</列>
</asp:GridView>
< asp:Button ID ="btnRetrieveCheck" runat ="server"
Text ="Retrieve Checked Items" onclick ="btnRetrieveCheck_Click"/>

//服务器端代码
受保护的无效btnRetrieveCheck_Click(对象发送者,EventArgs e)
{
foreach(GridView1.Rows中的GridViewRow行)
{
CheckBox cb =(CheckBox)row.FindControl("chkSel");
if(cb!= null& cb.Checked)
{
Response.Write("Employee ID:" + GridView1.DataKeys [row.RowIndex] .Value
+"MobileNo:" + row.Cells [3] .Text);
}
}
}
What was the issue you faced?
Here is a sample:

//Html/aspx page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSel" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="EmployeeId" SortExpression="EmployeeId" />
<asp:BoundField DataField="FName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="MobileNo" HeaderText="MNo" SortExpression="Age" />
</Columns>
</asp:GridView>
<asp:Button ID="btnRetrieveCheck" runat="server"
Text="Retrieve Checked Items" onclick="btnRetrieveCheck_Click" />

//Server side code
protected void btnRetrieveCheck_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSel");
if (cb != null && cb.Checked)
{
Response.Write("Employee Id: " + GridView1.DataKeys[row.RowIndex].Value
+ " MobileNo: " + row.Cells[3].Text);
}
}
}


这篇关于如何按照单击的复选框从gridview获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆