Search records using textbox and button controls present in first web page and display it’s corresponding records in datalist in second web page

In this blog we will know how to search records using textbox and button controls present in first web page and display it's corresponding records in datalist in second web page.

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="First.aspx.cs" Inherits="First" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Label ID="Label1" runat="server" Text="Search" Font-Bold="True"></asp:Label>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

   

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

    </div>

    </form>

</body>

</html>

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class First : System.Web.UI.Page

{

   

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Redirect("Second.aspx?id=" + TextBox1.Text);

 

    }

}

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="Second" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

     <asp:DataList ID="DataList1" runat="server">

    <FooterStyle BackColor="#FFCC00" ForeColor="#FF3300" />

        <EditItemStyle BackColor="#FF8080" />

        <AlternatingItemStyle BackColor="#FFFF99" />

<HeaderTemplate>

 

</HeaderTemplate>

    <ItemTemplate>

                        <table border="1">

                        <tr>

                        <th>Id</th>

                        <th>Name</th>

                        <th>Price</th>

                        </tr>

                        <tr>

                        <td><%#Eval("mid")%></td>

                        <td><%#Eval("mname")%></td>

                        <td><%#Eval("mprice")%></td>

                        </tr>

                        </table>

                        </ItemTemplate>

    </asp:DataList>

    </div>

    </form>

</body>

</html>

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

public partial class Second : System.Web.UI.Page

{

    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    SqlCommand com;

    string s1;

    string str;

    protected void Page_Load(object sender, EventArgs e)

    {

        s1 = Request.QueryString["id"];

        SqlConnection con = new SqlConnection(connStr);

        con.Open();

        str = "select count(*)from Mobile where mname='" + s1 + "'";

        com = new SqlCommand(str, con);

        int count = Convert.ToInt32(com.ExecuteScalar());

        if (count > 0)

        {

            bindlist();

        }

        else

        {

            Response.Write("Sorry No records found");

        }

      

    }

    void bindlist()

    {

        SqlConnection con = new SqlConnection(connStr);

        con.Open();

        str = "select * from Mobile where mname='" + s1 + "'";

        com = new SqlCommand(str, con);

        SqlDataReader reader;

        reader = com.ExecuteReader();

        DataList1.DataSource = reader;

        DataList1.DataBind();

        reader.Close();

        con.Close();

    }

}