views:

607

answers:

1

Hello all,

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:

  1. First makes sure it is in time format HH:MM
  2. Second makes sure the arrival is before the seen time etc

My two questions are:

  1. 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.
  2. 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  A: 

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.

360Airwalk