2

What is the character limit for a control's id in a asp.net web application?

I have a scenario where I would be generating control id using Random function.

Ravi Vanapalli
  • 9,805
  • 3
  • 33
  • 43
  • 2
    possible duplicate of [What is a practical maximum length for HTML id?](http://stackoverflow.com/questions/584184/what-is-a-practical-maximum-length-for-html-id) There's no practical maximum, but there's an HTML specification which dictates what characters are allowed. So be careful with your Random function. Not to mention that Random doesn't mean unique and the HTML specification dictates, among other things, that ids must be unique. – Darin Dimitrov Jan 24 '12 at 14:23
  • I am adding time stamp to it for uniqueness. – Ravi Vanapalli Jan 24 '12 at 14:28
  • It may help to understand more about what you are trying to achieve. There is probably a better method for generating a unique ID than calling a random function each time, which only lowers the chance of multiple IDs. – Curtis Jan 24 '12 at 14:35
  • I am creating a screen which will have friends list, where end user can keep on adding friends. I need to have label and textbox added. – Ravi Vanapalli Jan 24 '12 at 14:37
  • As @Curt said, random numbers, and even timestamps, may not be unique. I use `Guid.NewGuid().ToString().Replace('-', '_')` (appended to some prefix) for (_almost_ guaranteed!) unique control IDs. – JonBrave Sep 01 '16 at 12:09
  • EDIT: To clarify my preceding comment, a .NET 4 `Guid` has 122 bits of randomness (see http://stackoverflow.com/questions/2757910/how-are-net-4-guids-generated), which is a lot more than the `Random` class's 32 bits. – JonBrave Sep 01 '16 at 12:21

2 Answers2

3

Stumbled on this while looking for info/thoughts on generating random IDs as an additional SPAMBot thwarting mechanism and thought I'd answer.

As Darin pointed out, while not specific to ASP.NET, the following previously posted question has an answer that states that 1 Million characters have been successfully used as a HTML ID in all modern browsers:

What is a practical maximum length for HTML id?

The quick DOTNET test page below ran in a 4.0 app pool confirms that ASP.NET handles this just fine—though at that size, loading the page and posting back can take a minute. Remember that the "name" attribute also takes on the "id" value, so it's effectively doubled.

I experienced strange browser behavior with IDs approaching 10 Million characters, but if you're thinking about using that large of an ID, you probably need to rethink a few things ;)

<script language="c#" runat="server">

    StringBuilder controlID = new StringBuilder();
    int controlIDLength = 1000000; // ONE MEEEEEEELLLLLION CHARACTERS!!!

    void Page_Init(object sender, EventArgs e)
    {
        // Create a really really long control ID
        for(int n = 1; n < controlIDLength; n++)
        {
            controlID.Append("A");
        }

        var TestControl = new System.Web.UI.WebControls.TextBox();
        TestControl.ID = controlID.ToString();
        ControlPlaceholder.Controls.Add(TestControl);
    }

    void Page_Load(object sender, EventArgs e)
    {
        ControlIDLengthLiteral.Text = "ControlID Length: " + controlIDLength.ToString();
        if (IsPostBack)
        {
            ControlValue.Text = "Value Received: " + ((TextBox)PageForm.FindControl(controlID.ToString())).Text;
        }
    }

</script>
<html>
    <head>
        <title>Maximum ID Length Test</title>
    </head>
    <body>
        <p>
            <asp:Literal ID="ControlIDLengthLiteral" runat="server" /><br />
            <asp:Literal ID="ControlValue" runat="server" />
        </p>
        <form id="PageForm" runat="server">
            <asp:PlaceHolder ID="ControlPlaceholder" runat="server" /><br />
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>
Community
  • 1
  • 1
kiddailey
  • 3,302
  • 3
  • 22
  • 23
1

It is a control property of datatype string.So You can add a value which will adopt into a string variable.

sathishkumar
  • 1,780
  • 4
  • 20
  • 31