C# I';我试图将Gridview中按钮内的文本更改为同一行某个单元格中的字符串
这就是我的gridview的外观 我想让第一个按钮中的文本与forumname匹配,在本例中,它应该是SSSS,下面的按钮应该是Seinfeld。我从我编写的服务中获取的数组中获取数据,然后将数组绑定到GridView。我认为我应该在创建gridview时更改按钮文本,但我不知道如何实现这一点 此外,gridview是在框中键入您要查找的论坛名称,然后单击go后创建的 代码:C# I';我试图将Gridview中按钮内的文本更改为同一行某个单元格中的字符串,c#,asp.net,gridview,C#,Asp.net,Gridview,这就是我的gridview的外观 我想让第一个按钮中的文本与forumname匹配,在本例中,它应该是SSSS,下面的按钮应该是Seinfeld。我从我编写的服务中获取的数组中获取数据,然后将数组绑定到GridView。我认为我应该在创建gridview时更改按钮文本,但我不知道如何实现这一点 此外,gridview是在框中键入您要查找的论坛名称,然后单击go后创建的 代码: protected void btgo_Click(object sender, EventArgs e)
protected void btgo_Click(object sender, EventArgs e)
{
string forumname = tbforumname.Text;
if (string.IsNullOrEmpty(forumname) == true)
{
lberror.Text = "Please enter a forum to search!";
}
else if (ws.GetList(forumname) == null)
lberror.Text = "didn't found any matching forums!";
else
{
GridView1.DataSource = ws.GetList(forumname);
GridView1.DataBind();
}
}
Html:
使用GridView\u OnRowCreated或GridView\u OnRowDataBound,根据您想要设置文本的时间,使用您想要的单元格值设置按钮文本
创建的行:
行数据绑定:
如果按钮的gridview中有一个
asp:TemplateField
,您可以试试这个
<asp:TemplateField HeaderText="myButton" >
<ItemTemplate>
<asp:Button ID="btn" runat="server" Text='<%# Eval("ColumnTextName") %>'/>
</ItemTemplate>
</asp:TemplateField>
并将此属性添加到gridview
OnRowDataBound=“gv_RowDataBound”
发布asp标记对不起,asp标记到底是什么?显示我发布asp的html代码(也称asp标记),以及在这种情况下该怎么办?我没有TemplateField,我没有使用Button,而是使用ButtonField,ID没有显示Hanks mate!你真是个传奇人物,这对纽扣场来说太完美了!好的,但是如果(e.Row.RowType==DataControlRowType.DataRow)返回false,那么它不会执行我们需要的操作。知道为什么吗?因为你添加了asp语法,看起来你使用的是一个按钮字段,而不仅仅是一个按钮字段,所以你必须对它进行修改。试着添加好吧,伙计,我试着这么做,但我得到了一个错误。所以我试着像Enrique Zavaleta在Buttonfield上发布的那样做,看起来效果不错!这个网站太棒了。
public void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button button = e.Row.Cells[0].FindControl("myButtonId") as Button;
Label label = e.Row.Cells[2].FindControl("myLabelId") as Label;
if(button != null && label != null)
{
button.Text = label.Text;
}
}
}
<asp:TemplateField HeaderText="myButton" >
<ItemTemplate>
<asp:Button ID="btn" runat="server" Text='<%# Eval("ColumnTextName") %>'/>
</ItemTemplate>
</asp:TemplateField>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
int colText = 1; //the index of the column you want to get the text
int colButton = 0; //the index of the column of your button
if (e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.Cells[colButton].Controls[0] as Button).Text = e.Row.Cells[colText].Text;
}
}