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>

...全文
290 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
熊哥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想说什么 有问题就问吧,不问,怎么给你解决。。
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2011-04-29 11:22
社区公告
暂无公告