How to encode an url in asp.net

Encode an URL
UrlEncode.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string url = "UrlEncodeTest.aspx?";
        url += "ID=" + ListBox1.SelectedValue.ToString()+ "&";
        url += "Name=" + Server.UrlEncode(ListBox1.SelectedItem.Text);
        Response.Redirect(url);
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net UrlEncode method example: how to encode an url in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net UrlEncode() method example</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Text="Select an employee for view profile"
            Font-Bold="true"
            ForeColor="Green"
            >
        </asp:Label>
        <br />
        <asp:ListBox 
            ID="ListBox1" 
            runat="server" 
            ForeColor="AliceBlue" 
            BackColor="DodgerBlue"
            >
            <asp:ListItem Value="1">Jenny Jones</asp:ListItem>
            <asp:ListItem Value="2">John Smith</asp:ListItem>
            <asp:ListItem Value="3">Ben Forta</asp:ListItem>
            <asp:ListItem Value="4">Raymond Camden</asp:ListItem>
            <asp:ListItem Value="5">Little John</asp:ListItem>
        </asp:ListBox>
        <asp:RequiredFieldValidator 
            ID="RequiredFieldValidator1"
            runat="server"
            ControlToValidate="ListBox1"
            Text="*"
            >
        </asp:RequiredFieldValidator>
        <br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            OnClick="Button1_Click"
            Text="Show Profile" 
            Font-Bold="true" 
            ForeColor="Green" 
            />
    </div>
    </form>
</body>
</html>
UrlEncodeTest.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        Label1.Text = "Hi This is " + Request.QueryString["Name"];
        Label1.Text += "<br />My employee ID is: " + Request.QueryString["ID"];
        string imageSource = "~/Images/" + Request.QueryString["ID"] + ".jpg";
        Image1.ImageUrl = imageSource;
        Image1.BorderWidth = 2;
        Image1.BorderColor = System.Drawing.Color.SaddleBrown;
    }
</script>

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