UserControl - accessing textbox within UserControl within a web form - c#

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.

Related

Use one control multiple times in ASP.NET

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

Correct Pattern for creating Server Controls?

I am creating a server control (inheriting from CompositeControl).
The control comprises of several data bound drop down lists (and some textboxes etc).
The drop down lists form a hierarchy, so are dependent on each other... eg
ddlCountry
-ddlCounty
--ddlCity
When ddlCountry is selected, this should refresh the list of ddlCounty... and so on with the ddlCity etc.
I would like to access the value of the ddlCountry within the control, so I can set the data source for second ddl (ddlCounty) accordingly,
but in CreateChildControls() the viewstate does not seem to have loaded the ddlCountry user selection yet into the control, so I get an empty string.
I also need to provide these ddl values as a public property.
This is getting quite messy, with the use of ReCreateChildControls() etc... which I'm sure is wrong.
My Question:
Can anyone suggest the correct pattern / set of rules to abide by to create this server control
eg
initialise controls OnInit
DataBind on OnLoad
Put EnsureChildControls() at beginning of any property: get or set
I assume there must be a correct way of implementing this, but cannot find any documentation or example to get this working as you would expect of a standard control.
Thanks

Maintain the state of dynamically added user control on postback?

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

How to access Form controls on Postback from Dynamically built form

I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.

ASP .NET - Run required field validator and calloutextender in code behind?

I am using ASP .NET (C#) and have a page with a listview linked to a sqldatasource.
The listview has a InsertItemTemplate which contains many textboxes. I want to make all the textboxes required via the RequiredFieldValidator (and the ValidatorCallOutExtender).
Is there a way to do this in the codebehind instead of the aspx page?
Ideally I would like the page to validate each control with the same method with only the error message changing.
I take it your question is about dynamically adding controls (and not about codebehind validation).
I didn't know and ran a few tests. It is possible to simply add (Validator)controls to the Controls property of the Form element, but controlling the order is not so simple (no Controls.Insert()).
So my advice would be not to use a plain TextBox but a UserControl. You could probably use Search&Replace to fix up the ItemTemplate.

Categories

Resources