GridView中有个LinkButton控件,后台如何设置其Visible值
如题,在GridView中我有多行数据,而LinkButton主要用来操作,我需要根据这条数据到后台获取是否给用户展现LinkButton控件。如------------...
如题,在GridView中我有多行数据,而LinkButton主要用来操作,我需要根据这条数据到后台获取是否给用户展现LinkButton控件。
如
-------------------------------
ID Title 操作
1 a 查看 删除
2 b 查看 删除
操作部分使用的是LinkButton按钮,这时需要根据ID来判断“删除”是否提供给用户使用。
请大侠给个解决方案,小弟感激不尽! 展开
如
-------------------------------
ID Title 操作
1 a 查看 删除
2 b 查看 删除
操作部分使用的是LinkButton按钮,这时需要根据ID来判断“删除”是否提供给用户使用。
请大侠给个解决方案,小弟感激不尽! 展开
3个回答
2012-02-02
展开全部
如何在GridView实现选中,编辑,取消,删除数据的功能
前台实现代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" / >
<Columns >
<asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" / >
<asp:BoundField DataField="姓名" HeaderText="用户姓名" / >
<asp:BoundField DataField="员工性别" HeaderText="性别" / >
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" / >
<asp:CommandField HeaderText="选择" ShowSelectButton="True" / >
<asp:CommandField HeaderText="编辑" ShowEditButton="True" / >
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" / >
</Columns >
<RowStyle ForeColor="#000066" / >
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" / >
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" / >
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" / >
</asp:GridView >
后台实现代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//删除数据
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
//更新数据
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
//取消数据
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//绑定数据
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
}
前台实现代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" / >
<Columns >
<asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" / >
<asp:BoundField DataField="姓名" HeaderText="用户姓名" / >
<asp:BoundField DataField="员工性别" HeaderText="性别" / >
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" / >
<asp:CommandField HeaderText="选择" ShowSelectButton="True" / >
<asp:CommandField HeaderText="编辑" ShowEditButton="True" / >
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" / >
</Columns >
<RowStyle ForeColor="#000066" / >
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" / >
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" / >
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" / >
</asp:GridView >
后台实现代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//删除数据
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
//更新数据
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
//取消数据
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//绑定数据
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
}
参考资料: http://www.lmwlove.com/ac/ID685
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询