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.
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.
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:
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>
It is a control property of datatype string.So You can add a value which will adopt into a string variable.