GridView怎么取去出主键直
这是页面代码:
<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;
}