Validators and ClientIDMode issue (ASP.NET) - c#

ASP.NET 4.0 has introduced the property 'ClientIDMode', which allows one to specify how the html element's ids are rendered in the output html.
My project is currently set to 'Static' (the shortest / contains no hierarchy id's)
When using any validator (RequiredFieldValidator, RegularExpressionValidator...), unless the control it's validating explicitly has it's property ClientIDMode:AutoID I get the error:
Input parameter 'controlId' cannot be
an empty string.
Is this the expected behaviour?
Edit: Being abit dumb with the error message, it wanted me to put an id on the Validator, which I obviously dont do (unless I need to reference it).
It still seems odd that it should fail because of this reason.

I ran into this in a slightly different way: if my user control had ClientIDMode="Static", I received the error on the validators that existed within the control (they did not all have ID's).
One workaround is to switch ClientIDModes on the offending user control. Hope this helps!

Related

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

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 !!
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.

What could cause this ASP.NET tag to be treated as a string?

I recently started working with some legacy ASP.NET stuff, and I've run into an interesting problem:
I've got a table displaying a few values, some of which are evaluated in C# server-side. They all work correctly. Then all of a sudden...
<td><asp:Label ID="Label2" runat="server" class='<%=SevenDayThresholdTooHighOrLow%>'><%=ChgFromSevenDaysAgoInfo%></asp:Label></td>
ChgFromSevenDaysAgoInfo is evaluated properly.
SevenDayThresholdTooHighOrLow is rendered as a string inside of the class quotations. That is
class="<%=SevenDayThresholdTooHighOrLow%>".
In the code-behind file, the two variables are declared in the same scope, and assigned values pretty much one after the other. My Visual Studio doesn't complain about not finding certain variables in code like it would if the property did not exist.
What other factors could be influencing this oddity? What could I have missed?
Thank you very much for your help, everyone!
Eli
EDIT: I took a look at what is the use of Eval() in asp.net and tried to set my tag up that way (class='<%# Eval("SevenDayThresholdTooHighOrLow")%>'), unfortunately to no effect.
That class attribute probably isn't being evaluated for server-side code, likely because class isn't a property of Label. (Label isn't an HTML element, it's a server-side control. So instead of HTML attributes it uses object properties.)
There's a CssClass property, you might try that instead. But even then the approach is different because the system may still not attempt to evaluate server-side code injected into already server-side components. Still, worth a try.
What should definitely work is setting the CssClass property in the code-behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
If you want to keep this out of code-behind (and who could blame you?) then another approach could be to replace the server-side control with an HTML element entirely. The idea being that if properties on this control aren't otherwise being set in server-side code, then does it really need to be a server-side control? If I remember correctly, a Label emits as a span:
<span class="<%=SevenDayThresholdTooHighOrLow%>"><%=ChgFromSevenDaysAgoInfo%></span>
You can't do that in just the markup code. You can't use server tags in a server control. You could use a data binding tag, but then you would need to trigger the data binding from the code behind anyway.
Just set the attribute from the code behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;

Required Field Validator + AsyncFileUpload

I have been trying to apply the default ASP.Net Required Field Validator to an AsyncFileUpload control (from the AJAX Control Toolkit).
The Scenario
I have created a Web User Control. Let’s call it wucFileUpload.
wucFileUpload has an AJAX Update Panel that is wrapping the Required Field Validator and the AsyncFileUpload. (I have to use the AJAX Update Panel for some particular reasons).
wucFileUpload is going to be used in many pages, and in some of them, this control is going to be generated automatically, so I do not know how many of them I will have per page.
wucFileUpload has a property called Required. If Required is true, it will enable the Required Field Validator, to check if the AsyncFileUpload has been filled at least once.
My try
I found this solution here on StackOverflow, and I tried to apply it. However, my case is a little bit different, from the one represented on that question.
My thoughts
I really liked the idea of having a hidden Textbox or a HiddenField. Because a single AsyncFileUpload can upload N number of files.
The Textbox could get some value on the first time OnClientUploadComplete runs… this way I’ll know the user has uploaded at least one image.
But I would also need a way of clearing that Textbox, because the user can delete the image that he just uploaded. And if the does that, then the field must be validated again.

How do you detect DOCTYPE (Transitional or Strict) server-side in ASP.NET?

I am wondering if there is a way to detect from the server-side which DOCTYPE the page is specified as. I have some HTML and CSS in a custom WebControl that renders differently depending on which DOCTYPE the page is. Is there a Page property or a Response property I could check?
Why don't you have an enum or a boolean on your control that the consuming pages can set (it's not like the doctype should be changing from page to page)?
Thing is, DOCTYPE is a client side declaration and it doesn't take part the control structure of ASP.NET pages (because it exists outside the html element of the page). I concur with #TheCloudlessSky, pass the setting to the control, as the only way I can see you detecting DOCTYPE is by opening up the file itself and reading the first line, this is also pretty useless for compiled web applications too.
Why will the DOCTYPE be changing from page to page?
The DocType will not change from page to page, but you do sometimes need access to it. I am scouring the internet at the moment trying to get at this data.
I know it is there in first HTMLElement (DomElement.data) because it is properly listed in debug mode, but using something like -
dElementList[str].DomElement.data;
This seems to be the only place where the data I need to get at exists, the data inside
Is not posible. I suspect there is a cast needed to a specific element type, but I have not yet come across it. The MSDN example for DomElement does not access any of the elements, but their code is basically the same as mine, dElement is a Dictionary, str is the ID or tag of the element (in this case '!' from it's tag), the Dictionary returns a HTMLElement.
So does anyone know how we get at the at DomElement.data ?

Infragistics WebTextEdit - Setting value in Javascript function

I'm currently using the Infragistics component set for .Net 2.0, Visual Studio 2005 and C#. I have the following chunk of javascript code (text and other variables are declared elsewhere):
***alert(box[select].value);
text.value(box[select].value);
alert(text.value);***
'text' is an Infragistics webTextEdit, while box is just a standard listbox. The two alerts seem to be working fine. Before I set the value, the listBox's selected value might be 'hello', and the alert box which pops up after I've assigned this value to 'text' is also 'hello'.
However, the value shown in the box on my form never appears to get updated. Anybody have some suggestions as to where I'm going wrong, gotchas in how Infragistics handles this kind of thing or anything else? I'm aware there may not be enough info here to diagnose the problem.
The value property is only available server-side. Using it client-side won't do anything. Setting it would have to be done server-side, or you'll need to craft fun javascript to address the text of the element that the control is actually rendered as in the browser.
http://help.infragistics.com/Help/NetAdvantage/NET/2007.3/CLR2.0/html/Infragistics2.WebUI.WebDataInput.v7.3~Infragistics.WebUI.WebDataInput.WebTextEdit~Value.html
Unless I misunderstand the question, if text is an instance of the Infragistics WebTextEdit, you should just be able to do:
text.setValue(box[select].value)
Or if text is the underlying input control, but 'id' is the ID of it,
var edit = igedit_getById(id)
edit.setValue(box[select].value)
See the WebTextEdit CSOM for more.

Categories