在gridview中嵌套一个imagebutton按钮,当点击按钮的时候怎样获取该行的主键值
请各位尽量讲述的详细一些,最好附带示例代码
谢谢
请各位尽量讲述的详细一些,最好附带示例代码
谢谢
解决方案 »
- 扩展程序所在的 UpdatePanel 不能与它所扩展的控件不同
- location 的路径
- 怎么为动态生成的按钮添加事件??
- 求教一个javascript问题。
- 数据库连接在服务器调试时需要改动sqlConnection吗?
- DataList或Reapter中的button可以添加脚本吗?
- asp.net比较初级的问题
- a href="#" onclick="OpenEditProduct()"链接完全没反应,这是哪儿出错了???
- C# 如何写访问windows共享文件判断用户名密码的权限提供指定的程序功能
- ajex(仙桃人) 进来拿分
- flash播放器播放FLV格式文件的问题
- JavaScript 省市区三级连动
<Columns>
<asp:BoundField DataField="ITEMID" HeaderText="编号" ReadOnly="True">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="ITEMNAME" HeaderText="栏目名称">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="ITEMDESC" HeaderText="栏目描述">
<ItemStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Center" Width="40%" />
</asp:BoundField>
<asp:BoundField DataField="ITEMORDER" HeaderText="栏目顺序">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="10%" />
</asp:BoundField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" DeleteImageUrl="image/icon_del.gif" DeleteText="<img src='image/icon_del.gif' border='0' alt='删除' />">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="10%" />
</asp:CommandField>
</Columns>
<RowStyle Height="25px" />
</asp:GridView>
protected void grdvItem_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
if (DeleteNewsItem(Convert.ToInt32(grdvItem.Rows[e.RowIndex].Cells[0].Text)) == 1)
{
// 添加操作成功后进行操作的代码
}
else
{
Response.Write("<script>alert('保存失败!\\n请确认填写信息后重新提交。');</script>");
}
}
catch (Exception)
{
Response.Write("<script>alert('保存失败!\\n请确认填写信息后重新提交。');</script>");
}
InitNewsItemList();
grdvItem.DataBind();
}
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtndddd" runat="server" CommandName="dd" CommandArgument ="<%# GridView1.Rows.Count %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "dd")
{
int index = Convert.ToInt32(e.CommandArgument);
string CID = GridView1.DataKeys[index].Value.ToString();
}
}
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnOK" runat="server" CommandName="OK" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "OK")
{
int index = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex; //通过下面的方式找到当前行主键
string id = GridView1.DataKeys[index].Value.ToString(); //然后做你的数据库操作
}
}
((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;这句话中e.CommandSource应该能定位到按钮吧,然后e.CommandSource).NamingContainer应该是定位到行吧(定位到cells应该是错误的吧),
{
if (!IsPostBack)
{
//绑定GridView
}
}
string id = GridView1.DataKeys[Row.RowIndex].Value.ToString();
{
GridViewRow currentRow = ((CheckBox)sender).Parent.Parent as GridViewRow; // 选中的行
Session["bid"] = currentRow.RowIndex + 1; if (currentRow.RowIndex + 1 > 0)
{
int count = 0;
// 获取主键编号
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1"); if (cb != null && cb.Checked)
{
this.masid = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);
Session["masid"] = this.masid; GridView1.SelectedIndex = row.RowIndex;
}
}
}
这是我在gridview中嵌套checkbox是获取选中行的主键的方法,我认为这和我现在嵌套imagebutton去选中行的原理应该是一样的,可是现在问题是我怎么都取不到呀!!!郁闷!
每次取到的值都是0