GridView怎么取去出主键直

wjh8618 2008-04-04 12:13:31
这是页面代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="305px" Width="912px" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="u_name" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="r_name" HeaderText="用户角色" />
<asp:BoundField DataField="u_name" HeaderText="用户名" />
<asp:BoundField DataField="u_state" HeaderText="状态" />
<asp:BoundField DataField="u_IP" HeaderText="用户IP" />
<asp:BoundField DataField="u_jointime" HeaderText="注册时间" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<table>
<tr>
<td style="width:100px"> <asp:LinkButton ID="linkGunbi" runat="server" CommandName="guanbi" OnCommand="linkGunbi_Command">关闭</asp:LinkButton></td>

<td style="width:80px"> <asp:LinkButton ID="linkChechu" runat="server" OnClientClick="return confirm('你确定删除吗');">删除</asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
这是代码:
protected void linkGunbi_Command(object sender, CommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
string u_name = GridView1.DataKeys[index].Value.ToString();
if (e.CommandName == "guanbi")
{
string u_name = e.CommandArgument.ToString();
string updateUser = "update users set u_state='禁用' where u_name='" + u_name + "'";
SqlConnection conn = DB.createConn();
conn.Open();
SqlCommand cmd = new SqlCommand(updateUser, conn);
cmd.ExecuteNonQuery();
conn.Close();
zhengChangUser();
jinYongUser();
}

}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string u_name = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
((LinkButton)e.Row.FindControl("linkGunbi")).CommandArgument = u_name;
}

...全文
74 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Allen_Chen_ 2008-04-04
  • 打赏
  • 举报
回复
你不是取出了吗.
蝶恋花雨 2008-04-04
  • 打赏
  • 举报
回复
<asp:TemplateField>
<HeaderTemplate > 删除 </HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbDel" OnClick="lbDel_Click" runat="server" CommandArgument=' <%#Eval("afficheid") %>' Text="删除" ToolTip="删除"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

C# code protected void lbDel_Click(object sender, EventArgs e)
{
string id=((LinkButton)sender).CommandArgument.ToString() ;//得到这行的ID
string strDel = "delete from dbo.bbs_affiche where afficheid='" +id + "'";
sqlt.Operate(strDel);
sqlt.ConClose();
MessageBox("a00006", null);
this.BindShowAffiche();
}
Allen_Chen_ 2008-04-04
  • 打赏
  • 举报
回复
哦..这个问题啊.检查e.Row.RowType
c8328349 2008-04-04
  • 打赏
  • 举报
回复
????
重发一下

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
string u_name = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
((LinkButton)e.Row.FindControl("linkGunbi ")).CommandArgument = u_name;
}
}
c8328349 2008-04-04
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
string u_name = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
((LinkButton)e.Row.FindControl("linkGunbi")).CommandArgument = u_name;
}
}

改一下

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧