【问题标题】:How to add button in html table and create event using asp.net and C#?如何在 html 表中添加按钮并使用 asp.net 和 C# 创建事件?
【发布时间】:2016-06-09 15:34:13
【问题描述】:

我想在 C# 的 html 表格列中添加按钮。我已经附加了后面的代码,并且在 code.button 中的 asp.net 正在为每一行的列中创建,但没有调用 click 事件。

        public string getWhileLoopData()
    {
        string htmlStr = "";

        SqlCommand thisCommand = thisConnection.CreateCommand();
        thisCommand.CommandText = "SELECT Id,EmployeeEmail,PayReference from EmployeeDetails";
        thisConnection.Open();
        SqlDataReader reader = thisCommand.ExecuteReader();

        PlaceHolder PlaceHolder1 = new PlaceHolder();

        while (reader.Read())
        {
            long id = reader.GetInt64(0);
            string Name = reader.GetString(1);
            string Pass = reader.GetString(2);[enter image description here][1]
            htmlStr += "<tr class='row_data'><th data-employee-id='" + id + "'>" + id + "</th><th>" + Name + "</th><th>" + Pass + "</th><th>  <input id='Button1' runat='server' class='button right' onserverclick='Button1_Click' type='submit' value='Update' /> </th></tr>";
        }

        thisConnection.Close();
        return htmlStr;
    }

ASP.NET 代码如下

    <table id="myTable" class="table table-striped">
        <thead align="left" style="background-color: #004080; color: White;">
            <tr>
                <th>
                    Id
                </th>
                <th>
                    Email Address
                </th>
                <th>
                    Empr Paye Ref.
                </th>
                <th>
                    Open
                </th>
            </tr>
        </thead>
        <tbody>
            <%=getWhileLoopData()%>
        </tbody>
    </table>

【问题讨论】:

  • 使用 c# 中的原始数据,您将能够引发 javascript 事件而不是服务器回发。您将需要像中继器一样支持模板的控件,您可以在其中指定单击事件。顺便说一句,您也可以使用 javascript 手动回发
  • 只是好奇为什么不为此使用 ASP.net 网格或列表视图控件?
  • 我想使用 HTML 表格和 JQuery 进行分页和搜索,我的代码适合网格响应。

标签: c# html asp.net .net


【解决方案1】:

没有名为onserverclick 的事件。 您可以使用 asp 按钮代替 html 按钮并使用 onclick 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2020-03-24
    • 2013-08-06
    • 2015-04-01
    • 2015-05-11
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多