hajnis
hajnis

Reputation: 21

C# web forms - How to pass data from master page to master page when redirect is on web form?

When on master page I have dropDownList with event. When event Raise I want to load/redirect webForm.aspx. In this webForm.aspx I want to load data based on dropDownList selected item. At the same time I want to load again dropDownList and select Item on which clicked was.

I created some solution, where I was using Response.Redirect("webform.aspx?courseID=4") on masterPage.

Is this good approach to this problem? I think there must be better, but I cant see it.

Thanks for answers.

Upvotes: 0

Views: 1393

Answers (1)

Khazratbek
Khazratbek

Reputation: 1656

One way is your solution: to use querystring.

The second way is: on dropdownlist's value changing you changing value of some session value (e.g. *Session["course_id"]), then Response.Redirect("webform.aspx") and restore by Session["course_id"] value.

protected void ddlCourses_SelectedIndexChanged(object sender, EventArgs e){
    Session["course_id"] = ddlCourses.SelectedValue;
    Response.Redirect("webform.aspx");
}

ABOUT YOUR SECOND QUESTION (HOW TO TAKE ROUTE VALUE FROM MASTER PAGE). I will give just brief explanation and a little example.

Create in your master page some method:

protected void IWantToWarkWithRouteValue(){
    //here you will do something
    //and to take value from routing, just use Session["myValue"] (I will explain this value later)
}

You should call this method in Page_Load, not in Pre_Init or Init

Then in your Course.aspx (caz' you using routing here) create the next method:

protected void SetCourseSessionFromTheRoute(){
    if (Page.RouteData.Values["courseID"] != null)
        Session["myValue"] = Page.RouteData.Values["courseID"].ToString();
}

And call this method in Page_PreInit:

protected void Page_PreInit(object sender, EventArgs e){
    SetCourseSessionFromTheRoute();
}

Thanksgiving to ASP.NET page's life cycle, you may do anything you need.

You may read about page's lifecycle here

As I said, it is just an example. You may find a better solution.

Hope it helps.

Upvotes: 1

Related Questions