Search code examples
asp.nethtmlcontrols

HtmlInputText Name property is not working and replace by ID


I am creating a series of input text controls from asp.net codebehind. I am using the below code, but this code doesn't set the name attribute in input control instead ID is set to the name attribute of input control. How to make name attribute different from ID.

                HtmlInputText sno = new HtmlInputText();
                sno.ID = "txtSno" + j;
                sno.Name = "txtSno-name";
                sno.Value = snoList[i].ToString();
                sno.Attributes.Add("class", "form-control");
                cellsno.Controls.Add(sno);

This is the final html which is rendered

<input name="txtSno1" type="text" id="txtSno1" value="1" class="form-control">

Expected is

<input name="txtSno-name" type="text" id="txtSno1" value="1" class="form-control">

Solution

  • You can't change the name property of the input using the HtmlInputText object's Name property.
    As mentioned in Microsoft docs here

    Use the Name property to determine the unique identifier name for an HtmlInputControl. In this implementation, the get accessor returns the value of the Control.UniqueID property. However, the set accessor does not assign a value to this property.