Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dropdownlist ctrl in my page.

I code to set datasoure for dropdownlist in codebehide file.

My question is whenever the page is postback the dropdownlist datasource is nothing and the following code run?

I would like to run the code once page first load.


VB
If UnitName_ddl.DataSource Is Nothing Then
                Dim dt As New DataTable("Unit_tbl")
                dt.Columns.Add("UnitName")
                dt.Rows.Add("--Select UnitName--")
                dt.Rows.Add("KOTAK")
                dt.Rows.Add("ICICI BANK LTD")
                UnitName_ddl.DataSource = dt
                UnitName_ddl.DataBind()
            End If
Posted
Updated 6-Sep-10 21:28pm
v2
Comments
Dalek Dave 7-Sep-10 3:28am    
Minor Edit for Grammar.

Hi Usankardurai,

I think you will have to reset datasoure at every postback event.

Please write the code out side of the PostBack


//Write code here..
if(!IsPostback)
{
//Remove code from here..
}


Please do let me know, if you still have problem. :)

Thanks,
Imdadhusen
 
Share this answer
 
Comments
sankar guru 7-Sep-10 3:32am    
i just write the code inside
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//my code

End Sub
and i set DataTextField and DataValueField as "UnitName"

How to prevent Data Lost each time ?
Sunasara Imdadhusen 7-Sep-10 7:20am    
I think i am not cleared about problem.
what you would to like. You have to maintain or prevent data during postback?
If you have viewstate active for your control try on page_load event:
If not ispostback Then

             Dim dt As New DataTable("Unit_tbl")
              dt.Columns.Add("UnitName")
              dt.Rows.Add("--Select UnitName--")
              dt.Rows.Add("KOTAK")
              dt.Rows.Add("ICICI BANK LTD")
              UnitName_ddl.DataSource = dt
              UnitName_ddl.DataBind


End If


If viewstate is disabled you have to load your values each time the page is postedback.
 
Share this answer
 
Comments
sankar guru 14-Sep-10 7:10am    
EnableViewState property is true for UnitName_ddl control but it doesnt contain no data once again i run it?
LNogueira 15-Sep-10 4:29am    
Try set
UnitName_ddl.DataTextField = "UnitName"
before UnitName_ddl.DataBind

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