我想做一个效果,在gridview中的第一行第二列的值如果是系统管理员的话,就不能删除
部分代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="4" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="用户编号">
<ItemStyle CssClass="table" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="用户姓名" />
<asp:BoundField DataField="Limication" HeaderText="用户权限" />
<asp:BoundField DataField="telephone" HeaderText="联系方式" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="btnupdate" runat="server" CommandName="btnupdate" CssClass="btn"
Text="修改" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Btndelete" runat="server" CssClass="btn" Text="删除" CommandName="Delinfo" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPrevious" />
<PagerStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:GridView>
cs代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string Id;
int RowIndex;
RowIndex = Convert.ToInt32(e.CommandArgument.ToString());
string StrConn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
if (e.CommandName == "Delinfo")
{
Id = GridView1.Rows[RowIndex].Cells[0].Text;
string Sql = "delete from tb_User where UserId = " + Id;
ExecPro.ExecPro.ExecuteNonQuery(StrConn, CommandType.Text, Sql);
showuser();
{
Response.Write("<script language=javascript>alert('删除成功!');historyback();</script>");
}
} if (e.CommandName == "btnupdate")
{
Id = GridView1.Rows[RowIndex].Cells[0].Text;
Response.Redirect("04-01-update.aspx?ID=" + Id);
} }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ Button But = (Button)e.Row.Cells[5].FindControl("Btndelete");
But.CommandArgument = e.Row.RowIndex.ToString(); //定义删除按钮的命令参数 }
if (e.Row.RowType == DataControlRowType.DataRow)
{ Button But = (Button)e.Row.Cells[4].FindControl("btnupdate");
But.CommandArgument = e.Row.RowIndex.ToString(); }
}
我是新手,赐教,也不知道在哪个事件里写,怎么判断
部分代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="4" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="用户编号">
<ItemStyle CssClass="table" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="用户姓名" />
<asp:BoundField DataField="Limication" HeaderText="用户权限" />
<asp:BoundField DataField="telephone" HeaderText="联系方式" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="btnupdate" runat="server" CommandName="btnupdate" CssClass="btn"
Text="修改" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Btndelete" runat="server" CssClass="btn" Text="删除" CommandName="Delinfo" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPrevious" />
<PagerStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:GridView>
cs代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string Id;
int RowIndex;
RowIndex = Convert.ToInt32(e.CommandArgument.ToString());
string StrConn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
if (e.CommandName == "Delinfo")
{
Id = GridView1.Rows[RowIndex].Cells[0].Text;
string Sql = "delete from tb_User where UserId = " + Id;
ExecPro.ExecPro.ExecuteNonQuery(StrConn, CommandType.Text, Sql);
showuser();
{
Response.Write("<script language=javascript>alert('删除成功!');historyback();</script>");
}
} if (e.CommandName == "btnupdate")
{
Id = GridView1.Rows[RowIndex].Cells[0].Text;
Response.Redirect("04-01-update.aspx?ID=" + Id);
} }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ Button But = (Button)e.Row.Cells[5].FindControl("Btndelete");
But.CommandArgument = e.Row.RowIndex.ToString(); //定义删除按钮的命令参数 }
if (e.Row.RowType == DataControlRowType.DataRow)
{ Button But = (Button)e.Row.Cells[4].FindControl("btnupdate");
But.CommandArgument = e.Row.RowIndex.ToString(); }
}
我是新手,赐教,也不知道在哪个事件里写,怎么判断
xzzz = GridView1.Rows[1].Cells[2].Value
if(GridView1.Rows[1].Cells[2].Value == "管理员")
{
//提示不可删除
}
else
{
//删除操作
}
例如 能删除的用户字段是usename能删除的人是admin
那么就 <asp:Button ID="Btndelete" runat="server" CssClass="btn" Text="删除" CommandName="Delinfo" visble=' <%bind("Usename")=="admin"?true:false'%>
/>
{
if (e.Values["字段名"].ToString() == "管理员")
{
e.Cancel = true;
}
}