By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,796 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,796 IT Pros & Developers. It's quick & easy.

pass multiple values from one aspx page to another

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.