kapildevsharma kapildevsharma - 3 months ago 33
ASP.NET (C#) Question

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?

Answer

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();
}
Comments