Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Showing posts with label dropdownlist. Show all posts
Showing posts with label dropdownlist. Show all posts

Thursday, 23 July 2015

Introduction:

In this article I will explain how to bind dropdown list to database and how to set default value in dropdown list from code behind.

Tuesday, 9 June 2015

Introduction: 
In this article I will explain how to fill or filter a gridview based on dropdown selected value in asp.net using c#, vb.net. In his article I will filter the gridview data according to dropdown selection. First of all I will bind the dropdown with data from database and then gridview. After binding the data from database I will filter the data based on dropdown selection.

Friday, 5 June 2015

Introduction

In this article I will explain how to set dropdownlist selected value on page load or code behind in asp.net (c#, vb.net). In dropdownlist we can set selected item by value or text using FindByValue and FindByText properties.
We have two different methods to set dropdownlist selected value.

Method 1: Set Dropdownlist Selected Value Based on value
using FindByValue property we can set dropdownlist selected value

ddlusers.Items.FindByValue("4").Selected = true;

Method 2: Set Dropdownlist Selected value based on Text
using FindByText property we can set dropdownlist selected value

ddlusers.Items.FindByText("Sales").Selected = true;

Source Code for sample Application:
Design your page as given below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>set dropdownlist default value in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
 <fieldset style="width:250px">
    <legend><strong>Set Default Value OF dropdown </strong></legend>
<div>
<asp:DropDownList ID="ddlDept" runat="server">
<asp:ListItem Value="1">HR</asp:ListItem>
<asp:ListItem Value="2">Marketing</asp:ListItem>
<asp:ListItem Value="3">Production</asp:ListItem>
<asp:ListItem Value="4">Sales</asp:ListItem>
<asp:ListItem Value="5">Inventory</asp:ListItem>
</asp:DropDownList>
</div></fieldset>
</form>
</body>
</html>


ASP.Net code Behind file code:

C# Code
write the code like as shown below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// set dropdownlist selected based on text
ddlusers.Items.FindByText("Sales").Selected = true;
}
}

VB.NET Code

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
' set dropdownlist selected based on text
ddlusers.Items.FindByText("Sales").Selected = True
End If
End Sub

End Class

Monday, 30 March 2015

Introduction: 
Using Jquery we can check whether Radiobutton is selected or not. This can be done by using server side code but using jquery it is fast and easy to implement.

Sunday, 8 March 2015

While working on asp.net project, we have the requirement to select time manually in hh: mm:SS (hours (24), minutes (59) and seconds (59)) in two digit format e.g. 00: 01: 02 etc in 3 different drop down list controls. So that user can select hour, minute and seconds manually.

Thursday, 5 March 2015

In this article I will explain How to fill dropdownlist at runtime with time interval of specified minutes or hours e.g. 5 minutes, 15 minutes, 1 hour, 5 hours etc as per requirement.

Thursday, 26 February 2015

Introduction

 In this article I am going to explain with example How to bind asp.net dropdown list dynamically from sql server database by using jquery AJAX and json.

In asp.net we can clear dropdownlist using the server side code i.e. DropDownList1.ClearSelection() method. We can do this at client side using jQuery. Jquery is fast and light as compare to server side code. So it is good to use jquery rather than write a code for this purpose. There are a number of methods to reset dropdown list back to default state.

Tuesday, 17 February 2015

Introduction:

Sometime it is required to fill dropdown and disable its some of the items on the basis of some condtion. While working on one of my asp.net project I got the requirement to populate departments in DropDownList and disable some of them so that they can't be selected.
Suppose if Department Name is “HRM” then it got disable.



In this article, I have explained how to dynamically Fill Dropdown List from SQL

Tuesday, 12 November 2013

Introduction:
In this article you will come to know how to bind dropdownlist in asp.net with database.

Implementation:

In the desgin (.aspx) page desgin the struture as below: Add dropdown control on the webpage.

<asp:DropDownList ID="ddlState" runat="server">
        </asp:DropDownList>

Write code in code behind file to Bind Dropdown:

C#.Net code


using System.Data;
using System.Data.SqlClient;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropdown();
        }
    }

    private void BindDropdown()
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from Tb_STATE", con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        ddlState.DataSource = dt;
        ddlState.DataTextField = "STATE";
        ddlState.DataValueField = "ID";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("--Select--""0"));
        con.Close();
        adp.Dispose();
    }

VB.Net Code

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConStr").ToString())
    Private Sub BindDropdown()
        Dim adp As New SqlDataAdapter("select * from Tb_STATE", con)
        Dim dt As New DataTable()
        adp.Fill(dt)
        ddlState.DataSource = dt
        ddlState.DataTextField = "STATE"
        ddlState.DataValueField = "ID"
        ddlState.DataBind()
        ddlState.Items.Insert(0, New ListItem("--Select--""0"))
        con.Close()
        adp.Dispose()
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgsHandles Me.Load
        If Not IsPostBack Then
            BindDropdown()
        End If

    End Sub