将C#变量传递给HTML按钮

时间:2018-10-30 04:19:37

标签: javascript c# html asp.net button

我试图通过将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 

2 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分:1)

请改为使用每个按钮的ID名称,并包含.Text属性,如下所示:

Button1.Text = (dt.Rows[0][0]).ToString();