Declarative Markup: Expression Builder - c#

Is there a way to display the value of MyClass.SomeMessage in the expression builder <%$ $> of the label control below?
<asp:Label ID="lblMessage" runat="server"
Text="<%$ Is there a way to display the value of MyClass.SomeMessage here %>" />
public static class MyClass
{
public static string SomeMessage
{
get
{
string message = "This is some test message";
return message;
}
}
}

It there a real reason this is a serverside label?
<asp:Label ID="lblMessage" runat="server" Text="<%$ Is there a way to display the value of MyClass.SomeMessage here %>" />
If yes, then change <%$ to <%# use lblMessage.Databind() on the server side and you can see the value there.
Since it's a label, I would suggest using some html client side value and use "<%=MyClass.SomeMessage %>". You must not have the runat server though.

Related

How to set Label Text in .aspx markup using a static class in application?

Below is the static class within a solution,
namespace WebApplication1
{
public static class LabelValues
{
public static string GetLabelValue(string str)
{
Return "Hello" + str; //There's a complicated logic for this
}
}
}
Now I want set Label text using below code. but its not working
<asp:Label ID="_label1" runat="server" Text="<%# LabelValues.GetLabelValue("_label1") %>" >
I want to call static class from Text attribute of label control to set values for labels.
As VDWWD has mentioned you can write out the complete namespace. Also for data binding syntax I believe you need to wrap it in single quotes.
<asp:Label ID="_label1" runat="server" Text='<%# WebApplication1.LabelValues.GetLabelValue("_label1") %>' >
As seen here https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/bda9bbfx(v=vs.100)

Access userControl inside formView footer template from code behind

I have the following code:
<asp:FormView runat="server">
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<div>
<hr/>
<uc1:Footer runat="server" ID="Footer" />
</div>
</FooterTemplate>
</asp:FormView>
In Footer.ascx I have:
<dx:ASPxLabel ID="lbl" runat="server" Font-Italic="True" Font-Size="10px"></dx:ASPxLabel>
I want to access my user control FooterDetail from the code behind to set lbl value.
How can'I do this.
Thanks.
First you need to provide a property that returns the Label of the UserControl or better just it's Text. Then you can use the FormView's FooterRow property and FindControl to get it:
var uc = (UserControlTypeName)FormView1.FooterRow.FindControl("Footer");
uc.Value = "New Value";
Here's the property in your UserControl:
public string Value
{
get { return lbl.Text; }
set { lbl.Text = value; }
}

How to use Server Side variables on Markup

I want to use Session variable's value on the markup. This is the code written on UserContorl.ascx, file on NavigateUrl I want to send the Username which is stored in the session variable. I don't want to set NavigateUrl value on PageLoad function for some reason.
Note the code gives the error: The server tag is not well formed.
<asp:Panel ID="pnlMenuItems" runat="server" HorizontalAlign="Left">
<asp:HyperLink ID="LinkLogout" runat="server" NavigateUrl="~/logout/"+
<%= HttpContext.Current.Session["UserName"].ToString(); %>> CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
</asp:Panel>
You can bind data within server tags. e.g.
<asp:HyperLink ID="LinkLogout" runat="server"
NavigateUrl="<%# LogoutUrl %>"
CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
then in your code behind:
protected string LogoutUrl {
get {
return "~/logout/" + HttpContext.Current.Session["UserName"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) { DataBind(); }
}
The NavigateUrl gets set during the call to DataBind() using this method. In my example, the value would get set during page load, but you wouldn't have to specifically do it. If you need it to happen at a different time during the page lifecycle, you can try calling DataBind() during a different event.
How about:
<asp:Panel ID="pnlMenuItems" runat="server" HorizontalAlign="Left">
<asp:HyperLink ID="LinkLogout" runat="server"
NavigateUrl="<%# "~/logout/" + HttpContext.Current.Session["UserName"].ToString() %>"
CssClass="pnlMenuItems"
ForeColor="#666666">Logout</asp:HyperLink>
</asp:Panel>

javascript server tag not well formed

i wish to pass 2 value to invoke my script but i failed to do so it return me server tag not well formed
asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("movieTitle") %>' Width='500px'
CommandName="cmdLink" PostBackUrl='~/videotest.aspx'
OnClientClick="setSessionValue('video','<%#Eval("movieTitle") %>');"
Try like this:
OnClientClick='<%# string.Format("setSessionValue(\"video\", {0});", Eval("movieTitle")) %>'
Or even better ensure that you properly encode the movie title using the JavaScriptSerializer class:
OnClientClick='<%# string.Format("setSessionValue(\"video\", {0});", new JavaScriptSerializer().Serialize(Eval("movieTitle"))) %>'
Yeah, I agree, what a horrible mess are those WebForms. You would probably externalize this into a function in your code behind:
public string FormatJs(object movieTitle)
{
return string.Format(
"setSessionValue(\"video\", {0});",
new JavaScriptSerializer().Serialize(movieTitle)
);
}
and then:
OnClientClick='<%# FormatJs(Eval("movieTitle")) %>'

Binding Eval with an ImageURL in ASP.NET

I'm trying to bind an image using Eval() with VB.NET and ASP.NET, but am running into issues:
Code snippet
<bri:ThumbViewer Id="Th1" runat="server"
ImageUrl='<%# Eval("Name", "~/SiteImages/ram/3/{0}") %>'
Height="100px"
Width="100px"
/>
I set strImagePath in the code-behind as:
strImagePath ="~/SiteImages/ram/3/"
How can I replace:
~/SiteImages/ram/3/{0}
with the variable strImagePath?
simply use
<asp:Image id="abc" ImageUrl =<%# string.Format("~/SiteImages/ram/3/{0}",Eval("imagepath"))%>
imagepath could be from datatable or cs
I personally prefer to do these things in the codebehind directly like
<bri:ThumbViewer ID="thumbViewer" runat="server" ... />
and then in the codebehind you have some initialize or DataBind() method where you write
thumbViewer.ImageUrl= Path.Combine(ImagePath, Name); //or something similar, you have to check
This because especially when you develop in a team it is quite inconvenient and error-prone if people do some bindings in the ASPX code directly using Eval(...) and some in the codebehind. I prefer using the codebehind because then you immediately see what's going on on the page by just looking on your code, while your ASPx code is just for layout, definition of controls (with properties) etc...
string strImagePath = "aPage.aspx";
string pathFormat = "~/SiteImages/ram/3/{0}";
string path = String.Format(path, strImagePath);
Thats a little bit verbose, but you get the idea. What you're looking for is the String.Format method.
You can read more about it over at MSDN -> String.Format
So in your case that would be:
<bri:ThumbViewer Id="Th1" runat="server" ImageUrl='<%# Eval("Name", String.Format("~/SiteImages/ram/3/{0}", strImagePath)) %>' Height="100px" Width="100px"/>
as long as strImagePath is set to public or protected in your codebehind
Can you just write (and forgive me if this is wrong) if it is constant:
<bri:ThumbViewer ImageUrl='~/SiteImages/ram/3/<%# Eval("Name")%>' Height="100px" Width="100px" Id="Th1" runat="server"/>
And if it isn't:
<bri:ThumbViewr ImageUrl='<#Eval("ImagePath + Name") %>' ... />
//And in your codebehid:
public property ImagePath { get; set; }
...
ImagePath = "...";

Categories