ASP.net shorthand in TextBox - c#

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.

Related

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

unable to set value from server-side to a Textbox in client side

My code is like this
<asp:TextBox type="text" name="txtEndDate" Text="<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>" runat="server"></asp:TextBox>
Everything looks okay to me, But i don't know why its throwing an error
The server tag is not well formed.
Use single quotes like:
<asp:TextBox type="text" name="txtEndDate" Text='<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>' runat="server"></asp:TextBox>
The issue is that because of the ASP.net brackets which contain double quotes inside them
<asp:TextBox Text="<%# someMethod("someValue") %>" />
inside the Text field, you need to use single quotes instead of double quotes on that property, like this:
<asp:TextBox type="text" name="txtEndDate"
Text='<%# library.GetDictionaryItem("ProfilePages_CVR nummer")%>'
runat="server">
</asp:TextBox>
And it will work.
Note also that you are using the DataBinding notation ( <%# ) which will only work if your TextBox is inside a DataBound control, or you call DataBind on the control or page containing this TextBox.

How to reference code behind variable in ASPX?

I'm not sure why when referencing a code behind variable in an asp.net control, I get the text of the reference:
<%=this.Person.Contact.Emails[0].EmailAddress%>
This outputs the literal reference text:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%=this.Person.Contact.Emails[0].EmailAddress%>"></asp:TextBox>
This renders the variable value:
<input id="testfield" type="text" value="<%=this.Person.Contact.Emails[0].EmailAddress%>" />
Any ideas how I can get the variable value in the asp.net control?
You could say:
EmailAddress.Text = this.Person.Contact.Emails[0].EmailAddress
in your code behind
I prefer the solution in Code Behind in hunter's solution but another option would be to use data binding with #:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%# this.Person.Contact.Emails[0].EmailAddress%>" />
But then you have to bind the server control in code-behind:
EmailAdress.DataBind();
The = sign is like a call to Response.Write() at this place and just outputs whatever follows as text.

Can't put a inline tag inside of a html input tag

I have a input tag like:
<input src="..." title="<%= SomeResource.Label1 %>" />
It is not rending the text, it is rendering the actual value in the title attribute:
<%= SomeResource.SoeLabelInMyResourceFile%>
I tried single quotes and no change, what am i doing wrong?
It is suppose to render the title attribute as:
title="some text value stored in the resource file"
So the issue is the server tags are not being rendered, rather it is thinking that is plain text I want to display for the value of the title attribute.
UPDATE
The text renders just fine if I do this:
<td>
<%= SomeResource.Label1 %>
<input src="..." title="" />
</td>
But if I put the tags inside the title attribute, I get the error.
UPDATE
1 Create service folder App_GlobalResources (Project -> Add -> ASP.NET Folder)
2 Move resx-files to this folder
3 Get access to data:
In addition to programmatic access, ASP.NET 2.0 also introduces declarative syntax you can use to bind a named string to a property of a page or control. The syntax involves using the dollar sign ($) followed by the Resources namespace, the name of the resource file and the name of the string [see Resources and Localization in ASP.NET 2.0]
<asp:Literal runat="server" Text="<%$ Resources:Resource1, String1 %>" />
<input runat="server" type="text" value="<%$ Resources:Resource1, String1 %>" />
It works fine!
Try it:
<input src="..." title="<%$ Resources:SomeResource, Label1 %>" />
or
<input runat="server" src="..." title="<%$ Resources:SomeResource, Label1 %>" />
Can you make it work with an <asp:textbox> instead?
Try using GetLocalResourceObject()
<input src="..." title="<%= GetLocalResourceObject('SomeResource.Label1') %>" />
May be your input is marked as runat="server" ?
If you'll remove runat="server" title="<%= SomeResource.Label1.Text %>" gonna be work as expected.

Why can't I set the asp:Label Text property by calling a method in the aspx file?

Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}

Categories

Resources