Skip to main content

How to use QueryString in asp.net


QueryString in asp.net



QueryString is an alternate and popular way to pass variables between web pages. A page can pass variables with values and another web page can retrieve the variables and their values. We can declare query string variables in an anchor element. We also can pass one variable or multiple variables at a time within an anchor element (HTML link).



To pass multiple variables using QueryString we need to use the '&' separator between variables. Each variable contains its value that we need to assign by an equal '=' sign. this is an example URL to pass variables using the query string.




http://www.asp.net/Image.aspx?ImageID=1&ImageName=Elephant




In the above example URL, we separated the original URL and QueryString by a question mark '?' separator. Then we declare a variable name ID and assign its value to 1 by using an equal '=' sign. We also added another variable name 'ImageName' and its value 'Elephant'. Here we separated and combined variables in QueryString using the '&' sign.




We can retrieve those QueryString variables on another page where the link navigates. In the above example URL we can read the query string variables in Image.aspx web page. The HttpRequest QueryString property gets the collection of HTTP query string variables. The following example code describes how can we read the above query string variables.





string ImageID = Request.QueryString["ID"];


string ImageFullName = Request.QueryString["ImageName"];





If the URL does not have an 'ImageName' QueryString variable then the return value would be null. if we want to use spaces and '&' in the query string we need to replace 'space' with %20 and '&' with %26.




The following asp.net c# example code demonstrates to us how can we use QueryString in an asp.net application.





QueryString.aspx



<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net QueryString example: how to use QueryString</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">QueryString Example</h2>
<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="~/Image.aspx?ImageID=1&ImageName=Elephant"
Text="Test QueryString"
>
</asp:HyperLink>
</div>
</form>
</body>
</html>



Image.aspx



<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
string ID = Request.QueryString["ImageID"];
string Name = Request.QueryString["ImageName"];
Label1.Text = "ImageID: "+ ID;
Label2.Text = "Image name: "+ Name;
Image1.ImageUrl = "~/Images/"+Name+".jpg";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net QueryString example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">QueryString Example: Image View</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="SeaGreen"
>
</asp:Label>
<br />
<asp:Label
ID="Label2"
runat="server"
Font-Size="Large"
ForeColor="SeaGreen"
>
</asp:Label>
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>








Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...