Splitting a query string? - c#

this may seem like a stupid question but I have a query string which passes a unique id across pages, What I am wondering is how I would split the following so that I have just the id?
property-buildings_e1.aspx?SubPage=561
Many Thanks!

Request.QueryString("SubPage")

int subPage = 0;
if(Request.QueryString["SubPage"] != null)
subPage = Convert.ToInt32(Request.QueryString["SubPage"]);

If you want to get the value of SubPage
Request.Params["SubPage"]
or
Request.QueryString["SubPage"]

You can use
HttpUtility.ParseQueryString.
NameValueCollection parts = HttpUtility.ParseQueryString(query);
string subPage = parts["SubPage"];

You should be able to access it using
Request.Querystring("SubPage")

Related

address an object by retrieving the ID name of the object from a variable

Good evening,
I am trying to get the following done. I have seen a similar post but it was related with Unity.
Anyway, I am on web forms in asp.net and I have a radiobuttonList with ID="id001"
so on my code behind, I would normally be able to get the selected value by just doing:
string value = id001.SelectedValue
However, in this situation, I don't know the exact ID name, so I have a function that retrieves it. So now I have the variable with the name of the ID. So I want to be able to now, convert the value of that variable in something like this:
string foundid = "id001"
string foundidvalue = id001.SelectedValue
I hope this makes sense.
Thanks in advance for the help.
I am assuming this one is related to your previous question. So, when you found the control, instead of using function to get the fullname, you can do like this:
foreach (Control c in Page.Form.Controls.OfType<RadioButtonList>())
{
if (c.ID.Contains("id"))
{
// string FullID = c.ID.ToString();
var radioButtonList = c as RadioButtonList;
var selectedValue = radioButtonList.SelectedValue;
}
}
You want to use FindControl.
string foundid = "id001";
var foundCtrl = (RadiobuttonList)FindControl(foundid);
var result = foundCtrl.SelectedValue;

C# get parameter from url after redirect

I have a redirect:
string linkedInLogin = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=ID&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=ZEKNI&scope=r_basicprofile";
Response.Redirect(linkedInLogin);
This results in a redirect in my browser to
https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=STATE
How do I retrieve the value of the code paramater?
Request.Querystring["code"] gives me a null value. The problem is that the browser "thinks" the current URL was /home/MyController.
try:
HttpContext.Current.Request.Params["code"]
this way you can get a combined collection of QueryString, Form, Cookies, and ServerVariables items.
here you can read more: HttpRequest.Params Property
Do you have that link stored somewhere or are you asking for how to retrieve it as well?
Assuming you already have it:
var s = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT"
You can either do:
string s = "https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=ROB962242SMUT";
var code = s.Substring((s.IndexOf("code=") + 5), (s.IndexOf('&') - (s.IndexOf("code=") + 5)));
//CODE_I_NEED
Or
String sourcestring = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT";
Regex re = new Regex(#"code=(.*)&");
var result =Match m = re.Match(sourcestring).Value;
//CODE_I_NEED
edit: based on the other two answers i may not have understood your question properly xd

How to Concatenate an Asp.net control name with a string

Basically I have a few asp.net objects. I get the ID of object 1 and assign it to a string. I then want to add a control to a placeholder than end in that ID. Not too sure how to go about doing this.
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
phHazard(SOMEHOW GET THE ID HERE).Controls.Add(txtHazardDesc);
Thanks a lot.
What i understood id you want to add a string to a control that u can do like this..
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
placeholderId.Controls.Add(id);
OR
can refer below example.
https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

Wildcards in a Query String C#

Not sure how to ask this or if it can be done.
I have an ID number, that I want to test if it matches an ID in a URL query string that is stored in an API I am working with.
So if the URL i want to search contains a http://www.testurl.com/page?key=55555, I just want to put in my http request if that url 'contains' that number. Testing if the url is exactly equal has proved problematic as there is a lot of other info the stored query string.
Are wildcards within query strings even possible, considering I am encoding the html already? Any help is appreciated. Thank you!
string[] myUrls = new string[] {
"http://www.testurl.com/page?key=55555",
"http://www.testurl.com/page?key=555556789",
"http://www.testurl.com/page?foo=bar&key=55555&baz=938355555"};
string myToken = "key=55555";
bool exists;
foreach(string url in myUrls)
{
System.Uri uri = new System.Uri(url);
string q = uri.GetComponents(UriComponents.Query, UriFormat.Unescaped);
if (q.Split('&').Any(x=>x== myToken))
{
Console.WriteLine(string.Format("found it in '{0}'", url));
}
}
If you can access the QueryString, you could just do this:
if (HttpContext.Current.Request.QueryString["key"].ToLower() == "5555")
//do something

how to add values in array

i just want to ask help again. I've created a method to read values in gridview, i was able to get and read values from the gridview. The problem now, is how can i store the values inside an array and i want it to pass on the other page.
here's the code i've created
private void getrowvalues()
{
string combinedvalues;
foreach (GridViewRow row in gvOrderProducts.Rows)
{
string prodname = ((Label)row.FindControl("lblProductName")).Text;
string txtvalues = ((TextBox)row.FindControl("txtQuantity")).Text;
combinedvalues = prodname + "|" + txtvalues;
}
}
i want the result string combinedvalues to be put in an array or collection of strings which i can be access in other page. Is there a way to do it? Any inputs will be greatly appreciated.
thanks!!
Just saw KroaX answer which is the same, I leave mine for the example code.
private void getrowvalues()
{
string combinedvalues;
List<string> combinedValuesList = new List<string>();
foreach (GridViewRow row in gvOrderProducts.Rows)
{
string prodname = ((Label)row.FindControl("lblProductName")).Text;
string txtvalues = ((TextBox)row.FindControl("txtQuantity")).Text;
combinedvalues = prodname + "|" + txtvalues;
combinedValuesList.Add(combinedvalues);
}
// use combinedValuesList or combinedValuesList.ToArray()
}
Notepad code, untested...
To pass something from one page to another, you can store it in the session (sometimes it depends).
Session["combinedvalueList"] = combinedvalueList;
While in another page, you can access it.
if (Session["combinedvalueList"]!=null)
combinedValueList = Session["combinedvalueList"] as List<string>;
//Before Foreach
List<string> combinedvalueList = new List<string>();
//Inside Foreach
combinedvalueList.add(combinedvalues);
Please also see
List MSDN
There you can see samples and Methods of List class. It seems to me you are completely new to c# and programming in general ?

Categories

Resources