如何在asp.net中的代码后面的html按钮中获取隐藏字段值

时间:2019-05-18 05:16:45

标签: c# html css asp.net

我用中继器创建了一个HTML表。在此表中,我添加了一个按钮列。这是一个HTML按钮,因为ASP按钮在标记内部不起作用。因此,我在此HTML按钮内添加了一个ASP隐藏字段,以获取选定的行ID。我尝试了几种从隐藏字段中获取ID的方法。我想在单击按钮时获得选定的行ID。

我已经尝试了以下操作

我尝试了此代码,但在 SendValueToSender(id); 行中显示错误。 SendValueToSender使用建议以红线显示。它生成的方法。但是,当我运行代码并单击按钮时,会显示错误。

button_edit_ServerClick

    protected void button_edit_ServerClick(object sender, EventArgs e)
    {
        try
        {
            var btn = (HtmlButton)sender;
            var child = btn.FindControl("hidden");
            string id = Convert.ToString(((HiddenField)child).Value);
            SendValueToSender(id);
            Response.Write("id" + id);

        }
        catch (Exception exception)
        {
            Response.Write(exception);
        }
    }

SendValueToSender的生成方法

     private void SendValueToSender(string id)
    {
        throw new NotImplementedException();
    }

错误-添加SendValueToSender方法后

System.NotImplementedException:方法或操作未实现。在C:\ Users \ kularathna \ source \ repos \ EasyTravel \ EasyTravel \ Manage \ ManageNode.aspx.cs:EasyTravel.Manage.ManageNode.button_edit_ServerClick(对象发送方, C:\ Users \ kularathna \ source \ repos \ EasyTravel \ EasyTravel \ Manage \ ManageNode.aspx.cs:line 229中的EventArgs e)

229 - SendValueToSender(id);
241 - throw new NotImplementedException();

PageLoad方法

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Create Database Connection
            SqlConnection con = new SqlConnection("Data Source= LAPTOP-J70EHC58 ; Initial Catalog= Bus_Management_System ; Integrated Security = True ; Connect Timeout = 30 ; ");
        con.Open();

        //Retrieve node details
        string sqlst = "SELECT * FROM Node ";
        SqlDataAdapter sqlData = new SqlDataAdapter(sqlst, con);
        DataTable dt = new DataTable();
        sqlData.Fill(dt);
        rptrNode.DataSource = dt;
        rptrNode.DataBind();

        }

    }

ManageNode.aspx

    <table id="datatable-buttons" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>Node_ID</th>
                                        <th>Node_Name</th>
                                        <th>Starting_Node</th>
                                        <th>Ending_Node</th>
                                        <th>Distance_Between_Nodes</th>
                                        <th>Ticket_Price</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    <asp:Repeater ID="rptrNode" runat="server">
                                        <ItemTemplate>
                                            <tr>
                                                <td>
                                                    <asp:Label ID="lblNodeID" runat="server" Text='<%# Eval("Node_ID") %>'></asp:Label></td>
                                                <td>
                                                    <asp:Label ID="lblNodeName" runat="server" Text='<%# Eval("Node_Name") %>'></asp:Label></td>
                                                <td>
                                                    <asp:Label ID="lblStartingNode" runat="server" Text='<%# Eval("Starting_Node") %>'></asp:Label></td>
                                                <td>
                                                    <asp:Label ID="lblEndingNode" runat="server" Text='<%# Eval("Ending_Node") %>'></asp:Label></td>
                                                <td>
                                                    <asp:Label ID="lblDistance" runat="server" Text='<%# Eval("Distance_Between_Nodes") %>'></asp:Label></td>
                                                <td>
                                                    <asp:Label ID="lblTicketPrice" runat="server" Text='<%# Eval("Ticket_Price") %>'></asp:Label></td>
                                                <td>
                                                    <button runat="server" clientidmode="Static" class="btn btn-success" id="button_edit" onserverclick="button_edit_ServerClick">
                                                        <asp:HiddenField runat="server" ID="hidden" Value='<%#Eval("Node_ID") %>' />
                                                        Edit
                                                    </button>

                                                </td>
                                            </tr>

                                        </ItemTemplate>
                                    </asp:Repeater>
                                </tbody>


                            </table>

按钮列(位于ManageNode.aspx上方)

    <td>
      <button runat="server" clientidmode="Static" class="btn btn-success" id="button_edit" onserverclick="button_edit_ServerClick">
          <asp:HiddenField runat="server" ID="hidden" Value='<%#Eval("Node_ID") %>' />
      Edit
     </button>
          </td>

1 个答案:

答案 0 :(得分:0)

在设计部分中,加载Jquery Js和Page Js。

<td>
  <button runat="server" clientidmode="Static" class="btn btn-success" 
  id="button_edit" onclick="func('<%#Eval("Node_ID") %>')">
  Edit
 </button>
</td>

在页面Js中:

$(document).ready(function(){
 //button Click Function.
 function func(Id){
  $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../ManageNode.aspx/SendValueToSender",

        data: "{ID: '" + Id + "'}",
        dataType: "json",
        success: function (data) {
            var tdata = jQuery.parseJSON(data.d);
        },
        error: function (result) {
            alert('Data not found.');
        }

    });
 }
});

在服务器端。

[WebMethod]
public static void SendValueToSender(string ID)
{
   //do your stuff. 
}