I want to pass more than one variable to other web pages.
I try this code
string adi = TaskGridView.SelectedRow.Cells[3].Text;
string soyadi = TaskGridView.SelectedRow.Cells[3].Text;
Response.Redirect("Default2.aspx?adi=" + adi);
Response.Redirect("Default2.aspx?soyadi=" + adi);
but it doesnt work how can I do?
The safest way is to use String.Format() with Server.UrlEncode():
Response.Redirect(String.Format("Default2.aspx?adi={0}&soyadi={1}", Server.UrlEncode(adi), Server.UrlEncode(soyadi)));
This will ensure that the querystring values will not be broken if there is an ampersand (&) for example, in the value.
Then on Default2.aspx you can access the values like:
Server.UrlDecode(Request.QueryString["adi"]);
Concatenate them like this:
Response.Redirect("Default2.aspx?adi=" + adi + "&soyadi=" + soyadi);
When passing query string parameters, use the ? symbol just after the name of the page and if you want to add more than one parameter, use the & symbol to separate them
In the consuming page:
protected void Page_Load(object sender, EventArgs e)
{
var adi = this.Request.QueryString["adi"];
var soyadi = this.Request.QueryString["soyadi"];
}
You can also use the session to pass values from one page to another
Set the values in the page where you want to pass the values from in to a session
Session["adi"] = TaskGridView.SelectedRow.Cells[3].Text;
Session["soyadi"] = TaskGridView.SelectedRow.Cells[3].Text;
In the Page where you want to retrieve- You do like this..
string adi=(string)(Session["adi"]);
string soyadi=(string)(Session["soyadi"]);
You can as well pass values through ViewState or Session the diffrent with what you doing now is: People will not see anything in your url and have no idea what happend in backend. It's good when you passing some "topsecret" data ;P
Related
I've seen examples on here for saving one string to pass onto the next page. But, I haven't been able to find or figure out how to save multiple search parameters without requiring a variable for each search string, which is something I'd rather not do, as I have 12 search fields.
I've seen examples with saving your variables to a viewbag and then passing that when you go to the next page. That works for the one variable, but not for multiple.
Is there a more eloquent solution then to have to pass individual view bags for each variable?
Thanks!
You can use a class for it like this:
public class SearchField
{
public string Field1 { get; set; }
public int Field2 { get; set; }
// other fields
}
And save object to ViewBag like this:
SearchField objSearchField = new SearchField();
objSearchField.Field1="value1";
objSearchField.Field2 = 100;
// set other fields
ViewBag.SearchFields = objSearchField;
You can use viewbag or tempdata["stringid"]. tempdata might be a better choice here. What you will do is create an array of strings (or a list might be even better!) and store all your information in that array. Then you store it on tempdata. Something like this:
string[] URLs = new string[12];
//do stuff
Tempdata["URLs"] = URLs;
When you get to the page you need the URLs in again, either in the controller or in the view you will write:
string[] URLS = (string[])Tempdata["URLs"];
//do stuff
or
string[] URLS = Tempdata["URLs"] as string[];
Something like that should work. You should read up on the other ways to pass data in ASP MVC:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/
Passing an object array as TempData[] to view
I'm working on a project to develop a UWP app. I have implemented Sqlite database to store information about products.
I have a combobox in my page that displays the serial number of all the products. I've written the code for SelectionChanged event of the combobox as:
private void InvoiceSerial_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var a = conn.Query<Product>
("select brand from product where serial=?", InvoiceSerial.SelectedItem.ToString());
InvoiceBrand.Text = a.ToString();
}
But instead of displaying the brand name, I'm getting this:
System.Collections.Generic.List`1[MyProject.Inventory+Product]
InvoiceBrand is a textbox
Even if the result of an SQL query is exactly one row and one column, query functions in any language are still required to return a full table, to handle the general case where a resultant table has multiple items. The object you're looking for should be the first object in the list.
Your a variable is probably of type List<MyProject.Product>, and that's why calling a.ToString() returns the string representation of the type!
What you should probably be doing is retrieving the first element (just use a[0]) and then output the value of the "brand" field.
Ok so I got it to work thanks to #TimSchmelter
Now my code looks like this:
private void InvoiceSerial_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var a = conn.Query<Product>
("select brand from product where serial=?", InvoiceSerial.SelectedItem.ToString());
Product p = a.FirstOrDefault();
InvoiceBrand.Text = p.Brand;
}
I have three variables I'm trying to pass from one page to another: Two checboxlists (just the checked values) and one DateTime.
I'm getting the checked items like this (this is just for one of these checkboxlists which is called lstUsers):
string cblvalues = "";
foreach (ListItem cbitem in lstUsers.Items)
{
if (cbitem.Selected)
{
cblvalues += cbitem.Value + "-";
}
}
cblvalues = cblvalues.Trim('-');
Response.Redirect("Results.aspx?cblvalues=" + cblvalues);
How would I pass the other checkboxlist and the DateTime to "Results.aspx" as well? I've seen a few different examples such as Server.Transfer, and I'm not sure which one is correct here.
And for using them on the Results.aspx page, would I just do:
string cblvalues = Request.QueryString["cblvalues"];
You can put as many values as you like on the query string. (Though as query strings get very long the web server would eventually impose a limit.) Here you simply append one key/value pair:
Response.Redirect("Results.aspx?cblvalues=" + cblvalues);
Just use a & to separate additional key/value pairs:
Response.Redirect("Results.aspx?cblvalues=" + cblvalues + "&moreValue=" + moreValues);
If you do get to the point where the query string becomes absurdly long and you basically have a lot of data to pass to the next page, then you'd be looking at other ways of doing this. A simple alternative may be to store the values in session state, redirect the user, then pull the values from session state. Something as simple as this:
Session["cblvalues"] = cblvalues;
Session["moreValues"] = moreValues;
Response.Redirect("Results.aspx");
Then in Results.aspx you can get the values:
var cblValues = Session["cblvalues"];
// etc.
You might also clear the session values once you get them, if the session doesn't need to keep carrying them:
Session.Remove("cblvalues");
You can pass multiple values through query string by seperated them with a &
so your snippet will be like the following:
Let cblSecond be the second combobox then;
// Building first value here
foreach (ListItem cbitem in cblSecond.Items)
{
if (cbitem.Selected)
{
cblSecondValues += cbitem.Value + "-";
}
}
Response.Redirect("Results.aspx?cblvalues=" + cblvalues + "&cblSecondValues=" + cblSecondValues);
So that you can access them separately like this:
string cblvalues = Request.QueryString["cblvalues"];// gives you the first value
string cblSecondValues= Request.QueryString["cblSecondValues"];// gives you the second value
Response.Redirect(String.Format("Results.aspx?value1={0}&value2={1}&value3={2}", Value1, Value2, Value3));
If you really want to pass them using querystring then you ahouls include them as well
Response.Redirect("Results.aspx?cblvalues=" + cblvalues + "&cblvalues1=" + cblvalues + "&datetimemy=" + datetimevalue);
Form of a query string like below
http://server_name/path/aspxfile?field1=value1&field2=value2&field3=value3..&fieldn=valuen
As other answer mentioned, there are multiple ways like using Session or cookies or as well you can expose those control values as public properties and use then in next page using Page.PreviousPage property but the restriction is that you will have to use either of Server.Transfer() or Server.Execute() method instead of Response.Redirect.
I am facing one problem while storing a querystring value in a variable.
My url is something like shown below:
http://localhost:1372/testapp/default.aspx?Search=%family%
Below is what I tried:
string result = Request.QueryString["Search"].ToString();
When I tried to save the querystring search in variable its taking value as �mily% .
How can I get value of search parameter as family?
Thanks in advance.
The query string parameters have to get URL-encoded. The problem ist th % character which is used for URL encoding, so it has to get encoded itself.
'%' becomes '%25'
Which means, the whole URL becomes:
http://localhost:1372/testapp/default.aspx?Search=%25family%25
your can use HttpUtitlity.UrlEncode
In Parent Page Encode query string this way
public static sting Encode(string sData)
{
return HttpUtility.UrlEncode(sData);
}
Now pass this encoded query string to your parent page & on Parent page decode that query string by following function. I assume that encoded query string name is sData & its passed from parent page to child page
public static string Decode()
{
string dSData = HttpUtility.UrlDecode(Request.QueryString["sData"].ToString());
return dsData;
}
I think you have idea about how to pass query string from parent to child page
try this solution for more .
hi guy's here is one problem regarding asp.net state managenment.
I want to store three values into single viewstate.Is it possible to store in single or i will go for three viewstate variables.
the basic need is that,I am using gridview rowcommand event for finding three values.
and i wanted to use these values in button_click event.it is directely not possible so i prefer viewstate.
if any other way to do this you can post.I am new in .net development so please share some knowledge of you.
You can make a class, and mark it with a Serializable attribute. Then make a list instance of that class and store it as 1 item in the viewstate. This is when you have a lot of values to store. However yours is a simple case I think:
[Serializable()]
class SomeData
{
public string Value1 {get; set;}
public string Value2 {get; set;}
public string Value3 {get; set;}
}
Add to viewstate:
ViewState.Add("myData", new SomeData () {Value1 = "A",
Value2 = "B",
Value2 = "3"});
Retrieve back from ViewState on postback:
var data = (SomeData)ViewState["myData"];
Label1.Text = string.Format("{0}, {1}, {2}",
data.Value1, data.Value2, data.Value3);
You can use whatever separator you want.
But your code would be a lot cleaner if you used three separate variables.
Let .net handle the viewstate. Using one variable seems like an unnecessary complication.
In your scenario you don't need ViewState - you can store them in variables in the code behind because the rowcommand and button_click will both fire on the same post back. You only need to store items in ViewState if they're needed across post backs.
You can use any separator. For e.g.:
ViewState["items"] = item1 + "~" + item2 + "~" + item3
For retrieving values from ViewState, split the values by "~".