C#Web表单 - 当Web表单上的重定向时,如何将数据从母版页传递到母版页?

时间:2016-03-17 19:59:21

标签: c# asp.net webforms response.redirect

在主页面上,我有dropDownList with event。当事件提升我想加载/重定向webForm.aspx。在这个webForm.aspx中我想基于dropDownList选择项加载数据。同时我想再次加载dropDownList并选择单击的项目。

我创建了一些解决方案,我在masterPage上使用Response.Redirect(“webform.aspx?courseID = 4”)。

这是解决这个问题的好方法吗?我认为必须有更好的,但我无法看到它。

感谢您的回答。

1 个答案:

答案 0 :(得分:0)

一种方法是使用 querystring

第二种方式是:on dropdownlist的值改变你改变一些会话值的值(例如* Session [“course_id”]),然后 Response.Redirect(“webform.aspx”)并恢复通过 Session [“course_id”] 值。

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

关于你的第二个问题(如何从主页上获取路线值)。我将给出简短的解释和一个小例子。

在您的母版页中创建一些方法:

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

您应该在 Page_Load 中调用此方法,而不是在 Pre_Init Init

中调用此方法

然后在你的 Course.aspx (caz'你在这里使用路由)创建下一个方法:

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

Page_PreInit

中调用此方法
protected void Page_PreInit(object sender, EventArgs e){
    SetCourseSessionFromTheRoute();
}

感恩节到ASP.NET页面的生命周期,你可以做任何你需要的事情。

您可以阅读有关页面生命周期here

的信息

正如我所说,这只是一个例子。您可能会找到更好的解决方案。

希望它有所帮助。