How to set a Int property of a control on ASCX? - c#

I have an ASCX that contains
<my:Foo ID="Bar" runat="server" Value='' />
I want to set Value with textbox1.Text, but Value is an Int32. I am looking for something like this:
<my:Foo ID="Bar" runat="server" Value='<%= Int32.Parse(textbox1.Text) %>' />
But I get
Parser Error Message: Cannot create an object of type 'System.Int32' from its string representation '<%= Int32.Parse(textbox1.Text) %>' for the 'Value' property.
Is there a way to do this on the ASCX file? Do I have to implement a TypeConverter for this property?

I don't understand why you can't simply use the literal instead of a string representation:
<my:Foo ID="Bar" runat="server" Value="58" />
If you want to set this value dynamically, you will need to do so in the code behind or within a code block, for example in the page load event handle, as you cannot use code blocks (<%%>) within a server side control:
// code behind, in the page_load event handler
Bar.Value = 58;
Or, within the ascx, outside of server side controls:
<% Bar.Value = 58; %>

Change it to
<my:Foo ID="Bar" runat="server" Value="58" />
The ASP.Net parser will automatically parse integer properties.
<%= ... %> expressions aren't supported for server-side controls, so your code causes ASP.Net to try (and fail) to parse the literal string <%= Int32.Parse("58") %> as an integer.

Related

How to assigning dynamic value to CssClass in .aspx page not working

I am using server side HyperLink control on webform and want to assign dynamic CSS Value to it.
HyperLink control is inside repeater control
It fails if i use it like this CssClass='search-<%#Eval("CSS") %>' and if i do it like this then it works CssClass='<%#Eval("CSS") %>'.
Issue i have is that i have to concatenate search- to field value <%#Eval("CSS") %>
How can i define so that value is assigned to it
in HTML source it shows up like this class="search-<%#Eval("CSS") %>"
SOLVED it by doing in this manner
CssClass='<%# Eval("CSS") +"-type-search "%>'
You need to include whatever text you need to inside <%# %> tag enclosed with ' ' or "" depends on what you used at start of your markup
class='<%# "search-" & Eval("CSS") %>'

Label/Literal control with formatted text field

I have discovered that it is possible to populate resource strings with variable information using string.format, see below:
String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales)
I can display this on my page using:
<p><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></p>
In some cases I have a Label or Literal (or any control) which might dynamically hide or show content. Ordinarily I would populate those using:
<asp:Literal ID="Literal1" Text="<%$ Resources:Temp,ContactUs %>" runat="server" />
I now would like the same String.Format functionality whilst still using controls. I found Display value of Resource without Label or Literal control but this doesn't actually work for me, it just writes out '<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>' on the page (not the content, that actual string).
UPDATE:
I have found a solution which works with some controls:
<asp:Label runat="server" ID="temp"><%= String.Format(Resources.Temp.TempString, Resources.Contact.PhoneSales) %></asp:Label>
However, this works doesn't work for Literal controls as they don't allow child controls. I'd prefer to keep using Literal's as they are the cleanest in terms of code generated, so still seeking a solution.
asp:Literal doesn't support <%= %> construct, and doesn't allow child controls (I mean something like <asp:Literal runat="server"><%= ... %></asp:Literal>).
But if you use data binding, your could use data-binding expresions <%# ... %>:
<asp:Label runat="server" Text="<%# string.Format(...) %>"></asp:Label>
To make this work you should ensure that either implicit or explicit data binding for your controls is used. Otherwise the control like this without binding outputs nothing.
This workaround is a little bit complex. Consider using either asp:Label control, or set the Text property from the code behind.
To solve my problem I have actually had a second look at how I am displaying content and found that a lot of times the Literals and Labels could be dropped in place of plain HTML code. I can then use my preferred method <%= ... %> to display content.
You could use an asp:PlaceHolder control instead of a Literal.
PlaceHolders can contain child controls; they also support <%= … %>-style "displaying expressions".

Set literal text with inline code in aspx file

In an ASP.NET project, I have a literal. In order to set the text property, I used the following code:
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
But instead of value of session.UserName, literal shows <%= session.UserName %>. I feel that solution is simple but I couldn't do it. How can set the text with inline code?
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 this.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
<asp:Literal ID="ltUserName" runat="server" Text='<%# Session["UserName"]%>'></asp:Literal>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserName"] = "Sample";
this.DataBind();
}
}
If you actually want to print the session value in the HTML page just use
<% =Session["UserName"].ToString()%> as "<% %> will act as server tag and you cant give it inside the literal control
I mean no need of Literal Control can just use mentioned coding instead of literal.
Did you tried:
Text='<%= session.UserName %>'
Single quotes may solve it
EDIT:
Based on this thread: stackoverflow.com/a/370263/360171
I would simply replace
<asp:Literal ID="ltUserName" runat="server" Text="<%= session.UserName %>" />
by
<%= session.UserName %>
You can't mix controls (<asp:Literal />) with code blocks (<%= .. %>).
You can access the Text property from within code:
ltUserName.Text = session.UserName;
Renatos answer is correct, you should put single quotes when you are going to evaluate an expression in a property.
The same can be said with a ItemTemplate, where you have controls to databind, where you would use Text='<%=Eval("MyDataProperty")%>'.

c# code in control attribute in aspx file

I'd like to change the text attribute of a button according to the session landuage. So I write:
<asp:Button id="btOptIn" runat="server"
Text="<% = UI.Instance.TxtSubmit %>"
onclick="btOptIn_Click" />
This does not work. I tried several other versions (like Text='<%# Eval(UI.Instance.TxtSubmit) %>') but could not succeed.
The same code (<% = UI.Instance.TxtSubmit %>) works outside the quotes of the attribute. What is the syntax to make it work within an attribute of a control?
Thank you for your time.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ > can be used to access data in resources files.
In the Page_Load of you will have to make a call to Page.DataBind() for this to work.

Use c# expression to define server tag property value

Is it possible to set the property of a server tag from a c# expression, i.e. something like
<asp:TextBox Width='<%= [some c# expression] %>'/>
?
I though this would be pretty straightforward, but I can't get such an expression to run.
Thanks for any help
Ryan
Yes, this is possible. You need to make sure the control runs server side (runat="server"), but depends on exactly what you are trying to evaluate in the expression.
So long as the expression returns a string, it should be fine.
<asp:TextBox id="txt" runat="server" Width='<%= (10 * 10).ToString() %>px'/>
This will result in a width='100' in the browser.
Update:
The above is completely wrong. You cannot put server side code render blocks (<%%> and <%=%>) in a server side control markup in this manner (since it already is a run server side).
In order to dynamically control the value, this needs to be done either in codebehind or within separate render blocks:
<%
txt.Width = (10 * 10).ToString() + "px";
%>
<asp:TextBox id="txt" runat="server" />
See this and this for reference.
Beware of the type you bind to .
for instance
<asp:TextBox runat=server ID=C_TB_MyTB
Text=<%# MyText %>
Width=<%# MyWidth %>
></asp:TextBox>
Binding this textbox with
protected string MyWidth="300";
protected string MyText = "Bla bla bla...";
won't work , whereas :
protected int MyWidth=300;
protected string MyText = "Bla bla bla...";
will do...
good code to you,

Categories