Tuesday 27 November 2012

Dynamic Drop Down list and fetchin value without using ajax in asp.net

in drop.aspx

<script type="text/javascript">
        function selectDropdown() {
        var dropdownValue = document.getElementById("DropDownList1").value;
        //alert("You selected : " + dropdownValue);
        document.getElementById("Textbox1").value = dropdownValue;
   }
</script>
  
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
     <asp:textbox ID="Textbox1" runat="server" CssClass="textarea"></asp:textbox>
    </div>
      </form>

in drop.aspx.cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    static string strconn = ConfigurationManager.ConnectionStrings["reg"].ConnectionString;
    SqlConnection con = new SqlConnection(strconn);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.Attributes.Add("onchange", "javscript:selectDropdown();");
            BindDrop();
        }
    }

    protected void BindDrop()
    {
        con.Open();
        SqlCommand comma = new SqlCommand("select username,mobilenumber from tb_reg", con);
        SqlDataAdapter da = new SqlDataAdapter(comma);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "username";
        DropDownList1.DataValueField = "mobilenumber";
        DropDownList1.DataBind();
        Textbox1.Text = DropDownList1.SelectedItem.Value.ToString();
        con.Close();

    }

}


Output:




No comments:

Post a Comment