Databinding problem with a dropdownlist in ascx file as it runs in aspx!

N

Nosaj

Houston, we've got a problem...

In a ascx file, I bind a datasource with a dropdownlist. The following
code runs perfectly in a aspx file but not in a ascx file. And I just
can't understand it. I changed many time my Page_Load function, but
nothing happened...

Function MyQueryMethod() As System.Data.DataSet
Dim connectionString As String = "server='XXX'; user
id='XXX'; password='XXX'; database='XXX'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [XXX].[LIBELLE] FROM
[XXX]"
Dim dbCommand As System.Data.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dataAdapter As System.Data.IDbDataAdapter = New
System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New
System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function


Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
DropDownList2.DataTextField = "LIBELLE"
DropDownList2.DataSource = MyQueryMethod()
DropDownList2.DataBind()
End If
End Sub
 
G

Guest

In VS.Net, Setup break points within the Page_Load method and step through
the application to see if the Page_Load method within your control is being
invoked or not. If it is you should get a display unless your query brings
no records.

BTW, by default ASP.NET uses the first datatable in your dataset. However,
notice that a dataset can contain more than one datatable. Therefore, for a
clear style of code, you should specify the table to which you databind the
dropdownlist like this:

ddlList1.DataSource = MyQueryMethod()
ddlList1.DataMember = "PutHereTheNameOfTheDataTable"
ddlList1.DataTextField = "Country"
ddlList1.DataBind()
'or alternatively specify the datasource as being a specific table
like this
ddlList1.DataSource = MyQueryMethod.Tables(0)
ddlList1.DataTextField = "Country"
ddlList1.DataBind()
 
S

Shamim

Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
DropDownList2.DataTextField = "LIBELLE"
DropDownList2.DataSource = MyQueryMethod()
DropDownList2.DataBind()
End If
End Sub



So, you are using Ascx file in Aspx. Now remember that
If Not Page.IsPostBack Then

So, that typixcally means you are doing databinding only first time. I
suppose for a ascx file you need to bind data every time the aspx file
calls it. You could try without that line. Also if you are dynamically
loading the control using loadcontrol it might loose state as well.
Yes, debigging could solve that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top