如何在GridView的itemTemplate内部访问链接按钮单击事件中的linkbutton [英] how to access linkbutton in link button click event inside itemtemplate in gridview
问题描述
如何访问gridview的itemtemplate内的链接按钮单击事件中的链接按钮.帮帮我.谢谢.
how to access linkbutton in link button click event inside itemtemplate in gridview .Help me.Thanks in advance.
推荐答案
请尝试此操作...
Please try this...
LinkButton lnkGridSubCategory = (LinkButton)gvdatasubcategory.FindControl("lnkGridSubCategory");
[请通过投票给您适合的解决方案或答案来鼓励参与]
[Please encourage participation by up-voting solutions or answers that work for you]
尝试一下
Try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("yourLinkButtonID") as LinkButton;
string text = lb.Text; // read
lb.Text = "some"; // write
}
}
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
访问链接按钮在gridview的itemtemplate内的链接按钮单击事件
access linkbutton in link button click event inside itemtemplate in gridview
假设单击的行的行值不确定,则不确定要尝试访问的值.
一种方法是在ItemTemplate
内为LinkButton控件添加Click事件.
Not sure what value you are trying to access , assuming Row Values of the Clicked Row.
One way is to add Click event for the LinkButton Control inside the ItemTemplate
protected void lnkBtn_Click(object sender, EventArgs e)
{
GridViewRow gridrow = (GridViewRow)((LinkButton)sender).NamingContainer;
// If need to get the value of BoundField use below
string value = gridrow.Cells[0].Text;// Change index of Cells[0] to get the specific cell value
//Or if trying to access the value of Control inside ItemTemplate then use below Line of Code
Label lblControl = (Label)gridrow.FindControl("ControlID");
string value = lblControl.Text;
}
否则,将CommandArgument和CommandName添加到链接"按钮并使用GridView控件的RowCommand事件. CommandArgument应该与获取所需的值绑定
喜欢
Or other way is to Add CommandArgument and CommandName to the Link Button and use the RowCommand Event of the GridView Control . The CommandArgument should be bound with the value needed to fetch
like
CommandArgument=''<%# Eval("ID") %>'' and CommandName="lnkClick"
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "lnkClick")
{
String ID = e.CommandArgument.ToString();
}
}
这篇关于如何在GridView的itemTemplate内部访问链接按钮单击事件中的linkbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!