I have a simple question in asp.net.
I want to know if it is possible to get data from controls in my user control directly . I want to do it without using Session variable,Viewstate …
EDIT: I now use the method of declaring public variables in the UC.
Here is a part of Page_load from my parent page:
this.plan_action = (UCPlan)Page.LoadControl("~/Association/UCPlan.ascx");
PlaceHolder1.Controls.Add(this.plan_action);
if (this.plan_action.Validate == true)
{
CheckBox1.Checked = true;
//String référence = Session["liste_action"].ToString();
for (int i = 0; i < this.plan_action.List1.Count; i++)
{
Label8.Text += this.plan_action.List1[i].Référence + "/";
//Label8.Text += "/";
}
}
but my variable validate stay to false.
Here is the code where I change the value of the validate variable with it declaration:
private bool validate;
public bool Validate
{
get { return validate; }
set { validate = value; }
}
protected void Button2_Click(object sender, EventArgs e)
{
//myCommand.Connection = myConnection;
//Session["liste_action"] = this.List;
this.Validate = true;
//Response.Redirect("../Risques_folder/AjouterRisque.aspx");
}
Thank you for your help,
Quentin
UPDATE due to new information
You need to learn about the sequence of events in ASP.NET.
The
Loadof the page happens a long time before theClickhandler ofButton2in your UserControl… so theValidateproperty is always going to be set tofalse.You have two obvious options (as I see it)…
Keep the creation of the
UserControlin yourPage_Load(or preferably, move it to yourPage_Init, as this is normally the most appropriate place for it). Then place your check for theValidateproperty in aPage_PreRender.Or, create an
Eventin yourUserControl,Raisethat event on the click of Button2, and handle the event in the Page.ANOTHER UPDATE
For the 2nd of the two options above, in your
UserControlclass have the following…In the
Button2_Clickmethod of theUserControl(after setting thethis.Validate = true;) call…In the
Page_Initof thePage, put something like…And then have a new method called something like
Remember, as you control the
delegateyou can pass whatever information you want, including an entire class. So instead of calling theValidateproperty, create a new instance of the class you want, and pass that as a delegate parameter.You can find more information on delegates and events on MSDN.
ORIGINAL ANSWER
Unless I’ve missed something, this is a very simple ASP.NET concept…
You can create properties and/or methods.
For example, as a property…
Or as a method
If you’re talking about passing the values between the
UserControland the ASP.NETPagethat contains it, then in yourPage, you can simply call the property or method. If your control was called (for example)myCtrl, then you can something like…(On the back of the great comment from AHMED EL-HAROUNY)
If you’re talking about passing the values to the client side page, then you can use the same properties / methods directly in the HTML markup. However, in this case, the properties / method can be declared as
protectedrather thanpublicFor instance, to display the value…
Or
Or if you’re going to use the value in javascript, something like…