normally when i want to dislplay the value of a dataitem in a repeater, i use this:
<%#Eval("contact") %>
but now i want to transform the value of this field (contact) in a codebehind function, so i tried this:
<%= ShowcontactInfo(Eval("Contact")) %>
but then i get the exception (at runtime):
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
What can i do?
Michel
Does this not work?
<%# ShowcontactInfo(Eval("Contact")) %>
Note that Eval returns an object, so your method ShowcontactInfo either needs to aceept an object, or you need to use a cast.
<%= is a shortcut for Response.Write, and is not the same as thnhe databinding syntax <%#
Can't you do it like this? : <%# ShowcontactInfo(Eval("Contact")) %>
Related
This is how a register the control on the page:
<%# Register TagPrefix="uc" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And this is how I use it:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
I don't know why uc1 has a green underline warning. When I hover over it, it says: "Unrecognized namespace uc1". And the user control is not displaying on the page.
Am I missing something? Yet this tutorial says that this is all I need to do.
You are specifying a uc prefix in <%# Register TagPrefix="uc" .. so that's the one you should use:
<uc:Pcp ID = "pcpClient1"></uc:Pcp>
Or you could change the declared prefix:
<%# Register TagPrefix="uc1" TagName="Pcp" Src="../../Controls/ClientPayement.ascx" %>
And use the control as you posted in the question:
<uc1:Pcp ID = "pcpClient1"></uc1:Pcp>
EDIT
As Shai Cohen pointed out in another answer, you are also missing the runat="server" from the usercontrol tag.
<uc1:Pcp ID = "pcpClient1" runat="server"></uc1:Pcp>
In addition to Juan's answer, you are also missing runat="server".
I have a web control that looks like this
public class Foo : WebControl
{
[Bindable(true)]
[Category("Default")]
[DefaultValue("")]
[Localizable(true)]
public string Bar { get; set; }
protected override void Render(HtmlTextWriter output)
{
output.WriteLine(Bar);
}
}
I want to put this webcontrol in my aspx page like so:
<cc1:Foo Bar="<%= Fa.La.La %>/otherstuff" runat="server" />
(obviously this code is simplified to show the problem)
In my Render method the variable Fa.La.La is not evaluated. It's coming in as the raw text "<%= Fa.La.La %>" How do I evaluate it?
I'm not particular how the variables are passed in. If the variables can be evaluated if they are passed in as <%# ... %>, that works fine. The point is I have some server-side variables I want evaluated before/while my Render() method is called.
The only thing I can think of is to use a regex to grab the contents of <%= ... %> and use reflection or something, but there has to be a more elegant way to do this.
This question is pretty similar to using server variables in a href <%= xx %> with runat=server, but it's not exactly the same since none of the answers there were useful.
Well, first you should be clear to diff between both tags.
here are some points i have read and used practically..
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if
DataBind() is not called.
<%# expressions can be used as properties
in server-side controls.<%= expressions cannot.
read more it on MSDN Blog
You should have to use binding expression <%# expr %>.
<cc1:Foo Bar='<%# String.Concat(Fa.La.La,"/otherstuff")%>' runat="server" />
and call DataBind() method in code-behind.
public void page_load()
{
DataBind();
}
I 'm trying to make a "login information" on the top panel, like "Welcome Back XXX", so I use
<% Response.Write(Session["username"]); %>
inside the aspx page.
It works, but is there anyway to use the variable directly without Response.Write here? It seems unnecessary.
There is a simple "shortcut" in the ASP.NET page syntax to Response.Write.
<%= Session["username"] %>
is functionally equivalent to
<% Response.Write(Session["username"]); %>
Typically you want to encode your session variables as HTML using Html.Encode, in case they contain characters which are not in the accepted HTML range. If you're using ASP.NET 4, you can use <%: %>, which is equivalent to Response.Write(Html.Encode(string)).
You can do it like this:
<%= Session["username"] %>
And if you use ASP.NET 4.0 you can automatically HTML encode the value by using this syntax:
<%: Session["username"] %>
put a asp.net label on your page, like
<asp:Label id=lblUserName runat="server" />
and on your codebehind page, on page_load event or on proper event
lblUserName.Text = String.Format("welcome back {0}",Session["username"]);
use a label and assign user name to it
In aspx (html code)
<asp:Label id=lblUserName runat="server" />
In aspx.cs (Code behind)
lblUserName .Text = "Welcome back"+Session["username"].ToString();
The correct way.
First is to check if the value is null
Second because you write it on a page, use the HTMLEncode to be sure that you avoid any type of injection, or problems.
now, if you like to use a Literal or a Label, or just direct write it, is up to you. If you going to place it inside an UpdatePanel you must use a Literal.
Now, if you use Literal avoid to set the ViewState to gain space from it, ether way you need to set it on PageLoad. And it will be
<asp:Literal runat="server" id="txtUserName" EnableViewState="false" />
and on page load.
if(Session["username"] != null)
{
Debug.Assert(Session["username"].ToString.Length > 0 , "Check out why I have zero user name");
txtUserName.Text = Server.HTMLEncode(Session["username"].ToString);
}
I am doing this:
<% Html.RenderPartial("SomePartial", Model); %>
My Model has a property UserID
In my partial I try this:
<%= Model.UserID %>
but I get this error:
CS1061: 'object' does not contain a definition for 'UserID' and no extension method 'UserID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Blankman,
Make sure your partial ascx file defines the model as per the main view i.e:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Your.Model>" %>
[edit] - as Stefanvds mentions below, if you only need the id portion from the model, then define your partial as:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
that would be the definitive i reckon :)
cheers
Make sure that at the top of your partial view you have Inherits attribute of your Control tag set to the type of the model you're passing in:
Inherits="System.Web.Mvc.ViewUserControl<MyNamespace.MyStronglyTypedModel>"
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DB.Models.TheModelYouWantToSend>" %>
your partial should start with this.
also, if you want to send the whole model, you dont need to specify it.
<% Html.RenderPartial("SomePartial"); %>
or if you would only like to send the ID.
<% Html.RenderPartial("SomePartial", Model.UserID); %>
which would make the header
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>" %>
The model type on your Partial View and the model you pass in to Html.RenderPartial method should match.
The way this page is laid out, all of the data is loaded at Page_Init. Well, I have a custom control that is having problems with this though.
I have it on the page like so:
<cc:SomeControl... />
And then I set the value at Page_Init using
MyControl.Value="blah";
Simple stuff..
The Value is an accessor and has something similar to this:
public string Value{
get...
set{
EnsureChildControls();
MyHiddenField.Value=value;
}
}
and it is here that I have a problem. It says that MyHiddenField is null. Is Page_Init just too early for this? Or is there some other function I need to call?
The fix for this was changing from using a namespace to reference the CustomControl to using a src with a filename
changing this:
<%# Register Assembly="MyProduct" Namespace="MyProduct.CustomControls" TagPrefix="cc" %>
to this:
<%# Register src="/CustomControls/MyControl.ascx" tagname="MyControl" tagprefix="uc2" %>