I am......
BUT if you do a Response.Redirect("samepag
Main Topics
Browse All TopicsHi there,
I have two dropdown lists that are filled with information from a SQL DB when the page first loads..... so far so good.
Next from the information the user chooses from the drop down list the page automatically refreshes and gives the user some stats and uses the LinkButton control to display them.
What i would like is for the user to be able to click on a link and the page refresh (with some extra querystring info) while still keeping state with the two drop down lists.
I can do either but i can't seem to work out how to do both.
Thanks in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
why do u need to redirect ....
just in the page load event check the values of two drop down boxes and add additional controls based on the values.
isPostBack - This is true when the page is posted back again.... check for u r values here and load addl controls here...
not isPostback - means loading for the first time.....
use response.redirect to send the user to another URL.....
Use postback for the same url...
check this
http://www.devx.com/dotnet
http://www.takempis.com/
http://www.macromedia.com/
http://www.dotnetjohn.com/
If you wish to keep state across pages, then you alternative is to use the Session Object.
The ViewState Object only maintains state across posts to the same page. Not to the other pages.
Th other thing you might consider is to serialise the ViewState to the database and use it for the other page (advanced level of knowledge)
What you are better off doing is to use a user control in your aspx page, create some public properties and then in a postback, the user control's properties are populated via code, and made visible. Your user control can contain any server controls etc etc,
What you have described is best achieved using UserControls.
Here u don't actuall need to use Session...as it will amke the server relatively slow...
the beauty of asp.net is the postback and maintaining state.... avoid using sessions....
Create the controls selectively on every postback
Or u can also do like this
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If viewstate("Add") = "True" Then ''''Check if the drop down list is selectd
AddLinkButtondata()
End If
End Sub
Private Sub selectbox_selectIndexChang
viewstate("Add") = True ''' Create a viewstate telling that user has selected the drop donw list
End Sub
pls check the syntax for the SUBRoutines declaration... was impromptu
devplayer, i only want to maintain state across the same page, BUT i also want to add a querystring to the post.
When the user selects a new value from the dropdown list the page is posted and the new value is retained, this works no problem.
Lets presume i also have a linkbutton which has a value of 5, what i want is to click on the linkbutton for it to submit the page and also the value 5 (to the same page)
How is that done without doing a Response.Redirect
???
you don't need to worry about the querystring,
if a user types in for e.g. http://myserver.com/Page.a
then in the server control form's action attribute, the QueryString is passed thru:
<form action="Page.aspx?id=4" method="post" >
</form>
<%@Page Language="VB"%>
<script language="VB" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsPostBack Then
Dim value As Integer= Convert.ToInt32(LinkButton
Literal1.Text = String.Format("<div>Value Is {0}</div>",value) & _
String.Format("<div>Value Of List Is {0}</div>",CountryList.Sel
End If
End Sub
</script>
<html>
<head>
<title>Posting Values</title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:DropDownList id="CountryList" runat="server">
<asp:ListItem Value="-1">Please Select</asp:ListItem>
<asp:ListItem Value="7">USA</asp:ListIte
<asp:ListItem Value="10">UK</asp:ListIte
</asp:DropDownList>
<asp:LinkButton id="LinkButton1" runat="server" CommandArgument="5">LinkBu
<asp:Literal id="Literal1" runat="server" />
</form>
</body>
</html>
http://www.dotnetjunkies.c
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If ViewState("Cat") = True Then
LoadCategory()
End If
If ViewState("Menu") = True Then
LoadOptionalItem()
End If
end if
Private Sub LoadCategory()
Dim i As Int32
Dim MenuButton As LinkButton
For i = 0 To 10
MenuButton = New LinkButton()
MenuButton.Width = Unit.Pixel(230)
MenuButton.CausesValidatio
MenuButton.Text = i
MenuButton.CommandArgument
AddHandler MenuButton.Click, AddressOf Me.LoadMenu
PlaceHolder.Controls.Add(M
Next
End Sub
Private Sub LoadMenu(ByVal sender As System.Object, ByVal e As System.EventArgs)
response.write(sender.Comm
ViewState("Menu") = True
End Sub
Private Sub dropdown_SelectedIndexChan
ViewState("Cat") = True
End Sub
<%@Page Language="VB"%>
<script language="VB" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsPostBack Then
Dim args As String = LinkButton1.CommandArgumen
Dim values As String () = args.Split(Convert.ToChar(
Dim id As Integer = Convert.ToInt32(values(0))
Dim report As String = values(1)
Literal1.Text = String.Format("<div>Id Is {0}</div><div>Report Is {1}</div>",id,report)
End If
End Sub
</script>
<html>
<head>
<title>Posting Values</title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:DropDownList id="CountryList" runat="server">
<asp:ListItem Value="-1">Please Select</asp:ListItem>
<asp:ListItem Value="7">USA</asp:ListIte
<asp:ListItem Value="10">UK</asp:ListIte
</asp:DropDownList>
<asp:LinkButton id="LinkButton1" runat="server" CommandArgument="5|Schedul
<asp:Literal id="Literal1" runat="server" />
</form>
</body>
</html>
This is the code: (all in page load)
string strConn = ConfigurationSettings.AppS
Cnn = new SqlConnection(strConn);
if (!Page.IsPostBack)
{
ds = new DataSet();
try
{
Cnn.Open();
// fill projects
da = new SqlDataAdapter("SELECT tblProjects.ProjectName, tblProjects.ProjectID, tblProjects.ProjectStatusR
da.Fill(ds, "Results");
lstProject.DataSource = ds.Tables[0].DefaultView;
lstProject.DataTextField = "ProjectName";
lstProject.DataValueField = "ProjectID";
lstProject.DataBind();
lstProject.Items.Insert(0,
}
finally
{
Cnn.Close();
}
}
if (Request["lstProject"] != null)
{
if (Request["lstProject"].ToS
{
//hard code in task name
lstTask.Items.Clear();
lstTask.Items.Insert(0,new
lstTask.Items.Insert(1,new
if (Request["lstTask"] != null)
{
if (Request["lstTask"] != "Select task")
{
// we have a task name....
Cnn.Open();
Cmd = new SqlCommand("sp_tm_ProjectS
SqlDataReader reader = Cmd.ExecuteReader();
while (reader.Read() && Request["lstProject"].ToSt
{
if (reader["Title"].ToString(
{
lblSchd.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblInProgress.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblCompleted.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblSchdBeforeToday.Text = reader[1].ToString();
}
}
}
}
}
}
ah c#, a fellow with good taste!!!
using System.Text;
void Page_Load(object sender, EventArgs e)
{
string strConn = ConfigurationSettings.AppS
Cnn = new SqlConnection(strConn);
if (! Page.IsPostBack)
{
ds = new DataSet();
try
{
Cnn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT tblProjects.ProjectName, tblProjects.ProjectID, tblProjects.ProjectStatusR
sb.Append("INNER JOIN tblClients ON tblCustomerUsers.ClientID = tblClients.ClientID INNER JOIN tblProjects ON tblClients.ClientID = tblProjects.ClientID ");
sb.AppendFormat("WHERE tblCustomerUsers.CustomerU
da = new SqlDataAdapter(sb.ToString
da.Fill(ds, "Results");
lstProject.DataSource = ds.Tables[0].DefaultView;
lstProject.DataTextField = "ProjectName";
lstProject.DataValueField = "ProjectID";
lstProject.DataBind();
lstProject.Items.Insert(0,
}
finally
{
Cnn.Close();
}
}
else if (lstProject.SelectedIndex != 0)
{
lstTask.Items.Clear();
lstTask.Items.Insert(0,new
lstTask.Items.Insert(1,new
/// your code accordingly
}
}
amendment
=========
else if (lstProject.SelectedIndex > 0)
{
lstTask.Items.Clear();
lstTask.Items.Insert(0,new
lstTask.Items.Insert(1,new
/// your code accordingly
}
next, actually this architecture is not a good idea. becos every post will cause the lstProject to be repopulated whenever
so you have 2 options, either use the SelectedIndexChanged event to autopostback whenever the lstProject is changed or use ViewState:
if (! Page.IsPostBack)
{
/// initial population
}
else
{
string value = lstProject.Items.Count == 0 ? String.Empty : lstProject.SelectedItem.Va
string persistedValue = ViewState["ProjectId"] == null ? String.Empty : (string)ViewState["Project
if (value.Length == 0)
{
/// do whatever to re-initialise the control
lstTask.Items.Clear();
}
else if (value.CompareTo(persisted
{
/// changes have been detected,
/// re-load the lstTask control
}
/// pass in the new value.
ViewState["ProjectId"] = value;
}
check this
http://aspnet.4guysfromrol
how to load data and bind data
Blimey - can't keep up with you chaps :)
So far i have this which is fine and it does work apart from if i click on the link button it still resets the drop down lists
string strConn = ConfigurationSettings.AppS
Cnn = new SqlConnection(strConn);
if (! Page.IsPostBack)
{
ds = new DataSet();
try
{
Cnn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT tblProjects.ProjectName, tblProjects.ProjectID, tblProjects.ProjectStatusR
sb.Append("INNER JOIN tblClients ON tblCustomerUsers.ClientID = tblClients.ClientID INNER JOIN tblProjects ON tblClients.ClientID = tblProjects.ClientID ");
sb.AppendFormat("WHERE tblCustomerUsers.CustomerU
da = new SqlDataAdapter(sb.ToString
da.Fill(ds, "Results");
lstProject.DataSource = ds.Tables[0].DefaultView;
lstProject.DataTextField = "ProjectName";
lstProject.DataValueField = "ProjectID";
lstProject.DataBind();
lstProject.Items.Insert(0,
}
finally
{
Cnn.Close();
}
}
else if (lstProject.SelectedIndex > 0 && Request["lstTask"] == null)
{
lstTask.Items.Clear();
lstTask.Items.Insert(0,new
lstTask.Items.Insert(1,new
}
else if (lstTask.SelectedIndex > 0)
{
// we have a task name....
Cnn.Open();
Cmd = new SqlCommand("sp_tm_ProjectS
SqlDataReader reader = Cmd.ExecuteReader();
while (reader.Read() && Request["lstProject"].ToSt
{
if (reader["Title"].ToString(
{
lblSchd.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblInProgress.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblCompleted.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblSchdBeforeToday.Text = reader[1].ToString();
}
}
}
Best i can do is post ALL the code..........
Just to let you know i'm currently using HyperLink controls instead of LinkButtons at the moment, set to change if i can get it working...
Here goes
//////////////////////////
<%@ Page language="c#" Codebehind="ProjectStatusR
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ProjectStatusRpt</t
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScri
<meta name="vs_targetSchema" content="http://schemas.mi
</HEAD>
<body MS_POSITIONING="GridLayout
<form id="Form1" method="post" runat="server">
<asp:label id="lblStockReport" style="Z-INDEX: 104; LEFT: 15px; POSITION: absolute; TOP: 10px"
runat="server" Font-Bold="True" Font-Size="Small" Font-Names="Tahoma" Width="249px">Project Status</asp:label>
<asp:Label id="Label8" style="Z-INDEX: 110; LEFT: 10px; POSITION: absolute; TOP: 225px" runat="server"
Font-Bold="True" Font-Size="XX-Small" Font-Names="Tahoma" Width="705px" BackColor="Silver"
ForeColor="#C00000" Height="5px"></asp:Label>
<asp:DropDownList id="lstTask" style="Z-INDEX: 109; LEFT: 450px; POSITION: absolute; TOP: 70px" runat="server"
Font-Names="Tahoma" Width="235px" AutoPostBack="True"></asp:
<asp:label id="Label7" style="Z-INDEX: 108; LEFT: 400px; POSITION: absolute; TOP: 75px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="50px">Task:</asp:la
<asp:label id="Label3" style="Z-INDEX: 106; LEFT: 15px; POSITION: absolute; TOP: 75px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="50px">Project:</asp
<asp:Label id="Label6" style="Z-INDEX: 101; LEFT: 11px; POSITION: absolute; TOP: 38px" runat="server"
Font-Bold="True" Font-Size="X-Small" Font-Names="Tahoma" Width="705px" BackColor="Silver"
ForeColor="#C00000"></asp:
<asp:Button id="btnBack" style="Z-INDEX: 103; LEFT: 563px; POSITION: absolute; TOP: 10px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" CausesValidation="False" Text="Back"></asp:Button>
<asp:Button id="btnSignOut" style="Z-INDEX: 102; LEFT: 611px; POSITION: absolute; TOP: 10px"
runat="server" Font-Size="X-Small" Font-Names="Tahoma" CausesValidation="False" Text="Sign Out"></asp:Button>
<DIV style="BORDER-RIGHT: navy thin solid; BORDER-TOP: navy thin solid; Z-INDEX: 105; LEFT: 10px; BORDER-LEFT: navy thin solid; WIDTH: 705px; BORDER-BOTTOM: navy thin solid; POSITION: absolute; TOP: 105px; HEIGHT: 100px"
ms_positioning="GridLayout
<asp:label id="Label5" style="Z-INDEX: 100; LEFT: 100px; POSITION: absolute; TOP: 70px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="100px">Jobs Completed:</asp:label>
<asp:label id="Label4" style="Z-INDEX: 101; LEFT: 125px; POSITION: absolute; TOP: 50px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="70px">In Progress:</asp:label>
<asp:label id="Label1" style="Z-INDEX: 102; LEFT: 20px; POSITION: absolute; TOP: 30px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="180px">Scheduled Jobs Before Today:</asp:label>
<asp:label id="Label2" style="Z-INDEX: 103; LEFT: 100px; POSITION: absolute; TOP: 10px" runat="server"
Font-Size="X-Small" Font-Names="Tahoma" Width="95px">Scheduled Jobs:</asp:label>
<asp:HyperLink id="lblSchd" style="Z-INDEX: 104; LEFT: 215px; POSITION: absolute; TOP: 10px" runat="server"
Font-Bold="True" Font-Size="X-Small" Font-Names="Tahoma" Width="105px" NavigateUrl='" "'></asp:HyperLink>
<asp:HyperLink id="lblSchdBeforeToday" style="Z-INDEX: 115; LEFT: 215px; POSITION: absolute; TOP: 30px"
runat="server" Font-Bold="True" Font-Size="X-Small" Font-Names="Tahoma" NavigateUrl='" "'></asp:HyperLink>
<asp:HyperLink id="lblInProgress" style="Z-INDEX: 115; LEFT: 215px; POSITION: absolute; TOP: 50px"
runat="server" Font-Bold="True" Font-Size="X-Small" Font-Names="Tahoma" NavigateUrl='" "'></asp:HyperLink>
<asp:HyperLink id="lblCompleted" style="Z-INDEX: 114; LEFT: 215px; POSITION: absolute; TOP: 70px"
runat="server" Font-Bold="True" Font-Size="X-Small" Font-Names="Tahoma" NavigateUrl='" "'></asp:HyperLink></DIV>
<asp:DropDownList id="lstProject" style="Z-INDEX: 107; LEFT: 75px; POSITION: absolute; TOP: 70px"
runat="server" Font-Names="Tahoma" Width="290px" AutoPostBack="True"></asp:
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 111; LEFT: 210px; POSITION: absolute; TOP: 255px"
runat="server" CommandArgument="scedule report">LinkButton</asp:Li
<asp:Label id="Label9" style="Z-INDEX: 112; LEFT: 245px; POSITION: absolute; TOP: 320px" runat="server">Label</asp:
</form>
</body>
</HTML>
//////////////////////// CODE
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace EcsStock
{
/// <summary>
/// Summary description for ProjectStatusRpt.
/// </summary>
public class ProjectStatusRpt : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Data.SqlClient.SqlC
protected System.Data.SqlClient.SqlC
protected System.Data.SqlClient.SqlD
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Data.DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
string strConn = ConfigurationSettings.AppS
Cnn = new SqlConnection(strConn);
if (! Page.IsPostBack)
{
ds = new DataSet();
try
{
Cnn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT tblProjects.ProjectName, tblProjects.ProjectID, tblProjects.ProjectStatusR
sb.Append("INNER JOIN tblClients ON tblCustomerUsers.ClientID = tblClients.ClientID INNER JOIN tblProjects ON tblClients.ClientID = tblProjects.ClientID ");
sb.AppendFormat("WHERE tblCustomerUsers.CustomerU
da = new SqlDataAdapter(sb.ToString
da.Fill(ds, "Results");
lstProject.DataSource = ds.Tables[0].DefaultView;
lstProject.DataTextField = "ProjectName";
lstProject.DataValueField = "ProjectID";
lstProject.DataBind();
lstProject.Items.Insert(0,
}
finally
{
Cnn.Close();
}
}
else if (lstProject.SelectedIndex > 0 && Request["lstTask"] == null)
{
lstTask.Items.Clear();
lstTask.Items.Insert(0,new
lstTask.Items.Insert(1,new
}
else if (lstTask.SelectedIndex > 0)
{
// we have a task name....
Cnn.Open();
Cmd = new SqlCommand("sp_tm_ProjectS
SqlDataReader reader = Cmd.ExecuteReader();
while (reader.Read() && Request["lstProject"].ToSt
{
if (reader["Title"].ToString(
{
lblSchd.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblInProgress.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblCompleted.Text = reader[1].ToString();
}
else if (reader["Title"].ToString(
{
lblSchdBeforeToday.Text = reader[1].ToString();
}
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.S
this.Load += new System.EventHandler(this.P
}
#endregion
private void SchdClick(object sender, System.EventArgs e)
{
Server.Transfer("ProjectSt
//Response.Redirect ("ProjectStatusRpt.aspx?bl
}
}
}
private void SchdClick(object sender, System.EventArgs e)
{
// You dont transfer to the same page
// Server.Transfer("ProjectSt
//Response.Redirect ("ProjectStatusRpt.aspx?bl
}
this calls the SchdClick Remove it
=================
/// this.LinkButton1.Click += new System.EventHandler(this.S
Right OK.....this what the page does.
User must first select a project from a list, once a project has been selected the user must then select a task, once the task has been selected a query is then ran against the database to return some stats on that task, so various labels (linkbuttons) are filled with numbers (4 linkbuttons i think)
The user can then click on those numbers to view a report (on the same page, which is just a bound datagrid) of what that number actually means.
Now, the report at the bottom of the page will have exactly the same layout but depending on which number you click on will determin the parameters of the query.
That why i want to attach a querystring to each linkbutton so i can say: samepage.aspx?report=sched
So when the page is submitted i can pass that value to a stored procedure so it knows what data to return.
Does it make more sense now?
PS. Food was lovely
in classic ASP, you'd have to rebuild the list items.
in ASP.NET, you do not if you postback and viewstate is enabled.
That is the one of the fundamental difference between classic ASP and ASP.NET
Next, you do not need to pass a querystring in most cases since in Page.Load, you can see at the server-side what is the state of the server controls and decide on what to do etc etc.
All I am saying is to forget about classic ASP (I know it's hard as I myself had to go thru this learning phase) and focus on what ASP.NET actually can do.
All the best!
Business Accounts
Answer for Membership
by: nishikanthPosted on 2004-03-09 at 01:15:40ID: 10548507
make the enable viewstate to true for the controls....