.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Search records using textbox and button controls present in first web page and display it's correspo

Posted By:satyapriyanayak       Posted Date: October 07, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

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();
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend