将C#变量传递给HTML按钮

4
我将尝试通过传递C#变量来动态更改网页上5个HTML按钮的文本/值。我通过页面加载中的SQL查询生成变量,但无法想出如何将变量传递给按钮。
        DataSet ds= new DataSet();
        DataTable dt= new DataTable();
        connection.Open();
        string commandstring = "SELECT TOP (5) [ButtonVal] FROM Table";
        SqlDataAdapter adptr = new SqlDataAdapter(commandstring, connection);
        adptr.Fill(ds);
        dt = ds.Tables[0];
        Btn1 = System.Convert.ToString(dt.Rows[0][0]);
        Btn2 = System.Convert.ToString(dt.Rows[1][0]);
        Btn3 = System.Convert.ToString(dt.Rows[2][0]);
        Btn4 = System.Convert.ToString(dt.Rows[3][0]);
        Btn5 = System.Convert.ToString(dt.Rows[4][0]);

HTML:

    <table>
<tr>
 <td><asp:Button ID="Button1" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td> 
 <td><asp:Button ID="Button2" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>        
 <td><asp:Button ID="Button3" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>     
 <td><asp:Button ID="Button4" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>   
 <td><asp:Button ID="Button5" text="XXX" value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>
<tr />

OnClick函数根据按钮的值重定向到另一个页面。

* 根据Jim W的答案编辑 *

        1)
          C#:
            public string Btn1
            if (!Page.IsPostBack)
            {
               Btn1 = (dt.Rows[0][0]).ToString();
            }

          HTML:
            <td><asp:Button ID="Button1" Text="<%# Btn1 %>" Value ="<%# Btn1 %>" 
            style="font-size:8px;height:30px;width:60px" runat="server"  
            AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>

          Output:
             Blank Button 

        2)
          C#:

            if (!Page.IsPostBack)
            {
               Button1.Text = (dt.Rows[0][0]).ToString();
            }

          HTML:
            <td><asp:Button ID="Button1" Text="<%# Button1 %>" Value ="<%# Button1 %>" 
            style="font-size:8px;height:30px;width:60px" runat="server"  
            AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>

          Output:
             Button text is "System.Web.UI.WebControls.Button" 

        3)
          C#:
            public string Btn1
            if (!Page.IsPostBack)
            {
               Btn1 = System.Convert.ToString(dt.Rows[0][0]);
            }

          HTML:
            <td><asp:Button ID="Button1" Text="<%# Btn1 %>" Value ="<%# Btn1 %>" 
            style="font-size:8px;height:30px;width:60px" runat="server"  
            AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>

          Output:
             Blank Button             

        4)
          C#:
            public string Btn1
            if (!Page.IsPostBack)
            {
               Btn1 = (dt.Rows[0][0]).ToString();
            }

          HTML:
            <td><asp:Button ID="Button1" Text="<%# Btn1 %>" Value ="<%# Btn1 %>" 
            style="font-size:8px;height:30px;width:60px" runat="server"  
            AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td>

          Output:
             Blank Button 

至少,按钮的'Click'处理程序PostBack将以'CommandName'的形式公开发起请求的按钮的值:因此,请填写一个合理的值(通过CommandName属性)。 - user2864740
请查看我的更新答案,你可能只需要在你的标记中使用Text=而不是text= - Jim W says reinstate Monica
了解前端是一个好主意,现在你已经完成了后端。接下来可以使用JS、JQuery或Angular进行脚本编写,这将处理所有后端数据。 - Juniar
2个回答

1
你可以使用数据绑定来实现,参见https://support.microsoft.com/en-us/help/307860/asp-net-data-binding-overview
例如:
 <td><asp:Button ID="Button1" Text="<%# Btn1 %>" Value ="XXX" style="font-size:8px;height:30px;width:60px" runat="server"  AutoPostBack="true"  OnClick="ChangeRedirect_Click" />   </td> 

在您发布的变量生成代码的末尾(或之后),调用Page.DataBind()

更新:完整示例

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NS.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" Text="<%# Btn1 %>" runat="server"/>
    </div>
    </form>
</body>
</html>

代码后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NS
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected string Btn1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Btn1 = "hello";
            Page.DataBind();
        }
    }
}

Jim,谢谢你的帮助 - 这让我更接近目标了,但现在按钮文本显示为“System.Web.UI.WebControls.Button”,而不是实际文本。我尝试将Text =“Button1.Value”和Text =“Button1.Text”放入代码中,但都没有起作用。 - ls101
@Is101,那个编辑很令人困惑,因为你的按钮ID与变量Button1相同。请回到使用Btn1,或者只是使用Button1.Text = (dt.Rows[0][0]).ToString();并忘记数据绑定。如果你还卡住了,请更新完整的代码,这样我们就在同一个页面上了。 - Jim W says reinstate Monica
我认为你基本上是将按钮文本设置为按钮对象本身,这就是我想说的。 - Jim W says reinstate Monica
@Is101,你可以按照你帖子中的方法#1进行操作,但是你必须调用Page.DataBind()。我会在我的回答中添加一个完整的示例。 - Jim W says reinstate Monica

1
请使用每个按钮的ID名称,并包括一个属性.Text,如下所示:
Button1.Text = (dt.Rows[0][0]).ToString();

当您为Button1.Text(其中“Button1”是runat=server按钮的ID)分配一个值时,您不需要/希望任何数据绑定表达式覆盖这些值。因此,只需使用Text=" "即可正常工作。 - Hans Kesting
@Is101,你可能需要使用循环语句。通过在正确的位置将数据插入到相应的按钮中来遍历整个数组。例如Button.text,Button.label等。 - Juniar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接