Form fields not retained on postback - c#

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();

Related

RadAjaxLoadingPanel does not works with DNN

I have a dnn site, which has a label and an imagebutton, clicking on which replaces the label with textbox and user can enter their text, once submitted the label will be updated with this text. Now clicking on the imagebutton causes the page to postback, i don't want a postback for this, hence i have placed telerik RadAjaxLoadingPanel control, so the cool loading div gets displayed while processing is going on, but for some reason it's not working, It always throws below error:
Please, see whether wrapping the code block, generating the exception, within RadCodeBlock resolves the error.
Below is the markup of my page: (I tried the wrapping the code with RadScriptBlock and RadCodeBlock, in both case it throws same error as above)
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<a class="subscribetoday" href="#">
<strong>Subscribe Today!</strong> <asp:Label ID="lblsubscribemsg" runat="server" Text="12 issues for $14.95"></asp:Label>
<asp:ImageButton ID="imgEditSubscribe" runat="server"
OnClick="imgEditSubscribe_Click" ToolTip='Edit' ImageUrl="~/images/edit.gif" AlternateText='Edit' Visible="false" />
<div id="editsubscribe" runat="server" visible="false">
<asp:TextBox ID="txtSubscribe" runat="server"></asp:TextBox> <asp:ImageButton ID="imgSave" runat="server"
OnClick="imgSave_Click" OnClientClick="return validateSubscribeNote();" ToolTip='Save' ImageUrl="~/images/save.gif" AlternateText='Save' /> <asp:ImageButton ID="imgCancel" runat="server"
OnClick="imgCancel_Click" ToolTip='Cancel' ImageUrl="~/images/cancel.gif" AlternateText='Cancel' />
</div>
<img src="img/prosound-subscribe.png" alt="Subscribe Today!">
</a>
</telerik:RadScriptBlock>
</telerik:RadAjaxPanel>
Can anyone tell me where i am going wrong with this.
The problem is with other server code blocks on the page (<%=%> for example, generally - <% ... %>), not with this concrete piece of code you are trying to AJAX-enable. You can read more here: http://docs.telerik.com/devtools/aspnet-ajax/controls/ajax/radcodeblock-and-radscriptblock
So, you should find the place where those code blocks are used and wrap THEM in a RadCodeBlock controls. It is often scripts that reference controls, e.g.:
<telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
<script>
function getReference() {
return $find("<%=someControl.ClientID%>");
}
</script>
</telerik:RadCodeBlock>
With DNN, however, I cannot say where these may originate.
Thus, your other option is to use an <asp:UpdatePanel> control to get AJAX requests instead of full postbacks. The native AJAX toolset also offers the <asp:UpdateProgress> control that you can use instead of RadAjaxPanel.

Receive RenderedNameAttribute from HtmlInputRadioButton

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);
}

<form> tag causes Sharepoint error in aspx

So, I am working on .aspx application on Sharepoint, and I wanted to add form dialog. (question is not about how to make dialog, I did it). Problem is that when I use:
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<form id="Form1" runat="server">
<div id="dialog" style="display: none">
</div>
</form>
<asp:Button ID="Button1" runat="server" Text="Button" Style="display: none" OnClick="Button1_Click" />
I get error,
Sorry, something went wrong
An unexpected error has occurred.
Logs say nothing special.
but when i just remove form tags, even dialog work properly but I cannot use few things that I need. In that div I have aspx controls and to make them visible I want to put them into form.
sharepoint masterpages already contain a form tag.
either you remove the form tag or you stop using a master page

Output static property directly on .aspx page

I want to output something like this on an aspx page (not codebehind):
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
Where Fields.FirstName is a static class. How do I do this? I'm getting an error saying "The name 'Fields' does not exist in the current context". What am I missing? Do I have to include something on the .aspx page?
You could try this:
<input type="text" value="<%=Fields.FirstName %>" id="txt1" />
However, bear in mind that it is no longer a server control.
It is possible to use the <%# Fields.FirstName %> notation in server controls, however they will only be populated when you call DataBind from the code-behind. It is quite custom to use single quotes in the outer scope since double quotes are often needed in the inner scope, like here:
<input type="text" value='<%=Fields["FirstName"] %>' id="txt1" />
But if no quotes are needed it should also work as you described:
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
As long as you call txt1.DataBind() somewhere in the code behind.
See also this question for more info.
Use the full class name (including all nested namespaces) and an = sign, you are not databinding (denoted by the # sign). I commonly do this...
<%=Namespace.MyStrings.MyConstantString%>
Also, depending on how your page is setup, you might have to use single quotes areound the response write brackets....
<asp:TextBox ID="..." runat="server" Text='<%=Namespace.MyStrings.MyConstantString%>'></asp:TextBox>
UPDATE:
Super hacky, but I got it to work...
<supr:SuprTextBox ID="txt" runat="server" ClientIDMode="Static"></supr:SuprTextBox>
<div id="preload" style="display:none;"><%=Supr.Strings.ASSET_CONTROL_LOCATION%></div>
<script type="text/javascript">
$(function () {
$('#txt').val($('#preload').html());
});
</script>
Had to redeem myself after the <%= syntax not working.
You would need to do this in the code behind or in a code snippet on the aspx page. You cannot nest asp tags (<%# %>) cannot be nested in the asp:text element.

ASP.Net: ClientID not correct in code-behind of a user control

The following code does not work. The markup is in a User Control and I suppose that's why ClientID returns the wrong prefix for the TextBox id.
Markup:
<INPUT id="txtName" runat="server" maxlength="50" style="WIDTH:100px">
<INPUT type="button" value="Find Your Doctor" id="btnFind" runat="server"
style="MARGIN-LEFT:10px;WIDTH:130px">
Code-Behind:
btnFind.Attributes.Add("onClick",string.Format("DoctorLink
('{0}',document.getElementById('{1}').value,{2});",
row["ZipCode"],
txtName.ClientID));
Results in browser:
<input name="DoctorsMainArea1$ctl01$txtName" type="text"
id="DoctorsMainArea1_ctl01_txtName" maxlength="50" style="WIDTH:100px" />
<input name="DoctorsMainArea1$ctl01$btnFind" type="button"
id="DoctorsMainArea1_ctl01_btnFind" value="Find Your Doctor" style="MARGIN-
LEFT:10px;WIDTH:130px" onClick="PrepareDoctorLink('90210',
document.getElementById('DoctorsMainArea1_ctl00_txtName').value);" />
As you can see, the parameter for the JavaScript call is DoctorsMainArea1_ctl00_txtName, but the actual id of the input element is DoctorsMainArea1_ctl01_txtName.
Any idea how to fix this? jQuery? I am not so much interested in an explanation of what's going on (maybe there is another control on this page that is interfering), but a more robust way to solve the problem.
I don't know which asp.net version you are using but in 4.0 you can declare inside any server control ClientIDMode="static" and it will give you the exact id in browser.
Example:
<asp:Textbox id="txtName" runat="server" ClientIdMode="static"/>
Others are predictable, inherit and it can be used with ClientIdRowsuffix.Can be used at page level and even on master pages and even in web.config file.
Example on web.config file:
<system.web>
<Pages clientIDMode="predictable"/>
other system web properties
</system.web>
Watched Craig shoemaker's Video at tekpub, you can also read more about it at Rick's bloglink text. It's pretty cool tho.
You should try moving the code that adds the onclick attribute to the button in the PreRender event (or OnPreRender override) in your page or user-control. That should probably get the ClientID right.
A fast solution:
btnFind.Attributes.Add("onClick",string.Format("DoctorLink
('{0}',document.getElementById('{1}').value,{2});",
row["ZipCode"],
"DoctorsMainArea1_ctl01_" + txtName.ClientID));
This happens because you have a content placeholder in your page somewhere.
another solution:
html tag:
<input type="text" name="txtName" id="txtName" />
code-bind:
string txtName_value = Request.Forms["txtName"];
and you can get the value
just use the html control.

Categories

Resources