Hi everyone,
I'm having a problem with using the DropDown list control in ASP.NET 2.0 and the selected item name not being retained in the control. I have this control on a MasterPage and when you select Blue I do a Response.Redirect to a content page. The problem is I'd like the DropDown list control to retain the name Blue, but it doesn't work it always displays the blank from "" after you make your selection. I have ViewState enabled as well as AutoPostBack on the DropDown list control set to true. If I set AutoPostBack to false, then the selected name is retained, but the Response.Redirect doesn't fire. Please note below:
//MySiteMasterPageprotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ColorsDropDownList.Items.Add("");
ColorsDropDownList.Items.Add("Red");
ColorsDropDownList.Items.Add("Green");
ColorsDropDownList.Items.Add("Blue");
ColorsDropDownList.Items.Add("Yellow");
}
}protected void ColorsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
switch (ColorsDropDownList.SelectedValue)
{
case"Red":
Response.Redirect("~/Red.aspx",false);
break;
case"Green":
Response.Redirect("~/Green.aspx",false);
break;
case"Blue":
Response.Redirect("~/Blue.aspx",false);
break;
case"Yellow":
Response.Redirect("~/Yellow.aspx",false);
break;
}
}
I have searched thorough the forums for similiar issues, but everything I've tried either hasn't worked or was not close enough to my situation.
Thank you,
What you need to understand about Viewstate isthat it is only applicable within a single page during postbacks; thatis, once you switch pages, all the page data from the prior page isgone.
However, if you must redirect to adifferent page for each color -- and I'm not sure you have to do that,since you could easily make one dynamic page -- you can hard-code theselected value of the DDL for each page. That is, if you're onthe Blue page, youknow the user picked Blue, so you can just set it when the page loads.
Cheers,
I may not have explained this properly. First I tried to pick colors as a simple example; however, the actual application is for a reporting application. There is a MasterPage that contains the TreeView control showing the reports the user is able to access based on their user role. The DropDownList control represents various departments. When the user selects one of the reports they have access to it is displayed in the TreeView it displays the department page with the selected report. Following what you suggested by hard coding I'm not following how to set the DropDown control manually to the department page name that was selected from the DropDownList control.
Report Portal
----------------------------------------------------------
Select: contains the list of depts|
General Rtps | Actual report content is displayed in this area.
Financial Rtps |
Sales Rpts |
Top 5% Sales |
Top 20% Sales |
Inventory Rtps |
Hi
I tried to work the same thing it seems to have worked fine for me, I am giving the full code that I used, may be that will help
MasterPage
-----------------------
<%@. Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID=ColorsDropDownList AutoPostBack=true runat=server OnSelectedIndexChanged="ColorsDropDownList_SelectedIndexChanged1"></asp:DropDownList>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
------------------------
Master page code behind
---------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ColorsDropDownList.Items.Add("");
ColorsDropDownList.Items.Add("Red");
ColorsDropDownList.Items.Add("Green");
ColorsDropDownList.Items.Add("Blue");
ColorsDropDownList.Items.Add("Yellow");
}
}
protected void ColorsDropDownList_SelectedIndexChanged1(object sender, EventArgs e)
{
switch (ColorsDropDownList.SelectedValue)
{
case "Red":
Response.Write("~/Red.aspx");
break;
case "Green":
Response.Write("~/Green.aspx");
break;
case "Blue":
Response.Write("~/Blue.aspx");
break;
case "Yellow":
Response.Write("~/Yellow.aspx");
break;
default:
Response.Write("~/default.aspx");
break;
}
}
}
----------------------
Default page
----------
<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content
------------
Hope this helps
thanks
vikram
Vikram's Blog
Ok, yes trying your example does work; however, I am not doing a Response.Write("~/Red.aspx");. I am doing a Response.Redirect("~/Red.aspx", false) to send the user to a page called Red.aspx. Actually this is just an example and not the actual name of the page the user is be redirected to.
Is it possible to get the same effect as using Response.Write, but instead use Response.Redirect??
Thanks,
Lance
When you do a Response.Redirect, all the ViewState information is lost. You need to use a different type of session state to keep track of the value
You could store the value in a session variable and then after the response.redirect read from it
Session["ddlIndex"] = ddlDropDownList.SelectedIndex;
Response.Redirect("otherpage.aspx");
On the other page you would do this
ddlDropDownList.SelectedIndex = (int)["ddlIndex"]
Thanks for the reply James. I see what you are saying, but when I try to compile this I receive a few errors on the line on the other page.
ddlDropDownList.SelectedIndex = (int)["ddlIndex"];
Invalid expression term '['
; exprected
; expected
Invalid expression term ']'
How do I fix this? Also that above line will still maintain the name of the item that was selected from the drop down list control even though the control resides on the Master Page and this is just a content page??
Thanks
It should be ddlDropDownList.SelectedIndex = (int)Session["ddlIndex"];
I didn't check the code so it may have minor syntax errors like forgetting the ; terminator.
Ok, i should have caught that Session was missing also. That takes care of that problem, but I still have an issue with the Drop Down List Control. The name doesn't exist in the current context, because it is located on a different page.
0 comments:
Post a Comment