Search TextBox Value In Google Using ASP.NET

Introduction

Begin by adding a using statement for System.text library, since StringBuilder is located in the System.Text namespace. Use StringBuilder to modify a string without creating a new object or concatenating many strings together in a loop.

In this article I will show how to search using Google for a value in a TextBox using ASP.NET.

Complete Program

Search_Value.aspx.cs

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Text;  
  8. public partial class Search_Value : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.     }  
  13.     protected void Button1_Click(object sender, EventArgs e)  
  14.     {  
  15.         try  
  16.         {  
  17.             string text = string.Empty;  
  18.             StringBuilder txtaddress = new StringBuilder();  
  19.             txtaddress.Append("https://www.google.com/search?q=");  
  20.             if (searchtxt.Text != string.Empty)  
  21.             {  
  22.                 text=searchtxt.Text.Replace(' ','+');  
  23.                 txtaddress.Append(text+' ''+');  
  24.             }  
  25.             string url = txtaddress.ToString();  
  26.             Response.Redirect(url, false);  
  27.         }  
  28.         catch (Exception ex)  
  29.         {  
  30.             Response.Redirect(ex.ToString());  
  31.         }  
  32.     }  
  33. }   

Search_Value.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search_Value.aspx.cs" Inherits="Search_Value" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div style="border-style: double; border-width: medium; padding: inherit; margin: inherit; height: 194px;  
  10.     width: 349px; background-color: #808080; visibility: visible;">    
  11.         <br />  
  12.               
  13.         <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Large"  
  14.         ForeColor="Blue" Text="Search a value in Google"></asp:Label>  
  15.         <br />  
  16.         <br />  
  17.        
  18.         <asp:Label ID="Label2" runat="server" Text="Enter Search Text"></asp:Label>  
  19.    
  20.         <asp:TextBox ID="searchtxt" runat="server"></asp:TextBox>  
  21.         <br />  
  22.         <br />  
  23.                 
  24.          
  25.         <asp:Button ID="Button1" runat="server" Font-Bold="True" OnClick="Button1_Click" Text="Search" />  
  26.         <br /><br /><br /><br /><br /><br /><br /><br /></div><br />  
  27.     </form>  
  28. </body>  
  29. </html> 

Output

Animation1.gif

For more information, download the attached sample application.


Similar Articles