Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdownlist selected item text always returning the first item text

i am using this code to fill dropdownlist from database.

public void fillcountry()
{
    BL obj = new BL();
    DataSet ds = obj.dss("select * from Country  ");
    drplistcountry.DataSource = ds;
    drplistcountry.DataTextField = "CountryName";
    drplistcountry.DataValueField = "CountryId";
    drplistcountry.DataBind();
    drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
}

i am Using this fillcountry() in page load() event. and Rerutning selecteditm.text on Button Click event

drplistcountry is always showing First index text , How to solve it?

like image 941
kapildevsharma Avatar asked Apr 14 '14 05:04

kapildevsharma


People also ask

How do I add a default item to DropDownList?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do you bind a selected item in a drop-down list to a text box and edit it?

How to bind a selected item in a drop-down list to a text box and edit it? You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.

How do you display a selected value in a drop-down list?

Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

Can multiple items be selected in a DropDownList?

Dropdown lists are one of the most flexible elements in HTML. It is similar to that of the radio input, that is, only one item can be selected from a group of items by default. However, when the multiple attribute is used with the <select> element, we can enable the selection of multiple options from the list.


2 Answers

In .aspx page:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" 
EnableViewState="true" %>

For Dropdownlist Control set EnableViewState property to true.

In .aspx.cs page:

In PageLoad event check for following:

if(!IsPostBack)
{ 
    fillcountry();
}
like image 104
Hassan Avatar answered Oct 20 '22 01:10

Hassan


Try this code

And you shoud call this function only once.

 public void fillcountry()
    {
        BL obj = new BL();
        DataSet ds = obj.dss("select * from Country  ");
        drplistcountry.DataSource = ds;
        drplistcountry.DataTextField = "CountryName";
        drplistcountry.DataValueField = "CountryId";
        drplistcountry.DataBind();
        drplistcountry.Items.Insert(0, new ListItem("--Select--", "0"));
        drplistcountry.SelectedIndex = drplistcountry.Items.IndexOf(drplistcountry.Items.FindByText("--Select--"));
    }
like image 26
Jay Avatar answered Oct 19 '22 23:10

Jay