i have a template with some tokens like %{login}% or %{menu}%
i have also the related user controls Login and menu
the html string template comes from the db, my problem is that if i render the usercontrols with the Page.RenderControl or Server.Execute methods, i obviusly take the error "texbox/button must be placed inside a form tag with runat=server"
the login and menu usercontrols have inside the asp.net controls, so, how can i replace my tokens programmatically with the usercontrols leaving working the asp.net controls functionalities in the login and the menu?
Programmatically, you could read your string, and translate the tokens to the appropriate controls (which you probably already do), and, in so doing, you would instantiate an instance of your control and add it to an existing control collection, most commonly a PlaceHolder control.
Here's some reading on the topic: Dynamic Controls in ASP.NET
Related
What I have is few dropDownLists in few different templates of FormView.
And of course I can't get access to any of those.
What I want to do is get the same behavior for every DropdownList.
Is there any way to declare that DropDownList globally and reuse it or it should be done somehow with FindControl?
User control declarative syntax is very similar to syntax used to create an ASP.NET Web page.
The primary differences are that the user controls use an # Control directive in place of an # Page directive, and that the user controls do not include the html, body, and form elements around the content.
MSDN/creating usercontrol
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 a user control that contains only a text box and on another form I add this user control dynamically, a user can add the user control many times. I use a session variable to recreate the user control (maybe this approach doesn't sound cool). After recreating the control the value of the textbox disappears, obviously. Is there any solution to maintain the state of the user control on postback?
If you add dynamic controls back to the control during the correct Page Life Cycle event(PreInit) they will maintain their state through the IPostBackDataHandler interface.
PreInit - Create or re-create dynamic controls.
I've had the same problem in the past.
What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback (in my case, I kept all the information in the session, and re-created the controls).
Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.
Use the object cache. Add the usercontrol into the cache and retrieve it when you need it.
You can see a nice example of how this works at: ASP.net-Tutorials Cache and Object Cache.
I am also learning asp.net now and found that quite a nice explanation. I also used the Microsoft Library
Every server control that inherits the IPostBackDataHandler interface has a LoadPostData method that processes the postback data. When control is implemented by a class (page, form, placeholders, controls etc), that class calls the LoadPostData method and passes the posted data and key to maintain control states.
All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then the posted data will automatically be assigned to the control by calling LoadPostData method by the parent control.
Check this article to learn how to write code for dynamic control -
How to maintain dynamic control events, data during postback in asp.net
What would be the best approach to set dynamic content from the database to controls in an aspx page?
My database consists of pages (index.aspx, home.aspx and so on) which consists of controls (DivStart, LabelDescription, and so forth).
The first technique that came to my mind was looping through all the controls in the page, looking for controls that have a certain class, e.g. "Cms_DivStart", and would then set the inner html for that control from the database control called "DivStart". The problem is only runat server controls shows up, and I don't want to make all controls server side.
I could store all the dynamic texts for a page in hidden variables and set it with jQuery when the page has loaded, but that would make the text not show up directly..
Any other ideas would be greatly appreciated.
Thanks
/Andreas
You could use asp:PlaceHolder tags, these don't add any html unless they are utilised.
You can write a custom class, I'll call it "CustomPage", that inherits the System.Web.UI.Page class. Then your pages can inherit "CustomPage". In this class, add methods to retrieve data, set custom properties and display your content in the available controls.
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.