473,416 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

pass multiple values from one aspx page to another

I have to pass values from one aspx page to another. The controls I
have in the first page are:
a textbox, 3 drop down lists, and 2 check boxes, and a submit button.
It is a search page, and the users need not enter values in all the
controls. they can leave the textbox blank, and select values from one
drop down, or any other combinations.
I am trying to pass values with the help of session variables. But I
have multiple if else statements like:

if (ddlCategories.SelectedItem.Value == "")
{
Session["CatID"] = "";
}
else
{
Session["CatID"] = ddlCategories.SelectedItem.Value;
}

The value in the second drop down depends on the value in the first,
and the value in the third drop down depends on the second drop down.
So, its giving me an error saying "Object reference not set to an
instance of the object" if I don't select any value from the first drop
down .(same is the case for the second drop down).

What is the best way yo do this? without all the if else statements,
and may be without using the session variables.
any suggestions?

Nov 19 '05 #1
2 12341
Regarding your object reference error...

You could check for the existance of a selected item first, before trying to
read its value, e.g.

Session["CatID"] = "";
if (ddlCategories.SelectedItem != null)
Session["CatID"] = ddlCategories.SelectedItem.Value;

However I think that there may be a shortcut approach using
ddlCategories.SelectedValue instead. I haven't tested this property without
a selected item, but it will probably return null rather than an exception.
Worth exploring. If it works for you, you may be able to simplify the
constructs to, e.g.;

Session["CatID"] = ddlCategories.SelectedItem.Value;

.... with no safety check. If you need to treat "" and null the same (and
have "" as values in your list), you can write a helper function as in;

Session["CatID"] = MakeEmptyStringNull (ddlCategories.SelectedItem.Value);

Regarding your value-passing approach, session variables work fine but makes
the receiving page limited. I prefer to put essentially all navigation
rules into the Url so that users can bookmark search pages reliably, forward
the Url to friends, etc. The cleanest way I've found to do this is to
construct a class that manages the navigation to the target page, and
formulates the Url based on properties, e.g.

class SearchPageUrl
{
public string CatID;
public string Option1;
public string Option2;

/// Generate the Url
public override string ToString ()
{
return String.Format (
"/search/results.aspx?CatID={0}&Option1={1}&Option2={2}",
CatID, Option1, Option2
);
}

/// Determine whether the necessary properties have been set to generate
a valid Url
public bool IsValidUrl ()
{
return true;
}
}

in your onclick;

SearchPageUrl sup = new SearchPageUrl ();
sup.CatID = ddlCategories.SelectedValue;
sup.Option1 = ... ;
sup.Option2 = ... ;

if (sup.IsValidUrl())
Response.Redirect (sup.ToString ());
else
// show error

In your receiving page, you can simplify the querystring processing through
page-level properties, e.g.;

private bool HasCatID
{
get { return Request["CatID"] != null; }
}
private int CatID
{
get { return Int32.Parse (Request["CatID"]); }
}

Which keeps the code in your page clean, and centralizes the parsing of your
querystring propeties.

Clearly this is a bit more code than the Session var approach but it's worth
considering. Most users expect the pages to be bookmarkable.

/// M
"macyp" <vr******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I have to pass values from one aspx page to another. The controls I
have in the first page are:
a textbox, 3 drop down lists, and 2 check boxes, and a submit button.
It is a search page, and the users need not enter values in all the
controls. they can leave the textbox blank, and select values from one
drop down, or any other combinations.
I am trying to pass values with the help of session variables. But I
have multiple if else statements like:

if (ddlCategories.SelectedItem.Value == "")
{
Session["CatID"] = "";
}
else
{
Session["CatID"] = ddlCategories.SelectedItem.Value;
}

The value in the second drop down depends on the value in the first,
and the value in the third drop down depends on the second drop down.
So, its giving me an error saying "Object reference not set to an
instance of the object" if I don't select any value from the first drop
down .(same is the case for the second drop down).

What is the best way yo do this? without all the if else statements,
and may be without using the session variables.
any suggestions?

Nov 19 '05 #2
Hi Macyp,

Checking that the SelectedIndex DropDownList property is not equal to
-1 before retrieving the SelectedItem.Value property should solve your
"Object referenct" problem. Sample:

if (ddlCategories.SelectedIndex != -1)
Tod Birdsall
http://tod1d.blogspot.com

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: tom | last post by:
Hi Experts, I want to pass the selectedDate value from my calender form to another web form or a web user control. Could you please show me how to do this? Thanks in advance.
8
by: darrel | last post by:
I'm still trying to fully understand how best to pass variables between pages/usercontrols/each other. On a current site I've done, I've had one userControl do the logic and set the variable,...
5
by: Apollo0130 | last post by:
hi, i want to pass a parameter (a string) between two forms. i have a Webform A (default.aspx) with a button and a Webform B (detail.aspx). i want to click the button and open the webform B with...
9
by: Jamie | last post by:
I am receiving an Invalid ViewState error after posting back to the same page twice. Consistently the error occurs after the second postback and not after the first. I have looked into creating...
1
by: samir dsf | last post by:
hi i can send and retrieve a single value. but now i have around 3-4 values which i want to pass, from a link and receive in another page. here is how i am currently doing: Dim urlParams As...
2
by: c676228 | last post by:
Hi, This is my first time to post asp.net question on this forum. I have a question for "How to pass the first form value to the next form" I have enrollinfo.aspx form which look like as follow:...
3
by: Martin | last post by:
I have a calendar form that sends the inputted date back to a text box . Using the GET method, I would like to pass the date as a variable to another asp page. How do I do this? Thanks.
2
by: David Thielen | last post by:
Hi; I have a IHttpHandler that is generating a page. If it throws an exception I want to put up a nice page that is based on my master page so it looks good. How can I redirect from my...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.