C# gridview 点击Linkbutton透过某一条件修改数据
C# gridview 点击Linkbutton通过某一条件修改数据
C# gridview 通过点击Linkbutton(操作列的‘修改’按钮)修改数据。条件为‘修改’按钮绑定的 e.CommandArgument
sql语句为 update tb set 主营类目='值' (文本框的值) where 主营类目=e.CommandArgument

求具体实现方法
补充说明 gridview是动态绑定的,并且绑定的这张表中没有主键,只能通过点击当前行的‘主营类目’的值作为条件来修改表符合条件的数据
------解决思路----------------------
如:前台 <asp:LinkButton ID="lbtnDel" runat="server" CommandName="del" CommandArgument='<%#Eval("id")%>'
Text="删除" OnClientClick="return confirm('确定删除吗?');" OnClick="Operations">
后台代码:
public void Operations(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
if (lb.CommandName == "Edit")
Response.Redirect("xxx.aspx?id=" + lb.CommandArgument);
else
{
..............;
}
}
------解决思路----------------------
GridView呈现数据修改数据库数据更新示例
//GridView设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
//点击需要修改数据,修改后从新绑定数据显示
protected void ClassList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr0 = "update ST_class set ST_c_name='" + ((TextBox)(ClassList.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'where ST_c_id='" + ClassList.DataKeys[e.RowIndex].Value.ToString() + "'";
//调用公共类中的ExceSQL方法执行SQL语句
My_sqldata.ExceSQL(sqlstr0);
ClassList.EditIndex = -1;
ST_Type_List_Bind();
}
//加载文章类型的信息
public void ST_Type_List_Bind()
{
string sqlstr = "select * from ST_class order by ST_date desc ";
//调用公共类中的ExceDS()方法,用来返回一个DataSet类型
My_sqldata.ExceDS(sqlstr);
//获取数据表中的主键字段
ClassList.DataKeyNames = new string[] { "ST_c_id" };
ClassList.DataSource = My_sqldata.ExceDS(sqlstr);
ClassList.DataBind();
}
------解决思路----------------------
GridView呈现数据修改数据库数据更新示例
//GridView设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
//点击需要修改数据,修改后从新绑定数据显示
protected void ClassList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr0 = "update ST_class set ST_c_name='" + ((TextBox)(ClassList.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'where ST_c_id='" + ClassList.DataKeys[e.RowIndex].Value.ToString() + "'";
//调用公共类中的ExceSQL方法执行SQL语句
My_sqldata.ExceSQL(sqlstr0);
ClassList.EditIndex = -1;
ST_Type_List_Bind();
}
//加载文章类型的信息
public void ST_Type_List_Bind()
{
string sqlstr = "select * from ST_class order by ST_date desc ";
//调用公共类中的ExceDS()方法,用来返回一个DataSet类型
My_sqldata.ExceDS(sqlstr);
//获取数据表中的主键字段
ClassList.DataKeyNames = new string[] { "ST_c_id" };
ClassList.DataSource = My_sqldata.ExceDS(sqlstr);
ClassList.DataBind();
}
C# gridview 通过点击Linkbutton(操作列的‘修改’按钮)修改数据。条件为‘修改’按钮绑定的 e.CommandArgument
sql语句为 update tb set 主营类目='值' (文本框的值) where 主营类目=e.CommandArgument
求具体实现方法
补充说明 gridview是动态绑定的,并且绑定的这张表中没有主键,只能通过点击当前行的‘主营类目’的值作为条件来修改表符合条件的数据
------解决思路----------------------
如:前台 <asp:LinkButton ID="lbtnDel" runat="server" CommandName="del" CommandArgument='<%#Eval("id")%>'
Text="删除" OnClientClick="return confirm('确定删除吗?');" OnClick="Operations">
后台代码:
public void Operations(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
if (lb.CommandName == "Edit")
Response.Redirect("xxx.aspx?id=" + lb.CommandArgument);
else
{
..............;
}
}
------解决思路----------------------
GridView呈现数据修改数据库数据更新示例
//GridView设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
//点击需要修改数据,修改后从新绑定数据显示
protected void ClassList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr0 = "update ST_class set ST_c_name='" + ((TextBox)(ClassList.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'where ST_c_id='" + ClassList.DataKeys[e.RowIndex].Value.ToString() + "'";
//调用公共类中的ExceSQL方法执行SQL语句
My_sqldata.ExceSQL(sqlstr0);
ClassList.EditIndex = -1;
ST_Type_List_Bind();
}
//加载文章类型的信息
public void ST_Type_List_Bind()
{
string sqlstr = "select * from ST_class order by ST_date desc ";
//调用公共类中的ExceDS()方法,用来返回一个DataSet类型
My_sqldata.ExceDS(sqlstr);
//获取数据表中的主键字段
ClassList.DataKeyNames = new string[] { "ST_c_id" };
ClassList.DataSource = My_sqldata.ExceDS(sqlstr);
ClassList.DataBind();
}
------解决思路----------------------
GridView呈现数据修改数据库数据更新示例
//GridView设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
//点击需要修改数据,修改后从新绑定数据显示
protected void ClassList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sqlstr0 = "update ST_class set ST_c_name='" + ((TextBox)(ClassList.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'where ST_c_id='" + ClassList.DataKeys[e.RowIndex].Value.ToString() + "'";
//调用公共类中的ExceSQL方法执行SQL语句
My_sqldata.ExceSQL(sqlstr0);
ClassList.EditIndex = -1;
ST_Type_List_Bind();
}
//加载文章类型的信息
public void ST_Type_List_Bind()
{
string sqlstr = "select * from ST_class order by ST_date desc ";
//调用公共类中的ExceDS()方法,用来返回一个DataSet类型
My_sqldata.ExceDS(sqlstr);
//获取数据表中的主键字段
ClassList.DataKeyNames = new string[] { "ST_c_id" };
ClassList.DataSource = My_sqldata.ExceDS(sqlstr);
ClassList.DataBind();
}