sp_m
sp_m

Reputation: 2705

How to access value of usercontrol in asp.net

I have a web application that assigns time duration to students in a day. kind of a scheduler.

so for this i have a user control as TimePeriod and this control is loaded dynamically on the web page.

But the number of that user control on a page varies so for this i have user a code that dynamically creates a list of user control.

For example purpose i have set i value up to 2, actually it varies Looks like this:

for(int i=0;i<2;i++)
{
    TimePeriod ib = (TimePeriod)LoadControl("TimePeriod.ascx");
    //ib.RightArrowImgUrl = "~/images/wcm-circle-arrow-button.png";
    span_tempList.Controls.Add(ib);

}

When user is done making changes for time for all days. i.e. in all user control on page.

the page has a button which takes this changes to database.

but the problem arises here that How to access the values of these text boxes values from the page as it has now been rendered as html.

This is my User Control Code:---

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TimePeriod.ascx.cs" Inherits="ClassManagment.TimePeriod" %>


<div>
    <asp:TextBox ID="time3" size="10" value="08:00" runat="server"></asp:TextBox><asp:TextBox ID="time4" size="10" value="09:00"  runat="server"></asp:TextBox>

</div>

Code Behind of usercontrol:--------

 public partial class TimePeriod : System.Web.UI.UserControl
 {
        protected global::System.Web.UI.WebControls.TextBox time3 = new System.Web.UI.WebControls.TextBox();
        protected global::System.Web.UI.WebControls.TextBox time4 = new System.Web.UI.WebControls.TextBox();


        public string TimeFrom
        {
            get { return time3.Text; }
            set { time3.Text = value; }
        }

        public string TimeTo
        {
            get { return time4.Text; }
            set { time4.Text = value; }
        }
 }

How i can access values of usercontrol ?

Upvotes: 1

Views: 3034

Answers (3)

Claudio Redi
Claudio Redi

Reputation: 68440

On the button click handler, you can have something like this

string timeFrom = null;
string timeTo = null;

foreach (Control control in span_tempList.Controls)
{
     if (control is TimePeriod)
     {
         timeFrom = ((TimePeriod)control).TimeFrom.Text;
         timeTo = ((TimePeriod)control).TimeTo.Text;
         // Do something with these values
     }
}

Upvotes: 1

Francois Zard
Francois Zard

Reputation: 341

Keep a global array of your dynamically generated user controls in your class and access those stored instances directly as such:

public partial class Foo : System.Web.UI.Page
{

   TimePeriod [] timePeriodArray = new TimePeriod[2];

   protected void Page_Load(object sender, EventArgs e)
   {
     for(int i=0;i<2;i++)
     {
         TimePeriod ib = (TimePeriod)LoadControl("TimePeriod.ascx");
         span_tempList.Controls.Add(ib);
         timePeriodArray[i] = ib;
     }

   }

then you can access those instances directly.

Upvotes: 0

hackp0int
hackp0int

Reputation: 4161

like so:

TimePeriod ib = (TimePeriod)LoadControl("TimePeriod.ascx");
string timeTo = ib.TimeTo;
string timeFrom = ib.TimeFrom;

Upvotes: 1

Related Questions