'ImageButton' must be placed inside a form tag with runat=server - asp.net

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.

Related

Add to an aspx button an css id

Having the code below for a button, can I add an id to it so I can add CSS to the button?? Or I can put only a class??
<asp:Button ID="registerLink" runat="server" Text="Create Account">
</asp:Button>
In WebForms, the ID="" attribute of controls is transformed into something of the form ctl0__ctl1__registerLink (where ctl0 and ctl1 are the ID="" values of parent controls). This means the rendered id="" attribute is (generally) unpredictable and cannot be relied upon for styling or Javascript uses.
There are three possible solutions:
Use ctrl.ClientId to get the final rendered id="" attribute value, this works when you want to reference the rendered HTML from a client script on the same page, however it isn't of much use for styling unless it's an inline <style> element.
Use the clientIDMode setting to override how the id="" attribute is rendered. This requires ASP.NET 4.0 or later. You can set this in web.config, in your <%# Page declaration, or on each element. Set it to Static so the value is verbatim (with exceptions).
Implement your own Control Adapters that override how attributes render.
Ditch WebForms and use ASP.NET MVC ;)

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

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.

style tag automatically added to ASP.NET ListBox control

I have an ASP.NET project with the following line of code:
<asp:ListBox ID="lbxInvoices" runat="server" SelectionMode="multiple" Rows="16"></asp:ListBox>
Obviously, I'd like the control to render with 16 rows. Unfortunately, it refuses to do so...when I check the HTML source for the page, I see the following:
<select size="16" name="ctl00$cphMainContent$Wizard1$lbxInvoices" multiple="multiple" id="ctl00_cphMainContent_Wizard1_lbxInvoices" class="DefaultListBox" style="height:100px;width:250px;">
Where did the class attribute come from? And more importantly, where did the style attribute (which is apparently overriding the "size" attribute) come from? How can I get rid of them?
If I add a style="height: auto" attribute to my server side tag, I can get this to work, but that seems weird, and I'd like to just stop it from adding this attribute in the first place.
Am I missing something?

ASP.NET - using explicit end tags or not?

Is there any difference between
<asp:TextBox runat="server" .... />
and
<asp:TextBox runat="server" .... ></asp:TextBox>
Are there any gotchas using one over the other?
No, I prefer the first because it is cleaner and the closing tag serves no purpose. The only time you need a closing tag rather than using a self-closing element is when you need to out other elements inside the element itself.
Otherwise I always use a self-closing tag for cleanliness and simplicity's sake.
I have an exception to #Andrew's rule:
I've been using GridView in an ASP.Net form and binding a recordset to it dynamically for debug purposes (I didn't specify anything for it but an ID).
When you drag the GridView to your form, it creates an <asp:GridView></asp:GridView> code. I found out that if I drop the closing tag and close the opener with />, the GridView does not get displayed.
On the same note, check my question from last week, where people correctly commented that in HTML/XHTML, some tags (like DIV) must have a closing tag.
The only tag I've ever seen this make a difference on is a non-ASP.Net related tag, which is <iframe>.
I like doing the /> instead of a full end tag. It just seems cleaner.

Multi language: asp:label aganist html:label with asp:Literal

I'm adding multi language support to a prototype web site. The site was developed using html lables which I could multilanguage using asp:literal or I could change them all to asp:labels as shown below.
<asp:label ID="lblAddress1" runat="server" Text='<%$ Resources:lblAddress1 %>' /></br>
<label><asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:lblAddress1 %>"></asp:Literal></label>
Web stuff isn't my area of expertise and the guys here don't think there is any advantage one way or the other. What would you choose and why?
<asp:Literal>
Use this control as a placeholder for any text you wish to insert in the page. The output will not be wrapped in any html markup tags (simplest).
<asp:Label>
Use this control in the same way as the , however, This control will wrap the text in html tags. These span tags allow the control to have additional properties (css styling etc.) which can be leveraged.
<label>
This html tag has semantic value in a page and is used to associate form elements with their description.
<label for="SaveLoginName">Remember Me:</label>
<input type="checkbox" id="SaveLoginName" />
A browser can use this info to provide additional accessibility features such as enabling clicking text to toggle checkbox value.
Each of these have appropriate usage scenarios.
Seems to be a matter of taste. Although I think the second option may add a little weight to the page because literals are usually wrapped in <span>

Resources