use*_*443 5 .net c# asp.net gridview rowcommand
嗨我有一个网格视图,每行都有一个文本框,我试图获取RowCommand事件的值.下面的代码适用于期望第一行的所有行.第一行的textbox.text值始终为空.
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
Title <%# Eval("Title")%>
<asp:TextBox ID="TextBoxAddPost" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("postId") %>' runat="server">Add Post</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
bindGridView();
}
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddPost")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");
//Empty for first row but works for all others
Debug.WriteLine("row: " + row.RowIndex + ", textBox:" + textBox.Text.Trim());
GridView1.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码已经过简化以用于说明目的.每行实际上包含一个子gridview,因此为什么需要每行中的文本框.我担心page_load中的绑定会覆盖文本框的值,但是,如果没有page_load绑定,则不会触发rowCommand事件.
我觉得有点奇怪,它除了第一行以外的所有行都可以正常工作.
小智 0
要从文本框中获取数据,您必须首先通过放置以下代码来设置文本属性。
<asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
它肯定会从文本框中给出值。
无论哪种方式,您还可以设置 gridview 的 datakeynames 属性。单击此处获取数据键名称
| 归档时间: |
|
| 查看次数: |
8014 次 |
| 最近记录: |