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);
}
Related
I have some user controls in my .NET web application, I'm using them in the same page. They have some properties and for some reason I need some hidden fields that hold the values of the properties.
So in one of the user controls there's this piece of code:
<input type="hidden" data-versus="PL" value="<%= vs_pl %>" />
Where vs_pl is the property of the control:
private decimal? _vs_pl; // plan
public decimal? vs_pl {
get { return _vs_pl; }
set { _vs_pl = value; }
}
All this is rendered correctly as this:
<input type="hidden" data-versus="PL" value="-190.2">
In the other user control I have a similar piece of code:
<asp:HiddenField ID="hfOrg" runat="server" Value='<%= org %>' />
Where org is a property similar to the above one. But this is rendered as:
<input type="hidden" name="ctl00$cs$hfOrg" id="cs_hfOrg" value="<%= org %>">
In the very same page. None of the two controls have data binding or data controls internally and they are not bound to a data source in the page either.
I realize that the first case is not a server control, just a normal HTML input tag, while the latter is rendered by the server. However I find this a strange behavior and I'd expect it to work in the second case too.
Where am I wrong?
<%= %> Is a shortcut for Response.Write and it will only work in plain HTML like here
<input type="hidden" data-versus="PL" value="<%= vs_pl %>" />
You should use DataBindings in server controls like this
<input type="hidden" name="ctl00$cs$hfOrg" id="cs_hfOrg" value="<%# org %>">
It's important to remember that if you use DataBindings you should call the UserControl's DataBind method directly or indirectly but calling the DataBind method of a parent control or the Page itself.
Reference:
https://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
https://msdn.microsoft.com/en-us/library/bda9bbfx(v=vs.71).aspx
When you are trying to bind data in a Control you need another syntax.
<asp:HiddenField ID="hfOrg" runat="server" Value='<%# org %>' />
And if that control is outside a Repeater/GridView etc you need to call DataBind() in Page_Load.
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
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.
I am trying to do the following:
<asp:TextBox ID="txtName" runat="server" Text="<%= Name %>" />
When I execute my page it gets output as <%= Name %> instead of actually doing a response.write.
I tried modifying it to use the <% Response.Write(Name) %> instead but it did the same thing, putting the text there instead.
I can do this just fine:
<input type="text" value="<%= Name %>" />
That will actually work. Why doesn't this work when I use the TextBox control? Is there another way I'm supposed to do this?
Either use code behind:
txtName.Text = Name;
Or, add Page.DataBind() in your code behind and change the syntax of your control to:
<asp:TextBox ID="txtName" runat="server" Text="<%# Name %>" />
Note the # rather than the =. # represents a data-binding expression
Because the control is rendered differently than a literal. Use the codebehind to set the Text property.
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.
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();