gridview模板列中的删除按钮事件传值问题?
gridview中模板列删除按钮 点击删除 删除当前行数据
点击删除的时候需要传入删除方法一个ID(就是数据库中当前行的主键)
girdview不是绑定数据源形式 用的是动态指定绑定数据
如果在girdview加载数据的时候 将每行数据的主键ID赋值给删除按钮?(CommandArgument)
部分代码如下
后台代码
前台代码
点击删除的时候需要传入删除方法一个ID(就是数据库中当前行的主键)
girdview不是绑定数据源形式 用的是动态指定绑定数据
如果在girdview加载数据的时候 将每行数据的主键ID赋值给删除按钮?(CommandArgument)
部分代码如下
后台代码
protected void btnSearch_Click(object sender, EventArgs e)
{
GridBing();
}
private void GridBing()
{
List<Goods> list = new List<Goods>();
list = GoodsBLL.getGoodsByOrder(this.txtName.Text, this.txtBegin.Text, this.txtEnd.Text);
double total = 0;
foreach (Goods item in list)
{
total += item.Price;
}
this.GridView1.DataSource = list;
this.GridView1.DataBind();
this.Label1.Visible = true;
string msg = "";
if (!string.IsNullOrEmpty(txtBegin.Text) && !string.IsNullOrEmpty(txtEnd.Text))
{
msg = "从 " + this.txtBegin.Text + " 至 " + this.txtEnd.Text;
}
msg += " 总计消费: " + total.ToString() + " 元";
this.Label1.Text = msg;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentColor=this.style.backgroundColor;this.style.backgroundColor='#E8E0E0'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentColor");
Button btn = (Button)e.Row.FindControl("btnDel");
btn.Attributes.Add("onclick", "return confirm('你确定删除吗?')");
int id = Convert.ToInt32(e.Row.Cells[4].Text);
Users user = UserBLL.getUserById(id);
e.Row.Cells[4].Text = user.Name;
if (e.Row.Cells[5].Text.Length > 8)
{
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(0, 8) + "……";
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GridBing();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
int num = GoodsBLL.delById(Convert.ToInt32(e.CommandArgument));
this.GridBing();
}
}
}
前台代码
<div id="showGv" style="text-align: center;">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
Height="127px" Width="1024px" AutoGenerateColumns="False" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound"
onrowcommand="GridView1_RowCommand" DataKeyNames="id">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="name" HeaderText="商品名称" />
<asp:BoundField DataField="price" HeaderText="商品价格">
<HeaderStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="count" HeaderText="商品数量">
<HeaderStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="datatime" HeaderText="购买时间">
<headerstyle width="100px" />
</asp:BoundField>
<asp:BoundField DataField="userid" HeaderText="购买人">
<HeaderStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="remarks" HeaderText="商品描述" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:Button ID="btnDel" runat="server" CommandName="del"
Text=" 删 除 " />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
...全文
相关推荐
GridView行中按钮的使用
**方法2:ButtonField**: 直接在GridView的列编辑界面添加ButtonField,设置CommandName属性(如"del"),并在GridView的`RowCommand`事件中处理按钮点击
设置gridview列宽(超好用)
使用DataBound事件另一种更动态的方法是在GridView的`DataBound`事件中设置列宽。这允许根据数据的实际需求动态调整列宽。
在GridView中删除列 等
- 使用模板列(`TemplateField`)可以自定义列的行为和外观,比如添加删除按钮或其他控件。 - 考虑用户体验,提供确认对话框提示用户是否确定要删除,防止误操作。
C#中GridView动态添加列的实现方法
在列头模板中,我们传入了"删除"作为文本;在数据行模板中,我们传入了LinkButton控件,以便在每一行中显示这个按钮。
Gridview按钮事件
#### 三、Gridview中按钮事件的处理在Gridview中添加按钮并触发其事件主要包括以下几个步骤:1. **添加Button列**: 首先需要在Gridview中添加一个Button列。


