asp.net data binding in literal - asp.net

Lets say we have the following in the default.aspx file
<asp:Literal runat="server" Text="<%= TestMethod() %>" />
What needs to be defined in the default.aspx.cs file to make this work?
I tried to add a method called TestMethod to the _Default class which simply returned the string Test, but it didn't seem to work.
Can anyone help?
Thanks,
AJ

Apart from the method being marked public...
i think you could also remove the asp:Literal altogether
example
your code
<p><asp:Literal runat="server" Text="<%= TestMethod() %>" /></p>
could be
<p><%= TestMethod() %></p>
However if you are intent on using the Literal then please rather set it on page load.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
Regards.

I think you can get the same result by doing this
In your .aspx file
<asp:Literal runat="server" ID="ltr1" />
And in your aspx.cs file
ltr1.text = TestMethod();

Related

use variable in mailto hyperlink in asp.net

I want to use a variable declared in my .cs page(C#) as cc to mailto, but dont know how to use. Please help.
In default.aspx.cs page:
var code=cc#test.com;
In default.aspx page:
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc=code" ToolTip="Submit" Target="_blank">mail</asp:HyperLink>
This is not a good way to do this but you can set the NavigateUrl from code behind.
Try this:
HyperLink5.NavigateUrl="mailto:to#test.com?subject=Hi&cc="+code;
Have you tried embeding code like
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc="<%=code%> ToolTip="Submit" Target="_blank">mail</asp:HyperLink>

ASP.NET Repeater question

I have a repeater control and under the ItemTemplate, I have Image control. Anyway the old
How can I set the ImageUrl programatically?
Anyway, the old html code I have was like this:
<ItemTemplate>
<img src="<%# Eval("ImageSource") %>" alt="" />
</ItemTemplate>
But I want to check if the image exists in directory or not then I can setup with temp image.
I have a code but .. it's not really working so there's no sense of showing it here. Can you guys help me? Should I use ItemCreated or ItemDataBound event?
In the xml side in the template, you need to call a method directly.
<asp:Image runat="server" ID="myImg" ImageUrl='<%# MyImageUrlFunction(Eval("DataFieldName").ToString()); %>' />
You need a corresponding method in the code behind defined publicly:
public string MyImageUrlFunction(string field)
{
// put some logic here to determine url
return imageUrl;
}
In your ItemDataBound, do something like:
protected void rpt_ItemDataBound(object sender, RepeaterEventArgs e)
{
HtmlImage img = (HtmlImage)e.Item.FindControl("img");
string imageUrl = (string)DataBinder.Eval(e.Item.DataItem, "ImageSource");
if (File.Exists(imageUrl))
img.Src = imageUrl;
}
That's System.Web.UI.HtmlControls.HtmlImage, System.Web.UI.DataBinder and System.IO.File.
ItemDataBound. You can get the control reference through the current item's findcontrol event, and then check to see that the image exists. You can get the file path using Server.MapPath("~/images/test.png"), and then if it doesn't, inject your own.
You can also use a public method that the client-side markup can call, pass in the URL, and provide a default if it doesn't exist.
HTH.
<ItemTemplate>
<asp:Image ImageUrl='<%# System.IO.File.Exists(Eval("ImageSourceProperty").ToString()) ? Eval("ImageSourceProperty").ToString() : TemporaryImagePath %>' runat="server" />
</ItemTemplate>
for the Error
The server tag is not well formed
You should remove extra space in your code!
<%# System.IO.File......%>
should be <%#System.IO.File......%>

Asp.net, directly assigning properties from value?

Is there a way to express
<% objControl.ObjProp=ObjVar; %>
<my:Control ID="objControl" runat="server" />
As something like this, in one line? And without passing ObjVar as a string?
<my:Control ID="objControl" runat="server" ObjProp=ObjVar />
Unless you're in a databound context, no there's no simple way to do this. If it is a databound context (Like in a repeater/gridview) you can simply go ObjProp='<%# ObjVar %>', but outside that context you can't do it inline unfortunately.
use it like
<my:Control ID="objControl" runat="server" ObjProp="<%# ObjVar %>" />
As fyjham has mentioned, you need to do this in a databound context with the <%# %> syntax. If you are trying to set the property dynamically then your other option is to set it within the server side parent's onload code behind method.
What is ObjVar? If it's a static value, you can just add the attribute tag to the control element like so....
<my:Control ID="objControl" runat="server" MyCustomBooleanProperty="true" />
If it's a member variable of the page containing the control, then I'd do so in the code behind...
protected Page_Init()
{
this.objControl.ObjProp = this.ObjVar;
}
If you're databinding to the control, then the others are correct where you use the databinding context.
<my:Control ID="objControl" runat="server" ObjProp=<%#Eval("ObjVar")%> />

Can I use <%= ... %> to set a control property in ASP.NET?

<asp:TextBox ID="tbName" CssClass="formField" MaxLength="<%=Constants.MaxCharacterLengthOfGameName %>" runat="server"></asp:TextBox>
The code above does not work. I can set the MaxLength property of the textbox in the code behind but i rather not. Is there away I can set the MaxLength property in the front-end code as above?
You could use DataBinding:
<asp:TextBox
ID="tbName"
CssClass="formField"
MaxLength="<%# Constants.MaxCharacterLengthOfGameName %>"
runat="server">
</asp:TextBox>
and in your code behind Page_Load call:
tbName.DataBind();
or directly databind the page:
this.DataBind();
The <%= expression %> syntax is translated into Response.Write(expression), injecting the value of expression into the page's rendered output. Because <%= expression %> is translated into (essentially) a Response.Write these statements cannot be used to set the values of Web control properties. In other words, you cannot have markup like the following:
<asp:Label runat="server" id="CurrentTime" Text="<%= DateTime.Now.ToString() %>" />
Source: https://web.archive.org/web/20210513211719/http://aspnet.4guysfromrolla.com/articles/022509-1.aspx
Try to use custom expression builder:
// from http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
[ExpressionPrefix("Code")]
public class CodeExpressionBuilder : System.Web.Compilation.ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
object parsedData, ExpressionBuilderContext context)
{
return new CodeSnippetExpression(entry.Expression);
}
}
And then use it like
<asp:TextBox ID="tbName" CssClass="formField" MaxLength="<%$ Code: Constants.MaxCharacterLengthOfGameName %>" runat="server"></asp:TextBox>
As Ropstah said, it isn't going to work with the <%= expression %> syntax.
But you could probably use databinding, which just requires that you use the <%# expression %> syntax and then call MyTextBox.Databind in CodeBehind.
Of course, at that point it might be more clear to just do the whole operation in CodeBehind.
Another alternative: if you really want this to be declarative, you could get away from the Label and embed your expression in a span tag.That way you still get to apply CSS, etc and I think the <%= expression %> syntax would work.
Why don't you just set it in the Page_Init callback function in the code behind?
This example is geared towards getting the max length from underlying sql types in linq. But you should be able to customise it to your needs
http://blog.binaryocean.com/2008/02/24/TextBoxMaxLengthFromLINQMetaData.aspx
It looks like you want to be able to control the max length of a specific type of text box from a single location so that if that max length needs to change, you only need to change it in one place.
You can accomplish this by using a skin file. You set the max length in the skin file as you would normally and then any textbox that uses that max length would use the skin. If the length changes then you only need to change the skin file.
You can do it with databinding
<asp:TextBox
ID="tbName"
CssClass="formField"
MaxLength='<%# Constants.MaxCharacterLengthOfGameName %>'
runat="server" />
Then in the code behind
protected void Page_Load(object sender, EventArgs e) {
Page.DataBind();
}
You can embed "normal" code in the .aspx file if you so want, like:
<%
tbName.MaxLength = Constants.MaxCharacterLengthOfGameName
%>
<asp:TextBox ID="tbName" CssClass="formField" runat="server"></asp:TextBox>
This harkens back to an older style "classic" ASP way of doing this.

asp.net hyperlnk control

Is it possible to call a class's static property to set the navigateurl property?
<asp:HyperLink ID="hlRegister" NavigateUrl="<%= SomeClass.Property %>" runat="server" />
without using codebehind ofcourse!
You don't need code behind. You can just try it, like i just did. I created a simple page with exactly the code you have, and then created a class called SomeClass with a property named Property. It worked fine for me the way that you have it set up above.
Edit: Ok, it didn't compile with an error.. but It's giving me not the result I'm looking for.
http://localhost:3061/Sample/%3C%=%20SomeClass.Property.ToString()%20%%3E
using:
public static class SomeClass
{
public static string Property
{
get { return "http://www.google.com"; }
}
}
and
<asp:HyperLink ID="hlRegister" NavigateUrl='<%= SomeClass.Property.ToString() %>' Text="Goooooogle" runat="server" />
You can do this, but to avoid a syntax error you must modify your example to be as follows.
<asp:HyperLink ID="hlRegister"
NavigateUrl='<%= SomeClass.Property %>' runat="server" />
Notice the small difference of using single quotes rather than double around the script.
However, one might really ask why not just do it in the codebehind.
sure, in the code behind:
hl.NavigateUrl = Class.Static().ToString();

Resources