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

熊哥club 2011-04-29 11:22:48
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>

...全文
361 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊哥club 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hsc614540542 的回复:]
后台代码: string cmd = e.CommandName;//获取命令的名称
int Id = Convert.ToInt32(e.CommandArgument);//获取命令的参数
if (cmd == "dele")//删除
{
RoomManager.DeleteRoomInfoById(Id);
}

前台代码:
<ItemTemplate……
[/Quote]

我绑定的不是数据源 CommandArgument='<%# Eval("ID") %>' 这样用必须报错。必须后台指定给删除按钮CommandArgument值
hsc614540542 2011-04-29
  • 打赏
  • 举报
回复
后台代码: string cmd = e.CommandName;//获取命令的名称
int Id = Convert.ToInt32(e.CommandArgument);//获取命令的参数
if (cmd == "dele")//删除
{
RoomManager.DeleteRoomInfoById(Id);
}

前台代码:
<ItemTemplate>
<asp:ImageButton ID="ImageBtnDelete" runat="server" CausesValidation="False" CommandName="dele"
ImageUrl="~/pageImage/delete.gif" Text="删除" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
熊哥club 2011-04-29
  • 打赏
  • 举报
回复
楼上没看清楚问题么/? 我想说的是 在gridview加载的时候 如何将每行的id赋值给删除按钮?
  • 打赏
  • 举报
回复
LZ想说什么 有问题就问吧,不问,怎么给你解决。。

62,256

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧