UniqueID vs html element actual id - asp.net

I'm creating composite control, which has two other components that rely on each other.
In component A (image), I access component's B (input) UniqueID which equals
MyTextBox1$BoldTextBox
I use it in onclick JavaScript code...
But in rendered HTML input element has following id
MyTextBox1_BoldTextBox
So my javascript code , generated inside composite control has something like this:
onclick=$('#MyTextBox1$BoldTextBox').....
instead of:
onclick=$('#MyTextBox1_BoldTextBox').....
Could somebody please explain what is happening, and how can I reliably associate those two controls ?
Thanks , Paweł

You want the ClientID property instead of the UniqueID property.
The reason for this is (sort of) explained on Atanas Korchev's blog post "The difference between Id, ClientID, and UniqueID".

Related

how to pull a textbox value into an anchor tag querystring

How do I pull a value from an "asp:TextBox" and pass it in an anchor tag?
txtTaskName is my "asp:TextBox"
Add Email Distribution Lists
I know this should be simple and I've done it before, but am in a crunch and don't have my old sourcecode. Thanks All.
If you have your control already filled, you can use
<a href='EmailManager.aspx?<%=txtTaskName.Text %>'>Add Email Distribution Lists</a>
If you want to do it on change, you should use PostBack="True" and an OnTextChanged event (server side) for txtTaskName control, or use Javascript (client side).
Perhaps you could use a <%= ... => displaying expression rather than a data-binding expression?
Add Email Distribution Lists
I do not think you would have to specify runat="server" either in this particular example.
You can get more information on inline expressions from this article.
Note that this will only work if txtTaskName.Text has a value before the inline expression is evaluated, else the value will be string.Empty.

Validation while rendering multiple partial views

I have multiple views/models that are being looped into the main view. I load them via #Html.Partial()...These views/models are basically form elements with certain properties...Unfortunately I soon found out that only the first of each type of view/model is validated. I tried moving the fields around and only the first of each kind would validate.
My partial views look something like this:
#Html.DropDownListFor(model=>model.dropdownVal,Model.SelectItems,new { id=Model.FieldID, Name = Model.FieldID })
I looked at the HTML rendered, and it seems that the validation tags like "data-val" are not applied...
Any ideas would be greatly appreciated!
Add the following at the top of your partials to trick ASP.NET MVC into thinking that the helpers are used inside a form and generate the proper data-val attributes:
#{
this.ViewContext.FormContext = new FormContext();
}
Basically the Html.* helpers are generating data-val clients side validation attributes only if they are placed inside an Html.BeginForm. Except that in your case I guess that this Html.BeginForm is in your parent view and not inside the partial, so the #Html.DropDownListFor doesn't emit any validation attributes. By setting the current FormContext to a new instance in the partial as shown previously, the helper will generate the proper client side validation attributes on the corresponding input field.

How can I access custom Textbox attributes in ASP.Net?

I am using/abusing CSS classes and custom html attributes to provide default data to a set of textboxes. The code-front for this looks like the following (with some supporting javascript to handle checking/setting the default data when the field is blank):
<asp:TextBox ID="TXT_LenderName" class='defaultText' data-default='Institution Name' runat="server"></asp:TextBox>
This works.
I am working on the code-behind to process this form. I would like to be able to compare the value of the TXT_LenderName.Text to the value of the data-default attribute, but I haven't been able to find a way to get the value of a custom html attribute. Suggestions?
This is tested and worked
string customAttrDataDefault = TXT_LenderName.Attributes["data-default"];
txtpassword.Attributes.Add("value","Password value");
try this:
TXT_LenderName.Attributes["AttributeName"]= value;//here get or set the value.
If the control, like the TextBox control inherits from the System.Web.UI.WebControls.Control class then it should have an Attributes property which is a name value pair collection of the control's attributes.

ASP.NET dynamic controls and validators - Unable to find control id '...' referenced by the 'ControlToValidate' property of '...'

I have seem many forum posts / responses on this subject but I am no closer to a solution.
I am loading a repeating data structure from the DB into UpdatePanel controls in an event handler. The number of repeats varies so controls are all created and added to a container panel dynamically. The user has the ability to edit the data so I am also dynamically creating validator controls (RegularExpressionValidators)
The problem is in setting the ControlToValidate property of the validator. No matter how I set this I get the Unable to find control id '...' referenced by the 'ControlToValidate' property of '...' error response from the server which is then raised by ASP.NET AJAX JS.
The short version of the control adding code looks like this:
TextBox licenseCode = new TextBox();
licenseCode.ID = this.getNextId(); // generated ID contains only [A-Za-Z], [0-9], :, -, .
licenseCode.MaxLength = 50;
this.purchaseEdit.Controls.Add(licenseCode); // this.purchaseEdit is an asp:Panel
RegularExpressionValidator licenseCodeVal = new RegularExpressionValidator();
licenseCodeVal.ControlToValidate = licenseCode.ClientID;
licenseCodeVal.ID = this.getNextId();
licenseCodeVal.ValidationGroup = this.purchaseEditSave.ValidationGroup; // save button within the panel
licenseCodeVal.ValidationExpression = #"^.{1," + licenseCode.MaxLength.ToString() + "}$";
licenseCodeVal.ErrorMessage = "Between 1 and " + licenseCode.MaxLength.ToString() + " chars.";
this.purchaseEdit.Controls.Add(licenseCodeVal);
As you can see both controls are added to the same container, the validator is added after the TextBox, and the ControlToValidate property is currently set to the ClientID of the TextBox. I have tried the ID value too but no joy.
The really irritating thing is that if I don't add the validator, and using a breakpoint inspect the ClientID of the TextBox, I can inspect the DOM of the updated page and find the ID attribute of the TextBox:
<input type="text" id="ctl00_ContentBody_PCTL::4890cc3a-8d07-4dee-aa9f-bdd8735fcaf8::licCode"... />
As far as I can tell I'm not doing anything wrong, so why am I getting this error??
Any suggestions much appreciated.
You should not use the ClientID but the ID of the TextBox.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.controltovalidate.aspx
"I have tried the ID value too but no joy."
But you didn't get an exception and only the validation didn't work?!
My suggestion is to wrap the control and the validator in an UserControl and add this dynamically to your container-control. On this way you can use fix ID's because they all have distinct NamingContainers.
The code will be cleaner. Encapsulation also increases reusability.
ARGH so I finally figured this thing out - it came down to special characters in the ID. I was having an issue with a JQuery selector which contained ':' and figured I should just check if that was causing the validator issues - it was.
In the end I created my GUIDs without any hyphens and replaced ':' with '_' and the problem went away. This question helped.
I haven't seen anything from MSDN saying certain characters are not permitted in IDs, but then I tend to refer to MSDN with specific problems after jumping in, so maybe there is something on there.. See comment below

how to set user control style attribute?

how can i set style attribute in code behind c#?
thanks
niall
Basically like this
Control.Style.Add("background-color", "red");
Or like this for any other attribute:
Control.Attributes.Add("style", "color: red;");
A user control isn't directly converted to a single HTML object - it is a collection of objects grouped together, therefore you can't set its "style".
If you want to hide it, then it should have a Visible attribute which you can set to false, however, this means that it won't be rendered to the page at all, and so subsequently can't be made visible in client code.

Resources