Tag Archives: ASP.Net

Using ParseQueryString to get QueryString Values

Following is the sample code you can use to parse your querystring and return NameValueCollection

[code]

protected void Page_Load(object sender, EventArgs e)
{
String currurl = HttpContext.Current.Request.RawUrl;
String querystring = null ;

// Check to make sure some query string variables
// exist and if not add some and redirect.
int iqs = currurl.IndexOf(‘?’);
if (iqs == -1)
{
String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
Response.Redirect(redirecturl, true);
}
// If query string variables exist, put them in
// a string.
else if (iqs >= 0)
{
querystring = (iqs < currurl.Length – 1) ? currurl.Substring(iqs + 1) : String.Empty;
}

// Parse the query string variables into a NameValueCollection.
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

// Iterate through the collection.
StringBuilder sb = new StringBuilder("");
foreach (String s in qscoll.AllKeys)
{
sb.Append(s + " – " + qscoll[s] + "");
}

// Write the result to a label.
ParseOutput.Text = sb.ToString();

}

[/code]

This is the front end code ,

[code]

<form id="form1" runat="server">
Query string variables are:
<asp:label id="ParseOutput" runat="server">
</asp:label></form>

[/code]

Hope this Helps 🙂

JQuery in Visual Studio

A big part of the appeal of jQuery is that it allows you to elegantly (and efficiently) find and manipulate HTML elements with minimum lines of code. jQuery supports this via a nice “selector” API that allows developers to query for HTML elements, and then apply “commands” to them. One of the characteristics of jQuery commands is that they can be “chained” together – so that the result of one command can feed into another. jQuery also includes a built-in set of animation APIs that can be used as commands. The combination allows you to do some really cool things with only a few keystrokes.

For example, the below JavaScript uses jQuery to find all <div> elements within a page that have a CSS class of “product”, and then animate them to slowly disappear:

$(“div.product”).slideUp(‘slow’).addClass(“removed”);

As another example, the JavaScript below uses jQuery to find a specific <table> on the page with an id of “datagrid1”, then retrieves every other <tr> row within the datagrid, and sets those <tr> elements to have a CSS class of “even” – which could be used to alternate the background color of each row:

$(“#datagrid1 tr:nth-child(even)”).addClass(“even”);

The jQuery library also works well on the same page with ASP.NET AJAX and the ASP.NET AJAX Control Toolkit.

Visual Studio figures out external script references, such as to JQuery, by following special reference comments it finds at the top of .js files:

/// <reference path="jquery-1.2.3.js" />

You can download the JQuery from here. You can get more details on JQuery from http://jquery.com/ .

For intellisense support for JQuery you need to install this Hotfix.

Comment for javascript is written inside for intellisense support. JavaScript has the interesting feature that calling toString on a function returns the code of this function including the comments and thus the doc comments. Here’s a similar example in JavaScript where the documentation is written inside of a class constructor:

jsintellisense

Another difference with C# or VB.NET is that property and event accessors are two different entities in JavaScript. For this reason, to choose where the documentation should be for those members. Properties should be documented in the getter and events in the “adder” for this reason:

js

Use CS and VB files in App_Code

We all know that App_Code folder contains source code files written as traditional class files — that is, files with a .vb extension, .cs extension, and so on.

But when we keep .cs and .vb file together in one folder and try to build the project it throws errors. So to successfully build the project we follow two simple steps .

Step#1 : Create two folders VBCode for .vb files and CSCode for .cs files.

Step#2:  Modify web.config file.

[code]<compilation>
<codeSubDirectories>
<add directoryName="CSCode"/>
<add directoryName="VBCode"/>
</codeSubDirectories>
</compilation>[/code]

That’s all, Hope this helps.
Enjoy !! 🙂

Reading Database value in Javascript

Today while developing a page, we felt need to read database into javascript array. And we were confused if at all we can do this. So finally after googling on this we found a workaround. So I just thought of sharing this with you because I think this is really helpful.

[code]<script language="JavaScript" type="text/javascript">
var allRecords = new array();
var count=0;
<%While (NOT rs_yours.EOF)%>
allrecords[count]="<%= rs_yours.Fields.Item("foo1").Value %>"
<%
rs_yours.MoveNext()
Wend
%>
</script>[/code]

This script will read the value from your datareader into javascript array.

Also if you want to read a single value from the server side into your javascript like this,

[code]var location='<%=Session["location"]%>’;[/code]

Hope this helps 🙂