473,378 Members | 1,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
2 1997
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit...
8
by: David | last post by:
This is something I had never seen before. On an aspx page, upon pressing a link button for which I have an event handler in the code behind, the screen shows nothing but a line that says "true"...
3
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method,...
0
by: ASP Developer | last post by:
I created a very simple user control that has one dropdownlist. I bind the dropdownlist to database results on load in an ascx file. I would like to do be able to change the selectedindex from...
0
by: ASP Developer | last post by:
I have a databound dropdownlist in a user control (ascx file). In the corresponding aspx page I would like to be able to change the selectedindex of the dropdownlist. So I set up a property in the...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
1
by: CorporateCoder | last post by:
Hi, I am trying to bind the selected value of a databound dropdown box in a databound gridview control to the value being displayed in the template column the dropdown box has been added to. ...
0
by: cvijaykrishna | last post by:
i am having a web based application and i am having a problem with it pls check it Explanationi am sending a sample code plese see it in VS-2005 FOR BETTER UNFERSTANDING I have a main page...
1
by: philippe sillon | last post by:
I have a stupid problem but not able to find solution :( I have repeater that create user control (WebUserControl1) . I pass parameter (InterationString) to user control that is...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.