我正在尝试将 Gridview 中按钮内的文本更改为同一行的一个单元格中的字符串
I'm trying to change the text inside the buttons in a Gridview to the string in one of the cells on the same row
这是我的 gridview 的样子
我想使第一个按钮中的文本与论坛名称匹配,在本例中它应该是 ssssss,它下面的按钮应该是 Seinfeld。我从我编写的服务获取的数组中获取数据,然后将该数组绑定到 GridView。我想我应该在创建 gridview 时更改按钮文本,但我不知道如何实现。
在框中输入您要查找的论坛名称,然后单击“开始”后,也会创建 gridview。
代码:
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:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="menu.aspx.cs" Inherits="MyInterface.menu" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:TextBox ID="tbforumname" runat="server"></asp:TextBox>
<asp:Button ID="btgo" runat="server" Text="Go" OnClick="btgo_Click" />
<br />
<br />
<asp:Label ID="lberror" runat="server"></asp:Label>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Button" />
</Columns>
</asp:GridView>
<br />
</form>
</body>
</html>
根据您想要设置文本的时间,使用 GridView_OnRowCreated 或 GridView_OnRowDataBound,使用您想要的单元格值设置按钮文本。
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;
}
}
}
如果您的按钮在 gridview 中有一个 asp:TemplateField
,您可以试试这个
<asp:TemplateField HeaderText="myButton" >
<ItemTemplate>
<asp:Button ID="btn" runat="server" Text='<%# Eval("ColumnTextName") %>'/>
</ItemTemplate>
</asp:TemplateField>
ColumnTextName
是 forumname 列的名称。每次填充 gridview 时都会写入按钮文本
编辑
ButtonField
试试这个
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;
}
}
并将此 属性 添加到网格视图 OnRowDataBound="gv_RowDataBound"
这是我的 gridview 的样子
我想使第一个按钮中的文本与论坛名称匹配,在本例中它应该是 ssssss,它下面的按钮应该是 Seinfeld。我从我编写的服务获取的数组中获取数据,然后将该数组绑定到 GridView。我想我应该在创建 gridview 时更改按钮文本,但我不知道如何实现。
在框中输入您要查找的论坛名称,然后单击“开始”后,也会创建 gridview。
代码:
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: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="menu.aspx.cs" Inherits="MyInterface.menu" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:TextBox ID="tbforumname" runat="server"></asp:TextBox>
<asp:Button ID="btgo" runat="server" Text="Go" OnClick="btgo_Click" />
<br />
<br />
<asp:Label ID="lberror" runat="server"></asp:Label>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Button" />
</Columns>
</asp:GridView>
<br />
</form>
</body>
</html>
根据您想要设置文本的时间,使用 GridView_OnRowCreated 或 GridView_OnRowDataBound,使用您想要的单元格值设置按钮文本。
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;
}
}
}
如果您的按钮在 gridview 中有一个 asp:TemplateField
,您可以试试这个
<asp:TemplateField HeaderText="myButton" >
<ItemTemplate>
<asp:Button ID="btn" runat="server" Text='<%# Eval("ColumnTextName") %>'/>
</ItemTemplate>
</asp:TemplateField>
ColumnTextName
是 forumname 列的名称。每次填充 gridview 时都会写入按钮文本
编辑
ButtonField
试试这个
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;
}
}
并将此 属性 添加到网格视图 OnRowDataBound="gv_RowDataBound"