Using ASP <% .... %> Tags - asp.net

I have been searching for this for quite a while and couldn't find a solution...
I have my aspx file and in it a asp:SqlDataSource, where I want to get values which are equal to the Request.QueryString["key"]. I have defined a parameter for it but I can't find the right syntax to set the value.
Currently it is looking like this:
<SelectParameters>
<asp:Parameter Name="courseID" DefaultValue="<%= Request.QueryString["course_name"]
</SelectParameters>
where I always get the error, it is not well formed. What is the correct syntax, and is there an article how you use this <%.. %> commands?

There's an MSDN page that goes over what each tag is and what it does. Probably using <%...%> is not correct, as that's just a code tag. You want <%=...%> or <%:...%> which actually write values to the page.
But! Actually, if I'm reading what your problem is correctly, you want neither of those. For a SqlDataSource to pull in a query string value, you want to add a <SelectParameters> tag to the datasource, then add a <QueryStringParameter> to that.
Edit:
Yep, looking at the edit you just made, you definitely want a QueryStringParameter.

You can't use <%= within high-level asp controls. That construct writes directly to the output buffer, whereas the controls need to be processed first. In other words, rather than processing your <%= before expanding the control, it must first expand the control before it can process your <%=. They are at different levels of abstraction.
To do what you want to accomplish, rather than a plain <asp:Parameter> use an <asp:QueryStringParameter>. This will allow you to set the key you want to use from the query string.

Related

How to pass multiple server values to the web method when using AJAX Toolkit CascadingDropDowns

I need to pass in multiple server values to the web method.
If I pass through a single value in the ContextKey, like this:
ContextKey='<%# this.someValue.ToString()%>'”
I get the value, no problem.
But as soon as I try passing multiple variables, like this:
ContextKey='someKey1:<%# this.someValue1.ToString()%>;someKey2:<%#this.someValue2.ToString()%>'
I end up getting the literal string.
I used this post to get the syntax on how to pass multiple parameters:
http://selftaughtprogrammer.com/2012/11/01/how-to-pass-extra-parameters-when-using-ajax-toolkit-cascadingdropdowns-with-a-database/
Attribute values in ASP.NET markup can be either literals, or server-binded snippet, you cannot mix them in arbitrary order. However this is not a big deal, since server-binded snippet gives you everything need to form the necessary value. Say via string.Format:
ContextKey='<%# string.Format("someKey1:{0};someKey2:{1}", this.someValue1, this.someValue2) %>'

How can i get the values of parameters from query string in aspx file?

Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.

passing querystring parameters

what is the best way of passing querystring to another page, i would definitly avoid like using.......
<a href="Page2.aspx?WONumber=12345">
is there any better way?
i was thinking create a Baseclass with few prop and update the baseclass?
thanks.
It sounds like you want to take the querystring argument, and use it in subsequent pages.
If it's not desirable to pass-forward this querystring argument from your current page, perhaps it's called page1.aspx, without using another querystring parameter, you could:
store the value in Session. Consider Session["WoNumber"] = Request.QueryString["WONumber"].ToString();
store the value in Cookies. You could use something like: Response.Cookies["WoNumber"].Value = Request.QueryString["WONumber"].ToString();
It really depends on where you're getting the value from. You can build a URL using UriBuilder or if it's simple enough string concatenation could be OK (though you'd have to make sure to Server.UrlEncode the values).
If the value is a constant, as your example implies, then there is nothing wrong with putting it directly into a query string, although I would still use a proper named constant, eg.
<a href="Page2.aspx?WONumber=<%= TheMagicOrderNumber %>
with the constant defined in the code-behind:
protected const int TheMagicOrderNumber = 12345
If your objection is the maintainability of "magic string" URLS, and you'd be prepared to use a button instead of an anchor, you could do worse than
<form method="GET" action="Page2.aspx">
<input type="hidden" name="WONumber" value="12345" />
<input type="submit" value="Navigate" />
</form>
this method will generalise to a query string of any complexity with any number of parameters.
There is a great article I came across a few months ago when I was looking for enhanced security with querystrings...
http://devcity.net/Articles/47/1/encrypt_querystring.aspx
It's a very good article, and has a bonus the author offers code examples in C# and VB.NET.
There are times when I prefer to use querystrings over sessions... small number of session objects is ok, but too many and it starts to become a bit tedious to debug problems.
You can encrypt a querystring parameter, if security is your concern.
Or you can use other holders, such s p.cambell says above (session & cookie).
You could also store it in a database, and have the page you go to retrieve it onload.
Just depends on your application requirements.
Another thing I've done is to use <asp:panel>, basically using a single page as though it were multiple pages. In this way, I also have access to viewstate to hold my variables. (Whenever a user clicks 'next', or whatever they would click to goto the next page, I simply hide the panel they're on, and show the panel they want to go to [visible = true/false] property)

pass data from parent to popup

Apologies if this seems like a duplicate post...
Thomas Warner kindly answeres an earlier post suggesting I use:
Popup.aspx?Data1=Piece_of_data&Data2=Piece_of_data
Just want to ask, if my code is Popup.aspx?Data1=textbox1.text&Data2=textbox2.text
whats the proper way to reference whats in the textboxes?
The way is is above, all that appears in the popup is the actual text 'textbox1.text'
rather than what is actualy in that control.
thanks again
Using asp.net you can litterally write the value straight into the string like:
Popup.aspx?Data1=<%=textbox1.Text%>&Data2=<%=textbox1.Text%>
A more ideal way of doing this would be to build up the URL string in your codebehind so as not to clutter up your HTML and C# code.
That way you could do something like:
String popupUrl = String.Format("Popup.aspx?Data1={0}&Data2={1}",
textbox1.Text,textbox2.Text);
This will also allow you to do any sanitization checks on the values from the textboxes before you start passing those values around.

How to extract element id attribute values from HTML

I am trying to work out the overhead of the ASP.NET auto-naming of server controls. I have a page which contains 7,000 lines of HTML rendered from hundreds of nested ASP.NET controls, many of which have id / name attributes that are hundreds of characters in length.
What I would ideally like is something that would extract every HTML attribute value that begins with "ctl00" into a list. The regex Find function in Notepad++ would be perfect, if only I knew what the regex should be?
As an example, if the HTML is:
<input name="ctl00$Header$Search$Keywords" type="text" maxlength="50" class="search" />
I would like the output to be something like:
name="ctl00$Header$Search$Keywords"
A more advanced search might include the element name as well (e.g. control type):
input|name="ctl00$Header$Search$Keywords"
In order to cope with both Id and Name attributes I will simply rerun the search looking for Id instead of Name (i.e. I don't need something that will search for both at the same time).
The final output will be an excel report that lists the number of server controls on the page, and the length of the name of each, possibly sorted by control type.
Quick and dirty:
Search for
\w+\s*=\s*"ctl00[^"]*"
This will match any text that looks like an attribute, e.g. name="ctl00test" or attr = "ctl00longer text". It will not check whether this really occurs within an HTML tag - that's a little more difficult to do and perhaps unnecessary? It will also not check for escaped quotes within the tag's name. As usual with regexes, the complexity required depends on what exactly you want to match and what your input looks like...
"7000"? "Hundreds"? Dear god.
Since you're just looking at source in a text editor, try this... /(id|name)="ct[^"]*"/
Answering my own question, the easiest way to do this is to use BeautifulSoup, the 'dirty HTML' Python parser whose tagline is:
"You didn't write that awful page. You're just trying to get some data out of it. Right now, you don't really care what HTML is supposed to look like. Neither does this parser."
It works, and it's available from here - http://crummy.com/software/BeautifulSoup
I suggest xpath, as in this question

Resources