Is ctl00 a constant in ASP NET?

ahmd0 picture ahmd0 · Jul 6, 2011 · Viewed 7.6k times · Source

I need to reference a control in my web app that was generated with the use of a master page. The name of the control in the HTML becomes something like this "ctl00$MainContent$ListBox1". Can I safely do this in the code?

string strName = "ctl00$MainContent$ListBox1";
if (Request.Form[strName] != null)
{
String selectedLanguage = Request.Form[strName];
}

PS. I cannot use ClientID property because this code is called from InitializeCulture() override.

Answer

Code Maverick picture Code Maverick · Jul 6, 2011

You could, but what I do is set the MasterPage ID in my Init():

protected void Page_Init( object sender, EventArgs e ) 
{
    // this must be done in Page_Init or the controls 
    // will still use "ctl00_xxx", instead of "Mstr_xxx"
    this.ID = "Mstr"; 
}