如何在ASP.NET中的代码中的HTML按钮中获取隐藏字段值 [英] How to get hidden field value in a HTML button in code behind in ASP.NET

查看:72
本文介绍了如何在ASP.NET中的代码中的HTML按钮中获取隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试过的方法:



我试过以下



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



button_edit_ServerClick



I have created an HTML table with a repeater. In this table, I added a button column. It is an HTML button because ASP buttons not working inside of the tag. Therefore, I added an ASP hidden field inside of this HTML button to get selected row ID. I tried several ways to get the ID from the hidden field. I want to get selected row ID when the button click.

What I have tried:

I have tried followings

I have tried this code but shows error in SendValueToSender(id); line. SendValueToSender is shown in red line using suggestions It generated method. But when I run the code and click the button shows error.

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的生成方法





Generated method for SendValueToSender

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





错误 - 添加SendValueToSender方法后



System.NotImplementedException:未实现方法或操作。 at EasyTravel.Manage.ManageNode.SendValueToSender(String id)位于C:\ Users \ kularathna \ source \repos\EasyTravel \EasyTravel \Manage \ManageNode.aspx.cs:EasyTravel.Manage第241行.ManageNode.button_edit_ServerClick(Object sender,EventArgs e)在C:\ Users \ kularathna \ source \repos\EasyTravel \EasyTravel \Manage \ManageNode.aspx.cs:第229行





Error-After add SendValueToSender method

System.NotImplementedException: The method or operation is not implemented. at EasyTravel.Manage.ManageNode.SendValueToSender(String id) in C:\Users\kularathna\source\repos\EasyTravel\EasyTravel\Manage\ManageNode.aspx.cs:line 241 at EasyTravel.Manage.ManageNode.button_edit_ServerClick(Object sender, EventArgs e) in C:\Users\kularathna\source\repos\EasyTravel\EasyTravel\Manage\ManageNode.aspx.cs:line 229

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





PageLoad方法





PageLoad Method

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





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之上)





Button Column(It is in above 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>

推荐答案

您得到的错误是 NotImplementedException ,这是正常的,因为
The error you get is a NotImplementedException, which is normal since the line
throw new NotImplementedException();

已达到。



这意味着单击该按钮会实际调用 SendValueTosender()方法。您可能忘记实现 SendValueToSender()方法。

is reached.

Which means that the click of the button acually calls the SendValueTosender() method. You may have forgotten to implement the SendValueToSender() method.


这篇关于如何在ASP.NET中的代码中的HTML按钮中获取隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆