在asp.net,GridView的模版列中加入按钮,触发按钮事件后,如何获取该行的某个值?
在asp.net,GridView的模版列中加入按钮,触发按钮事件后,如何获取该行的某个值? --------------------编程问答-------------------- GridView1.Rows[e.RowIndex].Cells[某列].FindControl("控件名") --------------------编程问答-------------------- 先放个文本控件进去!!!!!! --------------------编程问答-------------------- 是个普通按钮呀。e.RowIndex 没这个方法的--------------------编程问答-------------------- GridView1.Rows[e.RowIndex].Cells[0].FindControl("控件名")
就可以取到 --------------------编程问答-------------------- 应该是单机事件里边吧? --------------------编程问答--------------------
你那按钮是写在GridView外面了?
是的话,你用将行索引存入隐藏控件
GridView1.Rows[隐藏控件的值].Cells[某列].FindControl("控件名") --------------------编程问答-------------------- 哦。单机事件
--------------------编程问答--------------------
是GridView 追加了一个列 --------------------编程问答-------------------- 页面代码
<asp:GridView>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="true" SortExpression="id" >
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="Button1_onClick" ImageUrl="~/Admin/Image/修改按钮.jpg" Visible="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:GridView/>
后台代码
protected void Butto1_onClick(object sender, EventArgs e)
{
怎么获得id值
}
--------------------编程问答-------------------- 你是总要有个点击行的事件或者一个复选框吧,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#99cc00';document.getElementById('HiddenField1').value="+GridView1.DataKeys[e.Row.RowIndex].Value+"; window.oldtr=this");
}
}
HiddenField1 --------------------编程问答-------------------- 没看懂。能说的清楚点吗?
--------------------编程问答-------------------- 在 每行的这个按钮上 附上 主键的值 --------------------编程问答-------------------- 哦。怎么附加呀。
--------------------编程问答-------------------- 前台放个按钮 比如删除
<asp:TemplateField HeaderText="delete" ShowHeader="False">
<ItemStyle Width="50px" />
<ItemTemplate>
<asp:Button ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick ='javascript:return confirm('确定删除<%# Bind("id")%>吗?')' Text="删除"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
后台。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)--------------------编程问答-------------------- 模板列内空间是button类型,Gridview的名称为gv,获取第一列单元格的值,如下
{
GridView1.DataKeys[e.RowIndex].Value.ToString();
}
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;
string id = gv.Rows[row].Cells[0].Text.ToString(); --------------------编程问答--------------------
<div id="layer" style="position:absolute;z-index:99;border:1px solid red;display:none"></div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="姓名" />
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" onclick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)--------------------编程问答--------------------
{
GridViewRow row = (GridViewRow)(sender as ImageButton).NamingContainer;
int index = row.RowIndex;
Response.Write(GridView1.Rows[index].Cells[0].Text);
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ID" HeaderText="姓名" />
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" CommandName="GetText" CommandArgument='<%#Eval("ID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
--------------------编程问答-------------------- protected void Button1_Click(object sender, EventArgs e)
//或
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetText")
{
Response.Write(e.CommandArgument.ToString());
}
}
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;
TextBox id =(TextBox )(GridView1 .Rows [row].FindControl ("TextBox3")) ;
Response.Write(id.Text );
}
模版里Button1的事件,TextBox3也是模版中的控件,这样能取到TextBox3中的值,把TextBox3换成boundfiled控件,则可以用15楼的代码
补充:.NET技术 , ASP.NET