【问题标题】:Getting c# function value and displaying in html table获取 c# 函数值并在 html 表中显示
【发布时间】:2014-07-25 14:35:42
【问题描述】:

我正在尝试获取 c# 函数返回的字符串并将其显示在我创建的 html 表中。我有一个调用 c# button_onclick 函数的 asp.net 按钮:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

我还有一个html表格:

<table>
<tr>
    <th>Col1</th>
    <th><span>Col2</span></th>
    <th>Col3</th>
</tr>

<tr>
    <td>Random1</td>
    <td>Random2</td>
    <td>Random3</td>
</tr>
</table>

我有一个链接到按钮单击的 c# 函数。

protected void Button1_Click(object sender, EventArgs e)
{
  //not sure what to do here. I want to call a separate function which will return
  //an array of 3 strings which would go in place of Random1, Random2, Random3 in the table
}

我不使用 asp.net 表的原因是因为我想将 css 用于我的 html 表(如果有办法将该 css 合并到 asp.net 表中,或者如果我的逻辑在这里完全关闭)。 知道我应该如何处理这个问题吗?

【问题讨论】:

    标签: c# html css asp.net visual-studio


    【解决方案1】:

    使您的&lt;td&gt; runat 服务器成为HtmlTableCell

    <td runat="server" id="td1">Random1</td>
    <td runat="server" id="td2">Random2</td>
    <td runat="server" id="td3">Random3</td>
    

    因此您可以在点击事件中访问它们:

    protected void Button1_Click(object sender, EventArgs e)
    {
         td1.InnerText ="Random1";
         td2.InnerText ="Random2";
         td3.InnerText ="Random3";
    }
    

    因为 HtmlTableCell 是 HtmlContainerControl 的子类,它提供了设置或获取 Innertext 和 InnerHtml 的属性,而这些属性又将在 &lt;td&gt; 中呈现

    【讨论】:

      【解决方案2】:

      一种选择是将asp:Label 放入您的表格中

      <tr>
          <td><asp:label id="lblRandom1" runat="server" /></td>
          <td><asp:label id="lblRandom2" runat="server" /></td>
          <td><asp:label id="lblRandom3" runat="server" /></td>
      </tr>
      

      并从Button1_Click 中填充它们

      此外,您可以通过设置CssClass 属性来将 css 与 asp 表一起使用,例如

      <asp:Table CssClass="classname" runat="server">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        • 2015-07-05
        • 1970-01-01
        相关资源
        最近更新 更多