gridview模板列中的删除按钮事件传值问题?

熊哥club 熊哥club 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
gridview中模板列删除按钮 点击删除 删除当前行数据
点击删除的时候需要传入删除方法一个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>

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
熊哥club
.NET社区 62,271 成员 668,968 内容