Monday 8 August 2016

Write a program to bind data to dropdown using aspx.cs page in ASP.NET

This post will help you to demonstrate a program to bind data to drop-down using aspx.cs page  in ASP.NET. You can copy this code after you open a blank project in IDE of ASP.NET (Visual Studio). Then try to run that copied code, at that time you will get an expecting output. In this program we are trying to bind data to drop-down using aspx.cs page. For this, we used one dropdownlist and one label to print output, and a button to perform this function. There you can see these drop-down and a button also in the output. The label will show as "The result will shown here", it will change after you try to change the value of drop-down in the output.Select any option from that drop-down, after that click on the button. Then you will get the output on the label. The options are added using aspx.cs page. We hope this code will help you to be a developer or a programmer. Before copying, try to understand the method that we used to find the output. Click on the read more button to get this code.


dropdown.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dropdown.aspx.cs" Inherits="our_program.dropdown" %>

<!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>Bind data to dropdown list</title>
    <center><h1>Bind data to dropdown</h1></center>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="btn" runat="server" Text="submit" onclick="btn_Click" />
        <br />
        <br />
        <asp:Label ID="lbl" runat="server" Text="result are shown here"></asp:Label>

    </div>
    </center>
    </form>
</body>
</html>

dropdown.aspx.cs

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

namespace our_program
{
    public partial class dropdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt=new DataTable();
                dt.Columns.Add("name");
                dt.Columns.Add("value");
                dt.Rows.Add("JUNUARY","JUNUART");
                dt.Rows.Add("FEBRUARY","FEBRUARY");
                dt.Rows.Add("MARCH","MARCH");
                dt.Rows.Add("APRIL","APRIL");
                dt.Rows.Add("MAY","MAY");
                dt.Rows.Add("JUNE","JUNE");
                dt.Rows.Add("JULY","JULY");
                dt.Rows.Add("AUGUST","AUGUST");
                dt.Rows.Add("SEPTEMBER","SEPTEMBER");
                dt.Rows.Add("OCTOBER","OCTOBER");
                dt.Rows.Add("NOVEMBER","NOVEMBER");
                dt.Rows.Add("DECEMBER","DECEMBER");
                DropDownList1.DataSource=dt;
                DropDownList1.DataTextField="name";
                DropDownList1.DataValueField="value";
                DropDownList1.DataBind();
            }


        }

        protected void btn_Click(object sender, EventArgs e)
        {
            string a = DropDownList1.SelectedValue.ToString();
            lbl.Text = "selected value is "+a.ToString();
        }
    }
}

No comments:

Post a Comment