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
Monday, March 26, 2012
pass the web value from asp.net to asp by url
hi all,
i have two web files, one is *.aspx and the other is *.asp
i need pass textbox.text value from *.aspx to *.asp
so i can process the value(ex:string ) in *.asp
i pass the value like this
<a href="http://links.10026.com/?link=iflowall.asp?case_no=<%=(case.Text)%>&bank_no=<%=(bank_no.Text)%>">connect</a></p>
and receive from *.aspx like this
<td><%= Request.QueryString("case_no") %>於<%= Request.QueryString("bank_name") %></td>
in *.asp, it can't decode the string "bank_name" so the value is null why?
if the string is about number(ex:1234) it`s ok, but wrong when the string is chinses word(ex:中文)
Yes you got the Query string wrong. As in your .aspx constructed url, the query string parameter is bank_no not bank_name
Pass UserID in hidden field
I have a hidden textbox with the userID session in it, but the UserID field is int,4
and the textbox only pases text (I think). So I get an input string error.
am I doing this right?
All help apreciated.
Thanks,
JBIf you've got in in session, you shouldn't need to pass it in from the hidden field.
Either way:
Convert.ToInt32(Session.Item("UserID"))
or
Convert.ToInt32(hiddedTextBox.Text)
Regards,
Xander
Thanks Xanderno,
I've been at this all afternoon with no luck.
I get "input string was not in a correct format"
The parameter code is
myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = Convert.ToInt32(UserID)
and the hidden text box is
<asp:TextBox id="UserID" runat="server" text='<% session(UserID")%>' Visible="False"
The database types are correct - any ideas?
Much appreciated,
JB
That means (likely) that UserID is blank. Which, (Ah ha!) looking at your code, it would be.
First, he session variable needs to be written to the html in order to appear in textbox or hidden field.
You could do this with either the full Response.Write: Response.Write(Session.Item("UserID"))
Or with the shorthand equals sign: =Session.Item("UserID")
Beyond that, since you're using an ASP.Net TextBox, with the visibility property set to false,
even if the session variable *was* being response.written, it still wouldn't work, because that textbox wouldn't be sent to the browser!
So, let's try something like this instead:
<input id="UserID" type="hidden" value="<% =Session.Item("UserID") %>" /
And that should fix you up.
Xander
Thanks Xander,
I'm getting closer but it now passes a "0" to the DB
If I use 'input' with an id of UserName instead of a textbox then in code behind it does not get declared.
So I declare it by
Dim UserID as Int32
Does this mean I have already converted UserID and so do not have to use Convert.Toint32 (UserID.text)
but then in my parameters
myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = UserID.what goes here? I only get a few choices and was hoping for 'value'
I'm sure this is wrong!
Thanks again.
JB
This is a totaly wrong assumption you make there..
Look at:
http://www.asp.net/Tutorials/quickstart.aspx
And find yourself a few samples to play with. It will help you to get the ideas one-by-one..
For the code above:
Dim UserID as Int32
//set the user id to 1
UserID = 1myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
//set my param to the value of the userid var.
myCommand.Parameters("@.MemberID").Value = UserID
Actually, you're pretty close already.
First off let's change the tag a bit.
<input name="UserID" type="hidden" value="<% =Session.Item("UserID") %>" /
Now, when you pull up the page, if you do a View | Source, you should see that tag on your page, with your UserID set as the value. If it doesn't have a value, then there is something wrong with your session variable that you need to hunt down.
Next, in you're code, you'll have this:
Dim UserID as Int32
'Now we have the variable, but we still have to assign it a value.
UserID = Convert.ToInt32(Request.Form("UserID"))
'Now that we have the UserID in the variable, pass it to the command object.
myCommand.Parameters.Add(New SqlParameter("@.MemberID", SqlDbType.Int, 4))
myCommand.Parameters("@.MemberID").Value = UserID
Thanks,
I have looked through all the quickstarts but canot find this.
All my other parameters pass properly except this one
The value must equal the hidden input session variable from the form but it just passes a "0"
Thanks for the help so far.
JB
Xanderno
That's fixed it!!
Thanks to you and the others for taking time to help me.
Cheers,
JB
Pass Value from Code Behind to TextBox
Hi, I have a very basic question (I am a beginner)
How do I pass a value from the Code Behind to, say, a text box?
I've got this on my aspx page:
<asp:TextBoxID="TextBox1"runat="server"Text=strTestOnTextChanged="TextBox1_TextChanged"></asp:TextBox>All I want to do is pass the value of strTest from the Code Behind to display in the Text Box. This is what I have in the code behind:
using System;
using System.Data;
using System.Configuration;
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;
publicpartialclass_Default : System.Web.UI.Page{
protectedvoid Page_Load(object sender,EventArgs e){
String strText;strText ="Hello";}
}
How can I get this to work?
It's very simple just try this
protectedvoid Page_Load(object sender,EventArgs e){
String strText;strText ="Hello";
TextBox1.Text =strText;}
You are right...it is very simple;
Thanks for the help...and Iapologize for the very basic question;
don't forget to mark as answerd the post if it is.
Greets
Whenever you write something in PageLoad event, it should be executed with every postback event... So use IsPostBack event of page to identify page was post back or not ...
Wednesday, March 21, 2012
Passing a string to a client side function
1. Put the xml string in a textbox control (and set the display style to
none using css stylesheet) then simply reference the value of your textbox
control, to get the XML string.
2. (Probably the better one) Add a hidden input field to your form, set the
runat attribute to server and then simply assign your XML string as the
value of the hidden input field from within your code behind. Using this
method the encoding is taken care of for you as the whole lot will be html
encoded. (I pretty sure this should then work fine, but I may be wrong!!)
Hope one of the above works for you.
Matt
http://www.3internet.com
"Jorell" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:70A4100B-D764-4FEA-B111-A5B9AE9E6529@dotnet.itags.org.microsoft.com...
> Hi everyone,
> I have an XML that I create on the fly. I need to pass this to a
Javascript function and I do it in this way:
> Page.RegisterStartupScript("Test", "<script
language=""javascript"">printHidden('" & XML & "')</script>")
> This works however if you encase the XML in single quotes then there could
possibly a terminating ' in the XML and on the other hand if you encase the
XML in double quotes and there is a single " in the XML...this becomes the
terminator and the client side throws an error: Unterminated string constant
> The user enters the information the XML is built with and we must allow
them the ability to enter these any characters.
> What I need to do is encode the string server side before it is passed and
then unencode it on the client.
> I have attempted to use Regex.Escape on the Server then unescape() on the
client with no luck.
> Any ideas/suggestions would be great! Thank you
> JorellJorell,
If you use the option of outputting your text to a form variable you should
not need to use escape characters at all. If you just leave the string
exactly as you want it to appear and output it to the form variables, when
you retrieve the value you should see exactly what you are looking for. Or
is there something that I am missing as to why do you need to put escape
characters in?
Matt
http://www.3internet.com
"Jorell" <anonymous@.discussions.microsoft.com> wrote in message
news:B8A31C70-DF4A-49D1-AB87-CA7E3AF4487D@.microsoft.com...
> Hi there. Thank you for the suggestion. I tried this as I figured the same
however the value, innerText and innerHtml etc properties all return the
HTML but with the escape characters ie. \"
> This still causes and issue and without actually doing a loop to look for
escape characters and replacing them...but there is a possibly of not
catching all of them.
> Essentially I am just writing this HTML to a div using the document.write
method and then printing the div using execWB.
> Any ideas would be great thank you!
> Jorell
Hi there Matt
I actually don't want the characters in the string. What I have is Complex HTML that I have created in a server function
I have a js function which needs to take that HTML and write it to the Body of an IFrame that it creates. ( that can't be changed) So basically what I need to be able to do is end up with a variable inside that js function which is straight HTML no escapes.
Mostly the problem is if you use textboxes of any sort...the innerHtml, innerText, Text, or value fields always return the value with escape charaters when accessing them from the client. I am not sure why
So no matter what I put the HTML in, it adds escape chars
Any ideas? I appologize if it is unclear! Thank yo
Jorell
Friday, March 16, 2012
Passing an '&' as part of the parameter's value in a query string
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?
Thanks.
--
moondaddy@dotnet.itags.org.newsgroup.nospammoondaddy wrote:
Quote:
Originally Posted by
I have a situation where I will collect text from a textbox and pass that
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?
>
Thanks.
Use the encodeURIComponent function to encode the value that you put in
the URL.
You should do this for any values that you put in an URL that might
contain characters that needs encoding.
--
Gran Andersson
_____
http://www.guffa.com
On Sep 12, 9:19 am, "moondaddy" <moonda...@.newsgroup.nospamwrote:
Quote:
Originally Posted by
I have a situation where I will collect text from a textbox and pass that
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?
>
& = %26
Alexey Smirnov wrote:
Quote:
Originally Posted by
On Sep 12, 9:19 am, "moondaddy" <moonda...@.newsgroup.nospamwrote:
Quote:
Originally Posted by
>I have a situation where I will collect text from a textbox and pass that
>text as one of the parameters in another pages query string. the problem is
>that if the user types in a '&' as part of the text message, the page being
>called thinks the '&' is parsing out another parameter. Using JavaScript,
>what should I do to pass the entire message in the query string?
>>
>
& = %26
>
That is correct, but that only solves the problem with that specific
character. There are several other characters that are not valid in a
value in a query string, like spaces for example.
--
Gran Andersson
_____
http://www.guffa.com
Thanks!
encodeURIComponent worked perfect.
"Gran Andersson" <guffa@.guffa.comwrote in message
news:uFyGU9Q9HHA.980@.TK2MSFTNGP06.phx.gbl...
Quote:
Originally Posted by
moondaddy wrote:
Quote:
Originally Posted by
>I have a situation where I will collect text from a textbox and pass that
>text as one of the parameters in another pages query string. the problem
>is that if the user types in a '&' as part of the text message, the page
>being called thinks the '&' is parsing out another parameter. Using
>JavaScript, what should I do to pass the entire message in the query
>string?
>>
>Thanks.
>
Use the encodeURIComponent function to encode the value that you put in
the URL.
>
You should do this for any values that you put in an URL that might
contain characters that needs encoding.
>
--
Gran Andersson
_____
http://www.guffa.com