How to set the name attribute of an element in ASP.NET which is not equal to its ID attribute - asp.net

I have a code which I have to use HTML elements instead of ASP.NET elements. So because I want to have access to the elements from CodeBehind I've set all of their attributes to runat=server.
As you know ASP.NET changes the name attribute of elements to something like CT100$MainContent$IDNameOfThatElement. How can I prevent ASP.NET to change it? Cuz I have to have some other names because of JQuery stuff...
For example ASP.NET changes the name attribute from required to CT100$MainContent$PaymentCode in the following code:
<div class="field">
<input type="text" runat="server" name="required" id="PaymentCode" />
</div>
Thanx in advance.

You have to add ClientIDMode="Static"
Example:
<asp:Panel ID="myPanel" runat="server" ClientIDMode="Static">
</asp:Panel>

As far as I know you can't override the name attribute without jumping through some hoops. What you can do is create a controlAdapter that can control which attributes will be rendered and how.
Have a look at this answer for an idea about how to accomplish your goal.

Related

Include CSS Content page in Masterpage ASP.NET

why i cannot adding css Content Page in Master Page Asp.Net.
i cannot change..
<asp:TextBox ID="name" runat="server"></asp:TextBox>
then i use css
#name {
width:300px;
height: 300px;
}
but he did not change.
anyone can help me resolve this?
By default server object ID's aren't rendered exactly the same. You should either use a class instead or set clientidmode to static for the textbox.
If you view the source on the rendered page and look for that textbox, you'll find the ID is probably a few names separated by an underscore.
Here is the same markup with a static client id.
<asp:TextBox ID="name" runat="server" ClientIDMode="static"></asp:TextBox>
Please note that you shouldn't do this for any control that you plan to repeat on a page.
In that case you should use a class.
You have to use this CSS using Class
because When Server control render in page then Their ID is Unique that generate by IIS foloowd by contant page Holder Name
means
<asp:TextBox ID="name" runat="server"></asp:TextBox>
this will genrate
<input id="ContantPlaceholderName_name" name="ContantPlaceholderName_name"/>
so use class name is better option

Css validation do not work in asp net

i cant use css validation class inside asp.net controls but all other css design works correctly for example i just need to add class="validate[required]" to make a control validation in html but the same is not working for asp.net textbox controls i tried CssClass too didnt work.
please help, thanks in advance
<asp:TextBox ID="txt_uname" runat="server" Height="25" CssClass="validate[required]"></asp:TextBox>
validate[required]
This is not a valid css class name.
This declaration means validate class will apply to those elements, which have required attribute.
Your css class
.validate[required]
{
color:Red;
}
So try this
<asp:TextBox ID="txtUsername" runat="server" CssClass="validate" required="required"></asp:TextBox>

How do I put hint in a asp:textbox

How do I put a hint/placeholder inside a asp:TextBox? When I say a hint I mean some text which disappears when the user clicks on it. Is there a way to achieve the same using html / css?
The placeholder attribute
You're looking for the placeholder attribute. Use it like any other attribute inside your ASP.net control:
<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>
Don't bother about your IDE (i.e. Visual Studio) maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is. So the above code (basically) renders to:
<input type="text" placeholder="hint"/>
Using placeholder in resources
A fine way of applying the hint to the control is using resources. This way you may have localized hints. Let's say you have an index.aspx file, your App_LocalResources/index.aspx.resx file contains
<data name="WithHint.placeholder">
<value>hint</value>
</data>
and your control looks like
<asp:textbox id="txtWithHint" meta:resourcekey="WithHint" runat="server"/>
the rendered result will look the same as the one in the chapter above.
Add attribute in code behind
Like any other attribute you can add the placeholder to the AttributeCollection:
txtWithHint.Attributes.Add("placeholder", "hint");
Just write like this:
<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
<asp:TextBox runat="server" ID="txtPassword" placeholder="Password">
This will work you might some time feel that it is not working due to Intellisence not showing placeholder
Adding placeholder attributes from code-behind:
txtFilterTerm.Attributes.Add("placeholder", "Filter" + Filter.Name);
Or
txtFilterTerm.Attributes["placeholder"] = "Filter" + Filter.Name;
Adding placeholder attributes from aspx Page
<asp:TextBox type="text" runat="server" id="txtFilterTerm" placeholder="Filter" />
Or
<input type="text" id="txtFilterTerm" placeholder="Filter"/>
asp:TextBox ID="txtName" placeholder="any text here"

Add a class along with class specified in the skin

We are using themes to style our control.
For textbox we have added like this in the skin
<asp:TextBox runat="server" CssClass="txt edit_txt" />
Now in some cases, we want to add another class along with this 2 classes.
when we added a textbox like this in aspx
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" CssClass="RequiredCheckClass"></asp:TextBox>
On rendering it didn't take the "RequiredCheckClass" class, but only the other 2 classes specified in the skin.
So is there any way to add a class along with the classes specified in the skin from an aspx page.
PS : now i am going to use #Curt suggestion or will use EnableTheming=false and will add all the required classes to this control. Please update if anyone got any other idea other than these....
One option would be to create another TextBox control in the Skin file like:
<asp:TextBox SkinID="Required" runat="server" CssClass="txt edit_txt RequiredCheckClass" />
And then use the following in your markup file:
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" SkinID="Required"></asp:TextBox>
Other than that I'm not sure. I've always avoided ASP.NET Themes since I found out how restrictive they are.
You can use the "class" property - just like you use it in a regular HTML element.
thats the bypass that i am using.
<asp:TextBox SkinID="Required" runat="server" class="RequiredCheckClass" CssClass="txt edit_txt" />

'ImageButton' must be placed inside a form tag with runat=server

I've got the ImageButton wrapped inside a form tag with runat="server" but STILL getting this error at runtime. The form tag is at the very beginning (before my table) and end tag is at the end (after the table).
<td>
<div>
<div id="pay-Button"><asp:ImageButton ID="PayButton" ImageUrl="<%=PayButtonImageUrl %>" OnClick="RedirectTest" runat="server" /></div>
</div>
</td>
You can't have managed controls within a <form> tag without the runat='server'. ASP.Net supports multiple form tags, but with only one of them having a server-side designation. Also, I don't believe that HTML supports nested forms. You'll either want to have it be a non-managed control in a separate form or remove the nested <form> tag from the server-side form.
Look here for further explanation.
remove the ImageUrl="<%=PayButtonImageUrl %>" part, I don't believe you can use those inline snippits for server control properties (unless you're databinding)
Does your tag also have
runat="server"
as an attribute?
It would be helpful if you showed more of your code so we can help.

Resources