By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,968 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,968 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(connectionStri ng)

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

Nov 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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()

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Nosaj" wrote:
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(connectionStri ng)

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

Nov 29 '05 #2

P: n/a
> 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.

Nov 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.