Thursday, 20 December 2012

SEARCH TEXTBOX FOR GRIDVIEW USING JQUERY

 IN THE ASPX PAGE
<html>
<head runat="server">
<%-- Jquery link  --%>  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<%-- Script  --%>   
 <script type="text/javascript">
     $(document).ready(function () {
         $('#txtcatsearch').keyup(function (event) {
             var searchKey = $(this).val().toLowerCase();
<%--3 is the column name in the gridview used to search  --%>   
             $("#catgridview tr td:nth-child(3)").each(function () {
                 var cellText = $(this).text().toLowerCase();
                 if (cellText.indexOf(searchKey) >= 0) {
                     $(this).parent().show();
                 }
                 else {
                     $(this).parent().hide();
                 }
             });
         });
     });
 </script>
</head>
<body>
<asp:TextBox ID="txtcatsearch" runat="server"></asp:TextBox>
 <asp:GridView ID="catgridview" runat="server">  
 </asp:GridView><%-- Fill the gridview using either databind in the cs page or by anyother type  --%>
</body>
</html>

Thursday, 6 December 2012

DROPDOWN LIST BINDING AND SELECTED INDEX CHANGE EVENT

CREATE A TABLE

CREATE TABLE Nametable
(
    [Nameid] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL
)

IN THE ASPX PAGE

   <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
    </asp:DropDownList>
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   </form>

IN THE ASPX.CS PAGE (dropdown binding and selected index change event)
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Default7 : System.Web.UI.Page
{
    //Configuring the connection string and calling it from web.config
    static string sathya = ConfigurationManager.ConnectionStrings["SQLCON"].ConnectionString;
    SqlConnection satcon = new SqlConnection(sathya);
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            binddrop();
        }
    }
//Binding the dropdown list with the sql database
    private void binddrop()
    {
        satcon.Open();
        da = new SqlDataAdapter("select * from Nametable",satcon);
        DataSet ds = new DataSet();
        da.Fill(ds);
        satcon.Close();
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Nameid";
        DropDownList1.DataBind();
    }
//SelectedIndexChanged event displaying the number according the name selected in the dropdown list
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Nameid = Convert.ToInt32(DropDownList1.SelectedValue);        satcon.Open();
        SqlCommand cmd = new SqlCommand("select * from Mobiletable where Nameid=" + Nameid , satcon)
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        TextBox1.Text = dr["Number"].ToString(); //Displaying the output filed "Number" in the texbox
        satcon.Close();
       }
}
back to top