Thursday, March 29, 2012
pass multiple values from one aspx page to another
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?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" <vradhika@.gmail.com> wrote in message
news:1106755384.274484.211140@.c13g2000cwb.googlegroups.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?
>
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
pass multiple values from one aspx page to another
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?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" <vradhika@.gmail.com> wrote in message
news:1106755384.274484.211140@.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?
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
Saturday, March 24, 2012
pass variable between webpage
pages, and user controls , any examples
-
dolla
----------------------
Posted via http://www.codecomments.co
----------------------Hi Dollar,
There are various ways in which ASP.NET helps you to pass information
from one page to another, it basically depends on your application.
In general, the applications use Session state to pass variables.
You can refer to the following link for complete details:
[State Management Recommendations]
http://msdn.microsoft.com/library/d...StateOption.asp
As for user controls, you can have public properties for the user control
which can be accessed by any page and also they will retain their values in
between
server round trips.
HTH
Mona[Grapecity]
"dollar" <dollar.1pp11p@.mail.codecomments.com> wrote in message
news:dollar.1pp11p@.mail.codecomments.com...
> I want to ask what is the best way to pass variable between asp.net web
> pages, and user controls , any examples?
>
> --
> dollar
> ----------------------
> Posted via http://www.codecomments.com
> ----------------------
pass variable between webpage
s, and user controls , any examples?Hi Dollar,
There are various ways in which ASP.NET helps you to pass information
from one page to another, it basically depends on your application.
In general, the applications use Session state to pass variables.
You can refer to the following link for complete details:
[State Management Recommendations]
http://msdn.microsoft.com/library/d...StateOption.asp
As for user controls, you can have public properties for the user control
which can be accessed by any page and also they will retain their values in
between
server round trips.
HTH
Mona[Grapecity]
"dollar" <dollar.1pp11p@.mail.codecomments.com> wrote in message
news:dollar.1pp11p@.mail.codecomments.com...
> I want to ask what is the best way to pass variable between asp.net web
> pages, and user controls , any examples?
>
> --
> dollar
> ---
> Posted via http://www.codecomments.com
> ---
>
Pass variables to header control (.asxc web user control)?
Generically, I have a series of pages that all use the same header and footer controls. I want each page to have it's own values for a few variables, and the header page needs to recognize those values and change some html based on those values. The html to be changed is just class names on tds, I'm assuming for it to work that maybe I need to change that to something .net can access easier.
Specifically, I tried just making the variables in the pages's .aspx.vb files and writing them out in the header's .aspx file (right where they'd go in the control) but it's errors and tells me I need to declare the variables in the header control.Try this (in C#)
In your code behind for the Header control:
private string _YourVariable = "";public string YourVariable
{
set
{
_YourVariable = value.ToLower();
}
}
and in the HTML of the page containing the control:
<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere"></uc1:YourHeader >
Hope this helps,
Nic
Thank you! I will work on it. I'm using vb, so I'll let you know how it goes.
Question: does the header control line allow multiple variables or just one?
Ie,
<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere" YourVar1="SomeValueY" YourVar2="SomeValueX" YourVar3="SomeValueZ" ></uc1:YourHeader
As many as you want.
Good to hear I can have several variables there, thanks.
Ok, I've had no luck creating equivalent code in vb, yet it looks so straight forward... make a local variable, set it to "", then give the local variable the value of the 1st one... does that sound about right?
:?
Update: Success!
Used this link (http://www.kamalpatel.net/ConvertCSharp2VB.aspx), followed your suggestions, and it works! Now I understand better how to approach this in the future.
Thanks for your help. :)
Wednesday, March 21, 2012
Passing a UserControl as a parameter in ASP.NET 2
I have a few user controls which I would like to pass to a function which is not on the page the usercontrols have been placed in.
The function is in the App_Code directory so I can access it from anywhere in the project but I can't refer to the user control in the parameters of the function, therefore I can't access the usercontrol.
I'm not allowed to place the UserControl in the App_Code directory, so how can I create an instance of it?
Do you have control of the server where the app is going to be deployed ?ie is it for an in house development or are you going to upload it to a public server ?
i ask coz if you have control of the server one way around the problem is to
make a dll (class) and by putting the code in the class your program will be able to access the code
Mark
Thanks Mark, will do that by building a dll. Only thing is I'm surprised that there is no way to do this using the ASP.NET 2 development model - the App_Code directory was supposed to replace the procedure of building and referencing dll's.