locked
Type 'System.Web.UI.WebControls.HiddenField' does not have a public property named 'name'. RRS feed

  • Question

  • User-195907812 posted

    Hi,

    I have a single webform that I am using to pass data to PayPal as follows:

    <form id="formPayPal" runat="server">
    <asp:HiddenField ID="hdCommand" name="cmd" runat="server" />
    <asp:HiddenField ID="hdButtonId" name="hosted_button_id" runat="server" />
    <asp:HiddenField ID="hdCustom" name="custom" runat="server" />
    </form>

    I am inserting the value via the code-behind, like:

    hdCommand.Value = "_s-xclick";

    However, when the form is executed I get a "hdCommand : Type 'System.Web.UI.WebControls.HiddenField' does not have a public property named 'name'."

    Is a HiddenField control not that same as a HTML "<input type="hidden" name="cmd" value="_s-xclick">"?

    The name attribute is what's used in form data, so I'm a bit confused.

    Wednesday, April 25, 2018 5:22 PM

Answers

  • User-369506445 posted

    hi

    you can use below code instead of it

      <input type="hidden" ID="hdCommand" name="cmd" runat="server"/>

    your full sample :

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            $(function() {
               var hidvalue= $("#hdCommand").val();
               if (hidvalue != "") {
                   alert(hidvalue);
               }
            });
        </script>
    </head>
    <body>
        <form id="formPayPal" runat="server">
           <%-- <asp:HiddenField ID="hdCommand" name="cmd" runat="server" />--%>
          <%--  <asp:HiddenField ID="hdButtonId" name="hosted_button_id" runat="server" />
            <asp:HiddenField ID="hdCustom" name="custom" runat="server" />--%>
            <input type="hidden" ID="hdCommand" name="cmd" runat="server"/>
            <input type="hidden" ID="hdButtonId" name="hosted_button_id" runat="server" />
            <input type="hidden" ID="hdCustom" name="custom" runat="server" />
        </form>
    
    </body>
    </html>
     protected void Page_Load(object sender, EventArgs e)
            {
                hdCommand.Value = "_s-xclick";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 25, 2018 5:39 PM

All replies

  • User-369506445 posted

    hi

    you can use below code instead of it

      <input type="hidden" ID="hdCommand" name="cmd" runat="server"/>

    your full sample :

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            $(function() {
               var hidvalue= $("#hdCommand").val();
               if (hidvalue != "") {
                   alert(hidvalue);
               }
            });
        </script>
    </head>
    <body>
        <form id="formPayPal" runat="server">
           <%-- <asp:HiddenField ID="hdCommand" name="cmd" runat="server" />--%>
          <%--  <asp:HiddenField ID="hdButtonId" name="hosted_button_id" runat="server" />
            <asp:HiddenField ID="hdCustom" name="custom" runat="server" />--%>
            <input type="hidden" ID="hdCommand" name="cmd" runat="server"/>
            <input type="hidden" ID="hdButtonId" name="hosted_button_id" runat="server" />
            <input type="hidden" ID="hdCustom" name="custom" runat="server" />
        </form>
    
    </body>
    </html>
     protected void Page_Load(object sender, EventArgs e)
            {
                hdCommand.Value = "_s-xclick";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 25, 2018 5:39 PM
  • User1120430333 posted

    If the Hidden field is an input type, then it has a name attribute. 

    Wednesday, April 25, 2018 5:45 PM
  • User409696431 posted

    DA924, the HiddenField does not have a name, it has a value.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield(v=vs.110).aspx

    Wednesday, April 25, 2018 7:41 PM
  • User1120430333 posted

    DA924, the HiddenField does not have a name, it has a value.

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield(v=vs.110).aspx

    https://www.w3schools.com/jsref/dom_obj_hidden.asp

    Wednesday, April 25, 2018 8:14 PM
  • User409696431 posted

    DA924 ,You seem to be confused.  That link does not describe the asp.net HiddenField, which is the topic of this thread.

    Wednesday, April 25, 2018 8:31 PM
  • User-195907812 posted

    (though not fully tested) vahid bakkhi's method does work (thank you) but I'm still confused why the .NET control isn't working.

    Anyone?

    Wednesday, April 25, 2018 8:58 PM
  • User-369506445 posted

    because The name attribute is automatically computed from the ID properties of the hidden field and its ancestors in the naming container chain. You don't get to set it yourself. You can only access it through the UniqueID of the control.

    Wednesday, April 25, 2018 9:18 PM
  • User1120430333 posted

    DA924 ,You seem to be confused.  That link does not describe the asp.net HiddenField, which is the topic of this thread.

    I gave something  that is hidden and has "name" attribute, if the OP chose to use it. I know what a hidden field is about.  

    Wednesday, April 25, 2018 10:49 PM