Showing posts with label Dropdownlist. Show all posts
Showing posts with label Dropdownlist. Show all posts

Sunday, 18 November 2012

There is a situation in drop down list where first item is "[Select]" and you need to make mandatory selection from drop down list except "[Select]".
Means, user must select value from drop down list but did not allow to select first element "[Select]". We can achieve this using required field validator control. For that we need to set "InitialValue" property.

Here is example for this.
In this example we take one product drop down list control in this list we list out all products, But the First Item is "[Select]" and it's value is "-1" in product drop downlist.
And We take one Required Field Validator control and set it's ValidateControl property to product drop down list id. We also set "InitialValue=-1" property.
We also took one button so that on click of button required field valdator control validate user input.
In this example we set InitialValue to -1 so that , if user select "[Select]" item from drop down list and click on "Save" button Validator control executed and validation message display on screen.
You can see that validation message in output.

Friday, 14 September 2012

There is a situation where you want to select an item from drop down list or combo box and raise server side event to do some processing at server side. You can raise or fire server side event on selection of item from drop down list or combo box. It will raise "onselectedindexchanged" event.
For that you need to set AutoPostBack="true" property.

Here is example for this.
In this example we take on product drop down list and select an item from that after item selected dropdonwlist's "onselectedindexchanged" event raised.

ASPX Code :
   <b>Product List :</b>  <asp:DropDownList runat="server" ID="ddlProductList" AutoPostBack="true"
                            onselectedindexchanged="ddlProductList_SelectedIndexChanged"></asp:DropDownList>

C# Examples :
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            ddlProductList.Items.Add(new ListItem("[select]", "-1"));
            ddlProductList.Items.Add(new ListItem("CPU", "1"));
            ddlProductList.Items.Add(new ListItem("LCD", "2"));
            ddlProductList.Items.Add(new ListItem("LED", "3"));
        }
    }

    protected void ddlProductList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("Selected Value : " + ddlProductList.SelectedValue);
        Response.Write("</br>Selected Text : " + ddlProductList.SelectedItem.Text);
        Response.Write("</br></br></br>");
    }

VB.net Example :
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            ddlProductList.Items.Add(New ListItem("[select]", "-1"))
            ddlProductList.Items.Add(New ListItem("CPU", "1"))
            ddlProductList.Items.Add(New ListItem("LCD", "2"))
            ddlProductList.Items.Add(New ListItem("LED", "3"))
        End If
    End Sub

    Protected Sub ddlProductList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Response.Write("Selected Value : " & ddlProductList.SelectedValue)
        Response.Write("</br>Selected Text : " & ddlProductList.SelectedItem.Text)
        Response.Write("</br></br></br>")
    End Sub

Output :


For Beginning .Net articles. Click Here...

This type of .Net Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Saturday, 8 September 2012

There is a need in website that you want to add items manually in dropdownlist or combobox.
You can achieve this using "Items.Add" methods of dropdownlist.

Here is example for this.
In this example we take one dropdownlist control name "ddlProductList". We fill this dropdownlist manually from coding.

ASPX Code :
<b>Product List :</b>  <asp:DropDownList runat="server" ID="ddlProductList"></asp:DropDownList>


 C# Examples :
        ddlProductList.Items.Add(new ListItem("[select]", "-1"));
        ddlProductList.Items.Add(new ListItem("CPU", "1"));
        ddlProductList.Items.Add(new ListItem("LCD", "2"));
        ddlProductList.Items.Add(new ListItem("LED", "3"));

VB.net Example :
        ddlProductList.Items.Add(New ListItem("[select]", "-1"))
        ddlProductList.Items.Add(New ListItem("CPU", "1"))
        ddlProductList.Items.Add(New ListItem("LCD", "2"))
        ddlProductList.Items.Add(New ListItem("LED", "3"))

Output :
For Beginning .Net articles. Click Here...

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Wednesday, 5 September 2012

There are a situations where you want to fill dropdownlist or combobox or listbox from database table.
For that you need to get database table records in to DataSet or DataTable and after that you fill dropdownlist from DataTable.
For that we need to set Three properties of dropdownlist control.
Properties are:
  • DataSource : Assign Datable object
  • DataTextField : This is column name of database table which you want to display in dropdownlist box.
  • DataValueField : This is column name of database table which use to identify which item is selected in list.
 After assigning this properties You need to call DataBind() Method of drop down list.

You can also say that fill dropdownlist using ADO.Net.

Here are sample example for this.
In this example we retrieve "product_master" table data from database and populate in to drop down list.

C# Examples :
        SqlConnection objConn = new SqlConnection();
        SqlDataAdapter objADP = new SqlDataAdapter();
        DataSet oDs = new DataSet();
        objConn.ConnectionString = @"Data Source=.\SQLEXPRESS;" +
                                   "Initial Catalog=TempDatabase;" +
                                   "User ID=sa;Password=sa;";

        SqlCommand objcmd = new SqlCommand();
        objcmd.CommandText = "Select product_id , product_name from product_master";
        objcmd.CommandType = CommandType.Text;
        objConn.Open();
        objcmd.Connection = objConn;
        

        objADP.SelectCommand = objcmd;
        objADP.Fill(oDs,"tblProductMaster");

        objConn.Close();

        ddlProductList.DataSource = oDs.Tables["tblProductMaster"];
        ddlProductList.DataTextField = "product_name";
        ddlProductList.DataValueField = "product_id";
        ddlProductList.DataBind();

        objADP = null;

VB.net Examples :

        Dim objConn As New SqlConnection()
        Dim objADP As New SqlDataAdapter()
        Dim oDs As New DataSet()
        objConn.ConnectionString = "Data Source=.\SQLEXPRESS;" & _
                                   "Initial Catalog=TempDatabase;" & _
                                   "User ID=sa;Password=sa;"

        Dim objcmd As New SqlCommand()
        objcmd.CommandText = "Select product_id , product_name from product_master"
        objcmd.CommandType = CommandType.Text
        objConn.Open()
        objcmd.Connection = objConn


        objADP.SelectCommand = objcmd
        objADP.Fill(oDs, "tblProductMaster")

        objConn.Close()

        ddlProductList.DataSource = oDs.Tables("tblProductMaster")
        ddlProductList.DataTextField = "product_name"
        ddlProductList.DataValueField = "product_id"
        ddlProductList.DataBind()

        objADP = Nothing

Output : 


You can also fill listbox with same way.


For Beginning .Net articles. Click Here...

Click Here to View "Fill dropdownlist from Enum with C# Examples and VB.Net Examples". Click Here

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


Tuesday, 8 May 2012

There are a situations where you want to fill or populate dropdownlist of listbox using Enum.
Here are sample example for this.

C# Example :
public enum ProgrammingLanguage
{
    CSharp,
    VB,
    JAVA
}
foreach (ProgrammingLanguage enmLnaguage  in Enum.GetValues(typeof(ProgrammingLanguage)))
{
     cboProgrammingLanguage.Items.Add(new ListItem(enmLnaguage.ToString(), Convert.ToInt32( enmLnaguage).ToString()));
}

VB.net Example :
Public Enum ProgrammingLanguage
   CSharp
   VB
   JAVA
End Enum 
For Each enmLnaguage As ProgrammingLanguage In [Enum].GetValues(GetType(ProgrammingLanguage))
   cboProgrammingLanguage.Items.Add(New ListItem(enmLnaguage.ToString(), enmLnaguage))
Next

Click Here to view "Populating or Fill dropdownlist from Database table with C# Examples and VB.Net Examples". Click Here...

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.