I am writing a website master page with a form for member login
Below is the markup from the master page:
<form id="loginForm" action="account.aspx" method="POST">
<div class="div-topHead">
<input type="button" id="submitLogin" title="Login" value="Login" />
<input type="button" id="forgetPW" title="Forget password" value="?" onclick="window.open('forgetpassword.aspx','_self')" />
</div>
<div class="div-topTail">
<div class="div-login">
<div class="row-body row-def">
<input id="input_memID" name="input_memID" runat="server" type="text" maxlength="255" />
</div>
<div class="row-header row-def">
Member ID:
</div>
</div>
<div class="div-login">
<div class="row-body row-def">
<input id="input_memPW" name="input_memPW" runat="server" type="password" maxlength="255" />
</div>
<div class="row-header row-def">
Password:
</div>
</div>
</div>
</form>
and below is the posting script
$(function(){
$('#submitLogin').click(function(){
var f = $('#loginForm').get(0);
$.post("../script/loginCheck.aspx", { "memID": f.inputID.value, "memPW": f.inputPW.value }, function(data){
var result = JSON.parse(data);
if(result[0] == 1){
//Login validated
f.submit();
} else {
//Login is invalid
alert(result[1]);
}
});
});
});
The problem is, when i open the website on browser,
the text input field names are automatically added a prefix "c100$"
and then from console I can see I got below error
Uncaught TypeError: Cannot read property 'value' of undefined
I have tried to lookup on the net what is this happening,
and seems it is something .NET automatically does.
Yet I could not find a way to make my post script works.
I don't need to keep the ID as it was without ct100,
as long as the posting script can work
I tried changing to f.ct100$input_memID etc but not working
Also tried the clientIDMode="Static" but can't get it work either
the browser complains clientIDMode is not an attribute of <% PAGE %>
So could someone please kindly advise what can I do?
Thanks in advance!
ClientIDMode is a property that can be added to individual controls.
In addition (from MSDN):
You can set the ClientIDMode value for all pages in a Web site by setting the pages element in the site's Web.config file. You can set the ClientIDMode value for a page in the # Page directive.
This for instance:
<div id="dvDiv" runat="server" clientidmode="Static" />
Will make the div's name be "dvDiv" in both client and server side.
So $("#dvDiv") will work fine on the client side.
About your last remark, make sure you put it in <%# Page %> and not in <% Page %> (notice the # sign)
Although Blachshma's answer is perfectly reasonable and will solve your problem, I recommend simply taking off the runat="server" attributes on your inputs.
I can't see that you have any real reason to have these exist as HtmlInputText objects if you don't need to reference them from page code-behind.
EDIT: as per comment, if you do need to access them from code-behind, then it's a better idea to simply change the ClientIDMode to Static.
You can simply use this jquery selector to get any element you want :-
$("[id$='yourElementID']")// Select an Element whose ID ends with yourElementID
Example :-
$("[id$='submitLogin']").click(function(){
var f = $("[id$='loginForm']").get(0);
....................
See The Detailed Information here
If the Javascript code is contained inside the aspx markup, you can also use
var f = $('#<%= loginForm.ClientID %>').get(0);
Related
I've got a traditional User Control, I have the following structure:
<asp:Content id="ContentBody" runat="server" ContentPlaceHolderId="MainContent">
<Core:AccountApplication id="uxAccountApplication" runat="server" />
</asp>
Inside the User Control I have a traditional:
<form id="AccountForm">
<input type="text" id="txtCompany" name="Company" />
<input type="submit" id="btnSubmit" />
</form>
The problem occurs when I call $('#AccountForm').serialize(); the request is constantly empty, nothing is serialized. If I do $('form').serialize(); it does serialize, but it also grabs all of the Web-Form ViewState rendering. Which requires some dubious approaches to correct.
Is there a better way to tackle?
Nested forms are not allowed in ASP.NET (see this question for example).
Your <form id="AccountForm"> element will be removed by ASP.NET (look at the generated HTML to confirm that).
After the answer received where it was denoted that the form element is removed, I verified and the entire form was indeed removed. So I did the following:
<div class="Application-Container">
<!-- All my form inputs -->
</div>
Wrapped them into a container, then I used jQuery to wrap the container into a form.
$('.Application-Container').wrap('<form id="AccountForm"></form>');
Now when you submit, it will rewrap into the form, then I can call $('#AccountForm').serialize() and it will correctly build my form data.
I have 2 controls in ascx file.
1) <input type="checkbox" id='<%= ECA_AdditionalOffers[i].Value["text"] %>_x'/>
2 )<asp:Literal Text='<%= ECA_AdditionalOffers[i].Value["text"] %>' runat="server" />
Everything is fine with checkbox. But literal control is not allowing me, to use '<%=' correctly. What can I do?
It just renders me this:
<div class="fleft for_dis" style="padding-top:2px;">
<input id="M:Vairuotojo ir keleivių draudimas <b>(+ 3 EUR)</b>_x" type="checkbox">
<%= ECA_AdditionalOffers[i].Value["text"] %>
</div>
Yes. You are right. Server side controls can't be touched with this method.
Actually I'm using an aspx custom radiobutton control like this
<input type="radio" id="declarableYes" value="Ja" name="declarable" class="form-radio" runat="server" required />
and i need to recieve the 'RenderedNameAttribute' which looks like this:
<input value="Ja" name="ctl00$ContentPlaceHolderMain$plcZones$lt$zoneMain$LegalData$declarable" type="radio" id="declarableYes" required="">
For exampel a asp.net 'System.Web.UI.HtmlControls.HtmlSelect' i can easely get the renderd Name attribute through the property controlXy.Name but for the RadioButton i can only access the "NonRendert" name which is in my case declarable.
I mentioned the Non-Public members where i can find the RenderedNameAttribute but i'm not abel to delegate to this property.
I need this value for javascript purposes.
I think you are looking for UniqueID property.
from MSDN:
This property differs from the ID property, in that the UniqueID
property includes the identifier for the server control's naming
container.
page.aspx
<form id="form1" runat="server">
<div>
<uc1:ucRadioButton runat="server" ID="ucRadioButton" />
</div>
</form>
ucRadioButtons.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ucRadioButton.ascx.cs" Inherits="ucRadioButton" %>
<input type="radio" id="declarableYes" value="Ja" name="declarable" class="form-radio" runat="server" required />
Which will result in
hth
When you're dealing with ASP.NET all your server controls get's a very wierd name indeed, but there are solutions where you don't need to have this special id.
In JavaScript, you can request the ClientID, of the control which would do what you want.
For example, when you have the following control:
<asp:TextBox id="txtName" runat="server" />
And in javascript, you want to alert this control, you could do something like:
<script type="text/javascript">
alert('<%= txtName.ClientID %>');
</script>
This should alert the control itself.
When you want a textbox named "txtName" have the name "txtName" even in your source, you could declare it like the following:
<asp:TextBox id="txtName" runat="server" ClientIDMode="Static" />
For more information about the ClientIDMode, please check the MSDN documentation.
ANSWER:
I wasn't able to find a buid-in solution to get this property. So i made a workaround for this by creating an extension for the HtmlInputRadioButton which takes the UniqeID and replaces the ID - ending with the name of the radio button.
Code:
public static string GetRenderedName(this System.Web.UI.HtmlControls.HtmlInputRadioButton radioButton)
{
return radioButton.UniqueID.Replace(radioButton.ID, radioButton.Name);
}
I need to add html tags dynamically in asp.net code behind , here is the scenario :
List<Product> products = ProductManager.GetProductList();//read data from database
Product is a class containing information I need to show in website such as product name, image,… .
For adding html code I tried this to show 5 products per page :
String finalHtmlCodeContainer="";
for(int i=0;i<=5;i++)
{
String myBox= "<div class=\"box\"> </div>";
finalHtmlCodeContainer+=mybox;
}
boxWrapper.InnerHtml=finalHtmlCodeContainer;
boxWrapper is a div that would contain our 5 product info.up to now everything is ok but problem appears when insted of "<div class=\"box\"> </div>",myBoxcontains long characters of html code , the original myBox should include this:
<div class="boxWrapper">
<div class="box">
<div class="rightHeader">rightHeader</div>
<div class="leftHeader">leftHeader</div>
<div class="article">
<img src="../Image/cinemaHeader.jpg" />
<p>
some text here <!--product.content-->
</p>
</div><!--end of article-->
<div class="rightFooter"><span>rightFooter</span>
<div class="info">
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
</div>
</div><!--end of rightFooter-->
<div class="leftFooter"><span>leftFooter</span>
</div><!--end of leftFooter-->
</div><!--end of box-->
</div><!--end of boxWrapper-->
you see if i add producet.atrributes to code snippet above, it would be more messy,hard to debug , has less scalability and... . what is your solution?
It depends on the underlying product. If you are using web forms, I'd suggest using a data bound control that supports templating like the ListView or Repeater. It gives you full control over the UI, and also supports reloading the UI in ViewState. In your scenario, you'd have to build the UI on every postback. The ListView or Repeater track it in ViewState.
If you are using MVC, well using a helper method can make this easier as you can stick the item template within and then use a foreach and render out the helper.
If you are talking doing it in JavaScript, then there are templating components for doing the same thing.
I suggest you to Add repeater in your <p> tag ,
<p>
<asp:Repeater runat="server" ID="rpDetail">
<ItemTemplate>
<div class="box">
<%# Eval("YourDataField") %>
</div>
</ItemTemplate>
</asp:Repeater>
</p>
And make your products list as Repeater's datasourse in code behind .
rpDetail.DataSource = products ;
rpDetail.DataBind();
I'm developing a website using EPiServer. I have a form which submits to itself.
On submit, I check if there are any fields missing. If yes, then error message is shown.
The problem is that my fields are reset when submitted.
I could check this using jQuery, but I'm not. I'm cheking this from code behind.
I've tried setting EnableViewState=true sevceral places, but no luck.
Here's part of my code:
<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
<asp:Panel ID="panelComplaint" runat="server">
<li>
<h3>Postnummer*</h3>
<input id="senderPostCode" name="postCode" type="text" size="20" enableviewstate="true" />
<asp:Label runat="server" id="lblPostCode" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<h3>Post sted*</h3>
<input id="senderCity" name="city" type="text" size="100" />
<asp:Label runat="server" id="lblCity" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<div class="spacer10px"></div>
<button type="submit" name="Send" >Send me</button>
</li>
</asp:Panel>
</asp:Content>
What do I need to do, in order to retain form fields?
Have you tried adding runat="server" to the input fields? As far as I'm aware, this is needed to ensure that asp.net round-trips data properly. I'm not sure if there's anything specific to EPiServer that would make that differ.
The enableviewstate attribute, unless it's specific to EPiServer, won't actually be doing anything.
If having the ID mashed up by ASP.net is a problem, consider upgrading to .NET 4.0, if you can, as this provides ways for you to control how the ID's are modified. If not, you could try placing a friendly translation into the page that you can then access via JQuery, for example:
<script language="javascript" type="text/javascript">
function getSenderPostCode() { return eval('<%=senderPostCode.ClientID%>'); }
var senderPostCodeField = JQuery('#getsenderPostCode');
alert(senderPostCodeField.length());
</script>
That said, having "getSenderPostCode()" to reference would mean that you could probably skip the step with JQuery of getting a reference to it, so the following would work:
alert(getSenderPostCode().length();
By adding runat="server" to your controls you then wouldn't need to use Request.Form["senderPostCode"].ToString() to access the content of that field, you could instead access it directly:
var contentOfSenderPostCode = senderPostCode.Value.ToString();