GridViewRow Cells在按钮中返回空字符串单击Gridview内的事件启动。 [英] GridViewRow Cells returning empty strings in button Click Event fire-up inside Gridview.
问题描述
我对这个问题感到困惑。
我在gridview的模板字段旁边放了一个按钮,想要从特定的GridView Row返回数据当点击相应的按钮时。
I am confused with this problem.
I have put a button in side the template field of a gridview and want to return the data from that specific GridView Row when that corresponding button is clicked.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" CssClass ="btnSkin" runat="server" Text="Answer"
Width="117px" onclick="Button2_Click" />
</ItemTemplate>
</asp:TemplateField>
<
In按钮单击事件fireup,我想通过创建GridViewRow元素来读取该数据。
In the button click event fireup, I want to read that data by creating a GridViewRow Element.
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
Label8.Text = gvr.Cells[1].Text;
Label10.Text = gvr.Cells[2].Text;
Label12.Text = gvr.Cells[3].Text;
}
现在的问题是,GridViewRow Cells返回空字符串。
我该怎么办?????
Now the problem is, the GridViewRow Cells are returning empty strings.
What should I do?????
推荐答案
试试这个
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
txt_bx.Text=((TextBox)GridView1.Rows[gvRow.RowIndex].FindControl("id of textbox in gridview")).Text;
}
问候..:)
Regards..:)
您好,
查看此链接,希望这能解决你的问题。
Hi,
Check this link ,Hope this will solve your question.
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow gvr= (GridViewRow)((Button)sender).NamingContainer;
Label8.Text = gvr.Cells[1].Text;
Label10.Text = gvr.Cells[2].Text;
Label12.Text = gvr.Cells[3].Text;
}
http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html [<一个href =http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html\"target =_ blanktitle =新窗口> ^ ]
当你想在gridview中触发任何控制事件时,那么控件的事件使用gridview的OnRowCommand活动。
在您的情况下请执行以下操作。
Hi,
When ever you want to fire any control event inside gridview, then instead that control's event use gridview's OnRowCommand event.
In your case do following.
<!--Aspx page-->
<asp:gridview id="YourGridID" runat="server" onrowcommand="YourGridID_RowCommand" ..="" xmlns:asp="#unknown">
<asp:templatefield>
<itemtemplate>
<asp:button id="Button2" cssclass="btnSkin" runat="server" text="Answer">
Width="117px" CommandName="GetRow" />
</asp:button></itemtemplate>
</asp:templatefield>
</asp:gridview>
//Code behind
protected void YourGridID_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetRow")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
//Here i am assuming you are having label inside your gridview
Label lbl= (Label)row.FindControl("YourLabelID");
Label lbl1= (Label)row.FindControl("YourLabelID");
Label lbl2= (Label)row.FindControl("YourLabelID");
Label8.Text = lbl.Text;
Label10.Text = lbl1.Text;
Label12.Text = lbl2.Text;
}
}
希望有所帮助。
Hope it helps.
这篇关于GridViewRow Cells在按钮中返回空字符串单击Gridview内的事件启动。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!