删除按钮不在gridview表格中如何获得gridview选中行的索引 求帮助 急急!!
展开全部
页面:<asp:button runat="server" id="btnDelete" Text="删除" onclick="btnDelete_Click" />
<asp:hiddenfield runat="server" id="hfID"></asp:hiddenfield>
<asp:gridview runat="server" ID="GridView1" onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="ID" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
</asp:gridview>
.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow GridR = GridView1.SelectedRow;
string s = ((Label)GridR.FindControl("lblID")).Text;
hfID.Value = s;//将选中行的主键放入隐藏控件中
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string id = hfID.Value;
//根据id获得这条记录,然后删除
}
<asp:hiddenfield runat="server" id="hfID"></asp:hiddenfield>
<asp:gridview runat="server" ID="GridView1" onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="ID" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
</asp:gridview>
.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow GridR = GridView1.SelectedRow;
string s = ((Label)GridR.FindControl("lblID")).Text;
hfID.Value = s;//将选中行的主键放入隐藏控件中
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string id = hfID.Value;
//根据id获得这条记录,然后删除
}
展开全部
private void button1_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
}
private void button2_Click(object sender, EventArgs e)
{
this.label1.BackColor = Color.Blue;
}
private void button3_Click(object sender, EventArgs e)
{
this.Close;
}
{
this.BackColor = Color.Red;
}
private void button2_Click(object sender, EventArgs e)
{
this.label1.BackColor = Color.Blue;
}
private void button3_Click(object sender, EventArgs e)
{
this.Close;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在这个按钮事件中写:
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = gvr.FindControl("CheckBox1") as CheckBox;
Label label = gvr.FindControl("Label1") as Label;
if (checkbox.Checked)
{
Response.Write("<script>alert('"+label.Text.Trim()+"');</script>");
}
}
}
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = gvr.FindControl("CheckBox1") as CheckBox;
Label label = gvr.FindControl("Label1") as Label;
if (checkbox.Checked)
{
Response.Write("<script>alert('"+label.Text.Trim()+"');</script>");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
datagridview1.CurrentRow.Cells["id"].Value获取选中行的ID,其他的你懂的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
传ID
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询