I need to place a user control into the template of a repeater control and reference data items from the repeater's datasource.
I tried (ignore the second cast which is specific to the CMS platform I'm using):
<%# ((EPiServer.Core.PageData)((RepeaterItem)Container.Parent.NamingContainer).DataItem)["PageName"]%>
But that throws back the following error: Unable to cast object of type 'ASP.usercontrols_searchcontrols_searchresult_ascx' to type 'System.Web.UI.WebControls.RepeaterItem'
Searchresult_ascx is another user control that contains the actual repeater.
I would add a property on the usercontrol to hold the container - like this:
<asp:repeater ... >
<my:usercontrol containerdata='<%# Container.DataItem %>' ... />
</asp:repeater>
And of course inside the user control databind to the PageData item you are passing along.
It sounds like you have more parents in the hierarchy to get to the control desired. I often use the immediate window in debug mode to find the depth I have to back out OR use the Trace="True" on the web page and look at the control tree to see the hierarchy. From that you should be able to figure out your code to get at the proper parent control.
I would add an OnItemDataBound event to the repeater and from there bind the appropriate data to the user control.
Related
I have the following asp.net div setup on my page:
<div runat="server" id="ImageContainer">
<img src="images/<%# Eval("Image") %>" class="ArticleImage"/>
</div>
and I know this is being created at the server as when I view it in developer tools its id is:
ctl00_body_ArticleInfoRepeater_ctl00_ImageContainer
which is being created by the server.
I now want to edit the Css under certain conditions by doing the following:
ImageContainer.CssClass = "invisibleClass";
My problem is, in the C# code I am getting the following error:
The name 'Image container' does not exist in its current context.
I have no idea why this is happening, as it clearly does exist.
Any ideas?
Are you using some databinding control such as GridView, Repeater or some other?
The syntax <%#....%> represents data binding which works if it is placed inside some data binding control.
In such case you cannot access "ImageContainer" control directly. You have to search through parent control.
Since you haven't mentioned what is parent control of "ImageContainer" it's hard to give code sample here... Though here is example how it can be done in GridView
Incase, if you haven't used DataBindingControl then I would recommand to check yourpage.aspx.designer.cs and you should be able to find control name there!
Hope this will be helpful.
That's because you're trying to reference a dynamically created object (obiviously inside a repeater). Firstly, you shouldn't set ID for dynamic objects created during runtime. IDs have to be unique, or else you'll run into problems.
Secondly, you need to get the parent object (probably the repeater itself or the container?) and then traverse the collection of child controls to find the one you're after. There are numerous answers about how to find a control in an asp.net webforms, so just google for a while and I'm sure you'll get the code.
For instance find control
Controls placed on data list,repeater,grid-view are not accessed directly like other server controls placed on the page. If you want to access it through code behind you can access it on Data-bound or Item_command event of the repeater control because these controls itself act as containers for other controls placed on them.
you can use
e.Items.findControl("controlID") to access a particular row controls.
I recommend you to study these two events of repeater control.
if you want to change class of all div with name imagecontainer , you can use javascript or jquery for doing it with few lines of code.
I have struggled to find a clear and concise answer to this simple question.
Given an object property in the page named "SomeObj", is it possible for two way data binding like this:
<asp:TextBox ID="TextBox1" runat="server" Text="<%# SomeObj.SomeProperty %>" />
This will correctly display SomeProperty in the textbox if we call "this.DataBind()" in Page_Load. However, my object property is never written to during postback - have I done something wrong or is this actually not possible?
Should I put this control inside some container like FormView or DetailsView? At what point in the page life cycle should I be able to see my object property being written to?
All the thousands of copy-and-paste tutorials out there focus on lists and grids and binding to Sql Data Sources, which I do use successfully. But for a simple, single object surely there is a way that does not require extraneous containers, data sources and code-behind?
Of course the code to do this in code-behind for one text field is trivial, but no longer when multiplied by loads of properties and loads of pages. I have instead written two simple routines that use reflection to automatically load controls with property values and then save the control values back to the properties. With these routines we can then fill the page with simple markup like this:
<asp:TextBox ID="SomeObj_SomeProperty" runat="server" />
By simply naming the controls the same as the object property and calling my two methods at the appropriate times I get two-way data binding and the code-behind has very little code in it other than creating the main object, and I do not have to wrap the markup inside other controls just to get the data binding to work.
I am not very experienced with ASP.NET and it seems like I am doing something that should already be done for me.
UPDATE:
Note that if I try to use Bind() instead I get the usual data binding exception because there is no special data-binding control like a list or grid view:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeObj.SomeProperty") %>' />
Gives this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used
in the context of a databound control.
can this be done?
I have c# .aspx page with two web user controls on both displaying Listview of data.
Now if you delete item from 2nd user control listview I want 1st update to show changes as well.
I've added the following in 2nd user control
<%# Reference VirtualPath="controlOne.ascx" %>
And I can call public void BindListview(); to rebind the listview.
But Listview on controlOne comes back with Null error as can't find listview.
Is there a way around this?
You need to call LoadControl method before working with Public members of the controlOne.ascx
take a look at the sample:#Reference
I am using ASP.NET Web Forms and i created a custom .ascx user control which represents a Car having properties like Type, Picture and Color.
Now i try to add on user interface this custom control in a repetitive way. Lets say if i have in database 5 cars, then 5 user controls will be render on UI preferable inside an asp control.
I was looking for such a control, studying the grid view, and repeater but they seems to have as data source only lists of objects which are build from "primitive" types not objects like a list of my custom ascx control.
My question is if i can somehow to render those user controls inside a repetitive cycle inside an asp or html control which gives the option to format it (supports css) ? (and if so please provide me an example)
Create a ListView with your user control inside of it, and then bind a List<CarObject> to that ListView
<asp:ListView ID="listView1" runat="server">
<ItemTemplate>
<myUC:MyUserControl ID="myUserControl" runat="server" />
</ItemTemplate>
</asp:ListView>
Bind a list of your object, such as List<Car>, to that list view, listView1.DataSource = myListOfCars;. Then in a ItemDataBound event bind your object to your user control.
You could also, alternatively, place the markup from your user control inside of the ListView, and then bind the information inside of an ItemDataBound event and sidestep the whole user control issue altogether. This still allows you to reuse the markup.
I have an ASP.NET web app that places several string data into an object as properties. The string data is sourced from a JSON feed from Twitter. A collection of my TwitterMessages is held in a Generic List.
What I want to do is use an asp:repeater to iterate through my List<T> to display all of the contents of the objects it holds, by using <%# Eval("MyURL") %>. MyURL is the name of a property inside my TwitterMessage object.
In a previous question I was advised to use the asp:Repeater template to display my data in a custom HTML table. So being able to use a template somehow or other, is really what I'm interested in.
Where I am struggling is in working out how to perform a databind to the Repeater so I can reference my object data in the .aspx page.
I am aware that I need to use the ItemDataBound method in order to make a data-bound event so that I can reference the property names in my string data object.
Hope that's enough info for some much appreciated help! :)
It's pretty straightforward to databind to your repeater. Depending on where your list object resides, you can either bind your repeater using markup, or codebehind.
If you want to do it in markup, you should make an ObjectDataSource wrapper around the List. Something like this:
<asp:Repeater ID="rpt" runat="server" DataSourceID="twitterFeedSource" >
<ItemTemplate>
<tr><td><%# Eval("MyURL") %></td></tr>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="twitterFeedSource" runat="server"
SelectMethod="GetTheListOfTwitterFeedObjects"
TypeName="myTwitterFeedClass"
>
</asp:ObjectDataSource>
The GetTheListOfTwitterFeedObjects method should return the list of objects. Each object would need to have the property MyURL. You can of course extend your template and bind to any other properties that your twitter objects have.
Otherwise, you can do it straight from the code. Simply do something like this in Page_Load:
if (!IsPostBack)
{
myRepeater.DataSource = myGenericList;
myRepeater.DataBind();
}