I have an ASCX that inherits from a WebControl that has a 'CustomConfiguration' property of type CollectionConfigurationItem. This configuration gets set elsewhere in the code, so by the time it gets to the ASCX it is set up to how I wish to use the data. At this point I'd like to render out another control using this configuration, like so:
<modules:DataModule runat="server" CustomConfiguration="<%# Model.CategoryListConfiguration %>" />
However, breaking into DataModule always results in 'CustomConfiguration' being null, which is the default value for the property. I've tried adding a Bindable attribute to the property but to no avail, and when I set an EventHandler for the DataBinding event on the DataModule it doesn't even get called.
How can I set this custom-class-typed property from the markup or, failing that what's the second-best method of getting this to work? Thanks for any light you can shed!
When you use the <%# %> syntax, it doesn't get executed until you databind the control. Try calling:
yourDataModuleID.DataBind();
e.g. from your Page_Init method.
Related
I have a Reapter control with a custom server side control inside of it. When I place the code:
<%# Eval("DateAdded") %>
inside the item template of the repeater it works fine, but when I place it inside the custom server control inside the repeater, it doesn't work.
Is it possible to do something like Parent.Eval() or Container.Eval() to get back to the context of the Reapeter rather than the custom control?
The repeater is databound, the control that you are placing this eval statement in isn't. You'll need to pass the value to the control using a property.
For Example:
<uc1:MyControl MyProperty='<%# Eval("DateAdded") %>' />
You can now access the MyProperty property inside your control to access this value.
It should work. Can you supply more context/code for the server control?
Also, are you using single quotes to set properties on a control dynamically using eval? Double quotes should throw an error, not just be ignored.
MyProperty='<%# Eval("DateAdded") %>'
I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.
I added a reference to the control in the .aspx page that will use it, like this:
<%# Reference Control="~/PictureGallery.ascx" %>
And in the UserControl I added a ClassName like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>
When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".
The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck.
So, I'm not able to get a reference to the UserControl.
How can it be fixed?
It sounds like you are confusing two separate ways of working with a UserControl.
One way is to register the control on your page so that you can put it into the page at Design Time e.g.
<div>
<asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">
</asp:PictureGallery>
</div>
The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this.
e.g.
Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)
edit
What is the class name of the control in the code behind...something like this:
Partial Public Class MyControlsClassName
Inherits System.Web.UI.UserControl
That is the type you need to use when you declare it. Is it within a namespace perhaps?
I don't think you've placed the control in your code behind. You may well have created the reference, but do you have a tag such as <asp:PictureGalary id="gallary"></asp:PictureGalary> anywhere in your aspx?
The ASP namespace is generated at run time- user controls get "compiled" as they are used by .aspx pages so this is why you get the error message "Type ASP.PictureGallery is not defined".
When adding user controls dynamically you should use the Page.LoadControl method:
Page.LoadControl("~/PictureGallery.ascx")
I'm a newbie in asp.net.
When using FormView, there is a big amount of code in ItemTemplate, EditItemTemplate and InsertItemTemplate which is almost identical.
For example:
<asp:ListBox ID="ListBox2" runat="server" Rows="1" CssClass="field"
DataSourceID="StatusList" DataTextField="DESCRIPTION"
DataValueField="STAT_ID" SelectedValue='<%# Bind("STAT_ID") %>'>
</asp:ListBox>
(Note: at the exception that Eval() would be used instead of Bind() in ItemTemplate)
I've been trying to avoid repeating this code but without the expecting result:
ListView allows the use of LayoutTemplate - but I didn't see any examples that insert this kind of code in LayoutTemplate. And inserting this code in LayoutTemplate would result in an error.
DetailView allows to produce code automatically but I'd like to use a specific design (for ex. using "fieldset" that encompasses some fields).
What would be the best way to avoid repeating this kind of code ?
You don't have to much choice about seperately specifying the Bind/Eval part, but you do have some control over the other pieces. You can make a custom UserControl that contains your layout.
Usually I include a property on this usercontrol called "Mode" which I either set to Edit or View, then based off of this property I change enabled/visible properties on the controls. You'll also need to include a property for each value you want bound/displayed in the usercontrol.
Put some labels, textboxes, etc... in your designer and hook them up to properties in your code behind, put the usercontrol on your page in your item/edit template and eval/bind to your data to the various properties (make sure to set the mode so it displays right).
I'm just testing a simple databind expression with:
<div>
Now: <%# DateTime.Now.ToString()%>
</div>
According to MSDN:
http://msdn.microsoft.com/en-us/library/bda9bbfx(VS.71).aspx
literal text <%# data-binding expression %>
should work.
When I compile, it's OK but I got a blank instead of Now DateTime.
What's wrong ?
You must call a DataBind method. In this case, add a call to the Page.DataBind in the page's load event. The DataBind method is not called automatically. You must call it for the page or for specific controls.
ASP.NET data binding overview at http://support.microsoft.com/kb/307860
I run into similar codes like this all the time in aspx pages:
<asp:CheckBox Runat="server" ID="myid" Checked='<%# DataBinder.Eval(Container.DataItem, "column").Equals(1) %>'>
I was wondering what other objects I have access to inside of that <%# %> tag. How come DataBinder.Eval() and Container.DataItem are not visible anywhere inside .CS code?
Within <%# %> tags you have access to
Anything that is visible in your code-behind class (including protected methods and properties).
Anything declared on the aspx page using <#import #>.
Anything passed in as the event arguments when the ItemDataBound event is fired (e.g. RepeaterItemEventArgs, DataListItemEventArgs, etc).
Container is actually a wrapper for RepeaterItemEventArgs.Item, DataListItemEventArgs.Item, etc. So you can actually access it in code behind within your ItemDataBound events as e.Item (e normally being the event arguments parameter name).
DataBinder is also accessible in code behind by using System.Web.UI.DataBinder.
On a side note, casting the Container.DataItem is preferred over using Eval. Eval uses reflection so there's an overhead there. In VB.NET it would be something like
<%#DirectCast(Container.DataItem, DataRow)("some_column")%>
Or C#
<%#((DataRow)Container.DataItem)["some_column"].ToString()%>
I believe you have access to anything within scope of the page class, though the results of the expression are converted to a string, so you can't embed conditional expressions the way you can with "<%" expression holes.
Here is a nice blog post which dives under the covers of the generated ASPX class.
Hope this helps.
<%# is specific to inline ASPX databinding like the link ckramer posted suggests.
How come DataBinder.Eval() and Container.DataItem are not visible anywhere inside .CS code?
To access the binding item in codebehind you would need to set up an ItemDataBound event.
ASP.NET generates a subclass of TemplateControl for each occurence of a template. Databinding statements are expressions used in a method inside that class. Thus, you can call any public/protected instance method on TemplateControl. See any example that uses XPath, as those will use the XPath and XPathSelect methods; Eval, XPath and XPathSelect are all instance methods on TemplateControl.
DataBinder is actually a separate class, and Eval is a public static method on it; it's in System.Web.UI. DataBinder.Eval and plain Eval are not directly related though they do very similar things visibly.
I believe that "Container" is actually a local variable or parameter where databinding statements are compiled. I can't remember its type at the moment.
using <%# %> actually means that code inside this block will execute when page.DataBind() method is being executed. Thus you can access anything at that point accessible as protected/public to that particular page/control.
Great example
<%#((System.Data.DataRow)Container.DataItem)["ColumnName"].ToString()%>