Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , i have a issue..
i have dynanically bounded the data from the database(sqlserver 2008 express) to a dropdownlist.(on VS2010. Whenever i change the value from default to something else in dropdownlist...it comes back to the original value(and not the selected one)....i tried to write the code on Page Init as well... but then it does not shows the value at all in dropdownbox after postback....Please help..

here is the code(dropdownlist autopostback is true)


Page_init
{
...connection....
da.fill(dt);
dropdownlist1.datasource=dt;
dropdownlist1.datatextfield="city"
dropdownlist1..databind();
}


Regards.
Posted

You have to check whether postback event is fired or not. I put some code below.

C#
protected override void OnInit(EventArgs e)
    {
            base.OnInit(e);

            if (!IsPostBack)
            {
                // Put your logic here.
              da.fill(dt);
              dropdownlist1.DataSource=dt;
              dropdownlist1.DataTextField="city"
              dropdownlist1.DataValuefield="city" // you may need to add it.
              dropdownlist1.Databind();

            }

    }


I hope this helps you well.
   
v4
if(!ispostback)
{
sqldataadapter da=new sqldataadapter("select * from table",cn);
datatable dt=new datatable();
da.fill(dt);
dropdownlist1.datasource=dt;
dropdownlist1.datatextfield="col";
dropdownlist1.datavaluefield="col";
dropdownlist1.databind();
dropdownlist1.items.insert(0,"Select");
}
   
You Need To Recreate again after postback i hope solves the problem
   
hi,


C#
if (IsPostBack)
               return;
...connection....
da.fill(dt);
dropdownlist1.datasource=dt;
dropdownlist1.datatextfield="city"
dropdownlist1..databind();

1



try this one put this code on page load or Page_init
   
v2
Comments
shikhar gilhotra 14-May-11 7:24am
   
NOOOOOOO... thats what i dont want ..can you please read the issuer again ???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900