gridview控件使用模版后在里面放置了个LINKBUTTON,要在后台实现点击这个LINKBUTTON后获取它的Text该怎么做
3个回答
展开全部
先获取当前行的索引 int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
LinkButton lbtn= (LinkButton )(gdvApply.Rows[row].Cells[那一列].FindControl("linkbutton的名称"));
string test=lbtn.Text;
LinkButton lbtn= (LinkButton )(gdvApply.Rows[row].Cells[那一列].FindControl("linkbutton的名称"));
string test=lbtn.Text;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主先确定在什么事件中查找,有很多方式,给出两种方式,请参考
.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Name") %>'
CommandArgument='<%# Eval("Name") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Name") %>' onclick="LinkButton2_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
public partial class GridViewDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
List<User> list = new List<User>();
list.Add(new User { Id = 1, Name = "aa" });
list.Add(new User { Id = 2, Name = "bb" });
list.Add(new User { Id = 3, Name = "cc" });
list.Add(new User { Id = 4, Name = "dd" });
GridView1.DataSource = list;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string text = (string)e.CommandArgument;
if (!string.IsNullOrEmpty(text))
{
lblMsg.Text = text;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
lblMsg.Text = btn.Text;
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
<asp:Literal ID="lblMsg" runat="server"></asp:Literal>
.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Name") %>'
CommandArgument='<%# Eval("Name") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Name") %>' onclick="LinkButton2_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.aspx.cs
public partial class GridViewDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
List<User> list = new List<User>();
list.Add(new User { Id = 1, Name = "aa" });
list.Add(new User { Id = 2, Name = "bb" });
list.Add(new User { Id = 3, Name = "cc" });
list.Add(new User { Id = 4, Name = "dd" });
GridView1.DataSource = list;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string text = (string)e.CommandArgument;
if (!string.IsNullOrEmpty(text))
{
lblMsg.Text = text;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
lblMsg.Text = btn.Text;
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
<asp:Literal ID="lblMsg" runat="server"></asp:Literal>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FindControl试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询