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.
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.
In my asp.net application, I need to be able to dynamically add user controls based on data in a database.
For example, on page1, I will bind three elements to a repeater:
some html content
a user control
more html content.
The repeater on the page is surrounded by an updatepanel
(updatemode=conditional, childrenastriggers=false)
The user control also has it's own updatepanet
(updatemode=conditional, childrenastriggers=true)
So, what I have is something like this:
outer update panel<br/>
repeater<br/>
item 1 = html<br/>
item 2 = user control<br/>
user control update panel<br/>
user control content<br/>
/user control update panel<br/>
item 3 = html<br/>
/repeater<br/>
/outer update panel<br/>
The problem is, I don't get any events fired by my user control. I'm pretty sure I need to create the control in the page_init, but I'm a little unsure of how to do this, since I may have to create any number of user controls of different types, and place them at different locations on the page. Has anyone ever run into this problem before, and how did you solve it?
Steps
Add add a placeholder control to updatepanel.
In CS file create a instance of your usercontrol.
Add that control to placeholder.
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.
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.
I am using c#.net
I have different views within my webform, these all generally display different information except for three textboxes (arrival / seen / depart time). To try and cut down on code, I have created a UserControl which contains these three textboxes.
I have referenced the UserControl at the top of my webform and also within each view.
<%#Register TagPrefix="uc1" TagName="userTimes" Src="~/usercontrols/userTimes.ascx"%>
<uc1:userTimes id="userAppointmentTimes" runat="server"></uc2:userTimes>
can’t seem to access the textboxes from the code behind. I need to firstly populate the textboxes and also hold any updated information to be re-inserted back into the database if changed.
Also each textbox has two Validation controls:
First makes sure it is in time
format HH:MM
Second makes sure the arrival is
before the seen time etc
My two questions are:
How do I access the UserControl from
the code behind? I have read that I
need to use the FindControl but I
don’t understand why, when I know
what it is called.
Do I undertake the validation
(server side) within the UserControl
code behind or the webform code
behind?
Thanks in advance for any help.
Clare
1.) You can access the User Control by its ID from the page's code behind - userAppointmentTimes in your case. To access your TextBoxes within the webform you need to use the FindControl-Method at the User Control level. So something like userAppointmentTimes.FindControl("WhateverTextBoxID") should work. You need to cast the result to TextBox of course.
You can't access the text boxes because ASP.Net does not automatically expose them for you. So alternatively you can provide public properties to set/get values to/from your textboxes inside your user control.
Within the user control, you can access your textboxes by their IDs.
2.) Put the validation controls inside your user control.
By webform you mean it's all inside the asp.net form-tag or do you have an asp.net form like FormView nested inside? If the latter is true you need to use FindControl at the FormView level - formView.FindControl("userAppointmentTimes"). Otherwise the user control is accessible from page level via its ID.