gridview rowcommand事件下怎么样更新数据库某一列的值,具体代码怎么操作呢
展开全部
前台Gridview中:
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="MyCommandList" CommandArgument='<%#Eval("MacID")%>'
OnClientClick="return Show()" Text='<%# Eval("Day_StartShut")%>' ></asp:LinkButton>
</ItemTemplate>
后台方法:
if (e.CommandName == "MyCommandList")
{
LinkButton btn = (LinkButton)e.CommandSource;
GridViewRow row = (GridViewRow)btn.Parent.Parent;
string MacID = e.CommandArgument.ToString();
string sql = "SELECT MacID,Day_StartUp,Day_ShutDown,ABS(Day_StartUp-Day_ShutDown) AS Day_StartShut,col_date";
sql += " From DW10048 WHERE (col_date>='" + txtStartDay.Value + "' and col_date <='" + txtEndDay.Value + "') and macid = '" + MacID + "'";
sql +=" order by col_date desc";
DataTable dtlist = l_Help.GetDataTable(sql);
DataView view = dtlist.DefaultView;
GvList1.DataSource = view;
GvList1.DataBind();
}
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="MyCommandList" CommandArgument='<%#Eval("MacID")%>'
OnClientClick="return Show()" Text='<%# Eval("Day_StartShut")%>' ></asp:LinkButton>
</ItemTemplate>
后台方法:
if (e.CommandName == "MyCommandList")
{
LinkButton btn = (LinkButton)e.CommandSource;
GridViewRow row = (GridViewRow)btn.Parent.Parent;
string MacID = e.CommandArgument.ToString();
string sql = "SELECT MacID,Day_StartUp,Day_ShutDown,ABS(Day_StartUp-Day_ShutDown) AS Day_StartShut,col_date";
sql += " From DW10048 WHERE (col_date>='" + txtStartDay.Value + "' and col_date <='" + txtEndDay.Value + "') and macid = '" + MacID + "'";
sql +=" order by col_date desc";
DataTable dtlist = l_Help.GetDataTable(sql);
DataView view = dtlist.DefaultView;
GvList1.DataSource = view;
GvList1.DataBind();
}
追问
能不能加点详细的注释啊!我有点看不懂
追答
前台中 就是定义个按钮放在模板列里面,然后设置他的CommandName 这个就是为了在后台防止你有好几个要执行的方法 用这个来做区别。然后设置CommandArgument 这个是你用来执行SQL 语句的参数,比如说你执行update语句 where条件中 ID = CommandArgument 。
后台呢 就是在command方法里面,首先判断e。CommandName 是哪个名字,如果匹配的话 就继续执行。 LinkButton btn = (LinkButton)e.CommandSource这个就相当于实例化或者说是找到这个这个按钮,因为是在模板列所以找不到。 GridViewRow row = (GridViewRow)btn.Parent.Parent;
获取到你选中的这一行。把设置好的参数获取到string MacID = e.CommandArgument.ToString();
.对啊。你把参数 就是把你GRIDVIEW的 的ID列 绑定到CommandArgument。然后
LinkButton btn = (LinkButton)e.CommandSource;
GridViewRow row = (GridViewRow)btn.Parent.Parent;
string MacID = e.CommandArgument.ToString();
写好这些。。再写上你的SQL 语句 比如 update 表 set a=0 where ID = MacID,执行这个语句 就完事了啊。。如果说像执行完看到结果的话 就重新绑定下你的gridview 就行了啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询