Set literal text with inline code in aspx file - c#

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")%>'.

Related

Add asp.net LinkButton to a Literal using c#

I want to add some asp.net linkbutton controls to a literal on a c# web form application like code below:
StringBuilder sb = new StringBuilder("<ul>");
sb.Append("<li>");
sb.AppendFormat("<asp:LinkButton runat='server' class='add-row' ID='BtnAdd' OnClick='BtnAdd_Click' CommandArgument='test'>{0}</asp:LinkButton>", "Text on the link");
sb.Append("</li></ul>");
this.Literal.Text = sb.ToString();`
and also i want to fire event of click and get CommandArgument's value of that.
private void BtnAdd_Click(object sender, EventArgs e)
{
//get the value of CommandArgument
}
I tried these and the linkbutton added to the literal successfully. but the linkbutton didn't convert to <a> tag and i found that with the firebug like this and no any events couldn't fire:
I want make a tree with leafs as linkbuttons using <ul><li> tags overall.
It would be very helpful if someone could explain solution for this problem.
The correct approach here is via the WebControl api and not from the template.
Your approach may work for HTML content only.
What you need to do is handle a page lifecycle event like OnInit, get a reference to your container control (this.MyPanel), and dynamically add items to that control's children property (this.MyPanel.Children.Add(new....))
The suggested Repeater solution is not suited for tree structure data sources, works for a list though...
As suggested by Andrei using linkbuttons in this way will not work.
I have used a similar solution in the past by creating a query parameter for each a tag in the literal and redirecting to either the current page or a different page and extracting the query parameter from there.
I would however advise you to first try and see if there is not a different way to accomplish what you are trying to do as the literal solution ends up very cluttered and code heavy.
I think a Repeater would better suit your purpose.
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li><asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnClick="BtnAdd_Click">Text on the link</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
And if you want to use CommandArgument you need an OnCommand, not a OnClick
<asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnCommand="BtnAdd_Command" CommandArgument='<%# Eval("value") %>'>
protected void BtnAdd_Command(object sender, CommandEventArgs e)
{
string commandArgument = e.CommandArgument.ToString();
}
If you have single <asp:LinkButton ..> then you can set it's property at run time. Like
BtnAdd.Id="";
or if you multiple then you can use AddControl method below is link
How to add controls dynamically when click button in asp.net?
How to add controls dynamically to ASP.NET form?
If you have to use asp: LinkButton just for one time then do it in this way.
<asp:Literal runat="server" ID="DocLink"></asp:Literal>
<asp:LinkButton runat="server" ID="LinkButton1" OnCommand='LinkButton1_Click'></asp:LinkButton>
and in .cs file
LinkButton1.Text = CurrentListItemAbsoluteUrl;
LinkButton1.CommandArgument = CurrentListItemAbsoluteUrl;
public void LinkButton1_Click(Object sender, CommandEventArgs e)
{
e. CommandEventArgs //To get your parameter

Added tag HyperLink in aspx page without GridView in c#

On MarkUp in my aspx page form I have these two TextBox :
<asp:TextBox ID="Mtl" runat="server" ReadOnly="true" Enabled="false"></asp:TextBox>
<asp:TextBox ID="ps" runat="server"></asp:TextBox>
The HTML view for these two TextBox is :
<input name="Mtl" type="text" value="901" readonly="readonly" id="Mtl" disabled="disabled" />
<input name="ps" type="text" id="ps" />
Now I need insert next to the TextBox with id ps the HyperLink where passed in querystring the value of TextBox with id Mtl, the value is 901.
I need pass this value for working in another aspx page.
I have tried this solution but the HyperLink is not clikable :
<asp:HyperLink ID="HlLink" runat="server"
NavigateUrl='<%# String.Format("~/box.aspx?v={0}&e={1}&l={2}", "y", "IC", HttpUtility.UrlEncode(Eval("Mtl").ToString())) %>'
ImageUrl="~/Images/edit_icon.gif" Target="_blank" Text="Mtl"></asp:HyperLink>
In this aspx page I don't have GridView, maybe it does not work for this reason ?
How to do resolve this ?
Please help me, thank you so much in advance.
Yes you are correct since your control is not inside a gridview (or any databoundcontrol for that matter) that's why it will not work.
Actually, <%# %> is called data bind expressions and they are evaluated for data bound controls only. For your HyperLink control to work with this code nugget you will have to explicitly call the DataBind method on that control like this:-
protected void Page_Load(object sender, EventArgs e)
{
HlLink.DataBind();
}
You can bind a jQuery 'Change' event on your textbox. And in this event you can set correct navigation url by picking up the value from the textbox and appending it in appropriate place in your query string. Its a fairly easy solution. If you don't know how to do it I can provide you a sample.

Calling function in usercontrol inline code doesn't always work

I have constructed an ASP.NET user control "Box.ascx" wtih the following code.
<div id="divContent" runat="server" visible='<%# AllowedToView(this.Privacy) %>'>
Content
</div>
In the codebehind, "Box.ascx.cs" has the following code.
public string Privacy = string.Empty;
public bool AllowedToView(string privacy)
{
return true;
}
When I use this control in a repeater, AllowedToView() function is hit. If I use this control without a repeater, AllowedToView() function isn't called. I want to know why this weird situation happens and how can I make the control call the AllowedToView() function when used without repeater.
Details are below.
I use this control in a repeater in "Default.aspx".
<asp:Repeater ID="rpRecords" runat="server">
<ItemTemplate>
<uc1:Box ID="myBox" runat="server" RecordID = '<%# Eval("RecordID") %>' />
</ItemTemplate>
</asp:Repeater>
The repeater is databound in "Default.aspx.cs" with the following code:
DataTable dt = FillTable();
rpRecords.DataSource = dt;
rpRecords.DataBind();
I use the "Box.ascx" control in "ShowBox.aspx" with the following code.
<body>
<uc1:Box ID="myBox" runat="server" />
</body>
I give values to the user control from the codebehind with the following code.
protected void Page_Load(object sender, EventArgs e)
{
myBox.RecordID = "1";
}
As mentioned in another answer, the # means it will require databinding to be executed.
So to answer your question "How to make it run outside of the repeater" the simple answer is to call myBox.DataBind().
Your question is very similar to asp.net inline code <%# MyboolVal %>. The problem is that <%= is equal to Response.Write and outputs straight HTML, so it won't work when setting the visible property.
I don't think you need the # but instead = in the ASP tag. Pretty sure # is only for databinding events and that's why it works in a repeater because a repeater performs a databound for rendering.
Check this link: http://blogs.msdn.com/b/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx
Im no expert on webforms but i think that your problem is that you are trying to databind that method and thats not working for you, try putting it in a <%= AllowedToView(this.Privacy) %>

Asp.net grdiview: can i format the dataitems in an itemtemplate?

i have this code in an itemtemplate in a gridview:
<%# DataBinder.Eval (Container.DataItem, "DiscountAmount")%>
It's a decimal value, and it shows 20.300000000000, which is technically right, but i'd prefer to show 20.30 or 20,30, depending on the culture.
But i've never been a big fan of gridviews, and the DataBinder.Eval and Container.DataItem haven't been good friends either, and i'm lost with how to use it.
it has a special prefix (<%#) and when i type anything other then the original code it's no good, but changing <%# to <%= or <% doesn't seem to work either?
This will also work:
<%#= String.Format("{0, 0:N2}",DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
Edit: I share your discomfort with declarative databinding syntax. You can accomplish the same thing in code-behind by calling the RowDataBound event and implementing whatever changes you want to make as the data is bound to the GridViewRow.
To do this, you need to wire up the event in the markup by setting OnRowDataBound to the name of your event handler, something like this:
<asp:GridView ID="InvoiceGrid" OnRowDataBound="InvoiceGrid_RowDataBound".....>
Then you create an event handler in code behind with a signature like this:
protected void InvoiceGrid_RowDataBound(object sender, GridViewRowEventArgs e)
The first thing you do in that event handler is test which type of GridViewRow type it is:
if (e.Row.RowType == DataControlRowType.DataRow)....
Then you do whatever formatting you want to do.
For folks happy with declarative markup, this may seem burdensome. But for people who are comfortable writing code, you can do a whole lot more here in code behind.
Did you try this?
<%= String.Format("{0:0,0.00}", DataBinder.Eval (Container.DataItem, "DiscountAmount"))%>
or just
<%# DataBinder.Eval(Container.DataItem, "DiscountAmount", "{0:0,0.00}")
You can read more format options in the article String Format for Double.
There are several ways...some of them stated above and here is another one:
Text='<%# GetFormattedDiscount(Eval("DiscountAmount").ToString())%>'
GetFormattedDiscout is a function in your code-behind where you can do whatever formatting you need and return it as string:
protected void GetFormattedDiscount(string amount){
return String.Format("{0:N2}",amount);
}
Even this should work:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#String.Format("{0:n2}",Eval("DiscountAmount")) %>'></asp:Label>
</ItemTemplate>

How to set a Int property of a control on ASCX?

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.

Categories