asp.net中,gridview绑定的选择框checkbox,多选后获取选中的id,点击button按钮,跳转到下一个页面的Girdview中
时间: 2024-02-17 09:00:56 浏览: 174
在 ASP.NET 中,可以使用以下步骤获取 GridView 绑定的多选 CheckBox 的选中项 ID 并将其传递到下一个页面中的 GridView。
1. 在 Gridview 中添加一个 TemplateField,将其中的 ItemTemplate 属性设置为 CheckBox。
```asp
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
```
2. 在 button 的 Click 事件中,遍历 GridView 中的每一行,检查每个 CheckBox 是否选中,并将选中的行的 ID 添加到 List 中。
```C#
protected void Button1_Click(object sender, EventArgs e)
{
List<string> selectedIDs = new List<string>();
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1");
if (checkBox.Checked)
{
selectedIDs.Add(row.Cells[1].Text); //第2列为ID列
}
}
//将选中的 ID 传递到下一个页面
Response.Redirect("NextPage.aspx?SelectedIDs=" + string.Join(",", selectedIDs));
}
```
3. 在下一个页面的 Page_Load 事件中,获取传递的选中 ID,并将其用于绑定另一个 GridView。
```C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["SelectedIDs"]))
{
string[] selectedIDs = Request.QueryString["SelectedIDs"].Split(',');
//使用选中的 ID 绑定另一个 GridView
GridView2.DataSource = GetData(selectedIDs);
GridView2.DataBind();
}
}
}
private DataTable GetData(string[] selectedIDs)
{
//根据选中的 ID 获取数据
//...
}
```
阅读全文
相关推荐














