When $ sign is in the Id of the ASP TextBox, FindControl method returns null

580 views Asked by At

I am writing to you because I want to understand what is happening.

In my code, I add controls dynamically to the System.Web.UI.WebControls.GridView (I extended it). After some action on the page, I am checking if the ID of the Control from Page.Request["__EVENTTARGET"] is the one, which I saved earlier in ViewState.

On .Net 3.5 everything works fine, but after switching my project to .Net 4 this doesn't work, because Page.Request["__EVENTTARGET"] returns Inherit ClientId, and I keeped in ViewState only short Id of my control. I fixed it by using Page.FindControl method. It works for LinkButtons, Buttons and DropDowns, but not for TextBoxes, because Page.Request["__EVENTTARGET"] returns name of my TextBox...

So I change ClientIdMode for my TextBox to Static, like 'string$string'. But still Page.FindControl was returned null. I have read that if TextBox is generated dynamically and it is nested in some controls, this method is not able to find it.

My first question. Why method Page.FindControl is not able to find nested TextBox, but it can handle with others control like DropDown or LinkButton?

But I handle this problem also. I wrote recursive kind of Page.FindControl and I can find my TextBox. But in this moment I came across a problem again. Value of this TextBox is old (I write 6, but in the Text property there is still 1). My first idea was that this is because I was doing it too early - in OnPagePreLoad method.

But then I change Id of my TextBox from string$string to 'string_string' and it started to work fine (value of my text box is that I wrote).

And this is my second question. Why dollar ($) sign cause I saw the old value?

Thanks !!

1

There are 1 answers

0
McCee On BEST ANSWER

The $ character is not valid in the ID property. See this Microsoft article for more information

Specifically,

Only combinations of alphanumeric characters and the underscore character ( _ ) are valid values for this property. Including spaces or other invalid characters will cause an ASP.NET page parser error.

Also, see this Microsoft article about the IdSeparator property.

Specifically:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

So replacing the $ with an underscore seems like the right answer.