Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown in detailsview problem

Status
Not open for further replies.

RapidDave

Technical User
Aug 28, 2008
4
GB
Hello everyone,

I am a complete newbie with ASP and VB.net so please forgive me when I ask something stupid! Sorry this is also a bit long!

I have a detailsview with a nested dropdown list. Here is the markup:

<asp:DetailsView ID="dvOperator" runat="server" Height="50px" Width="307px"
DataKeyNames="opid" AutoGenerateRows="False" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black">
<FooterStyle BackColor="#CCCCCC" />
<RowStyle BackColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<Fields>
<asp:BoundField DataField="opid" HeaderText="Operator ID" />
<asp:BoundField DataField="clerkid" HeaderText="Clerk ID" />

<asp:BoundField DataField="opidmenu" HeaderText="Link to Menu" />
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:DropDownList ID="ddTitle" runat="server" AutoPostBack="True"
DataTextField="Title" DataValueField="otitle" AppendDataBoundItems="True"
onselectedindexchanged="ddTitle_SelectedIndexChanged"
DataSourceID="m_decodeTitle">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:DetailsView>

I have added the DataSourceID as “m_decodeTitle”. In my VB program (code behind?) I have the following code:

Imports Progress.Open4GL.Proxy
Imports Rapid

Partial Public Class _Default
Inherits System.Web.UI.Page

Private m_Connection As Connection
Private m_AppObject As RapidApp
Private m_SubAppObject As RapidSubApp

Dim m_OEConnect As New OEConnection
Dim m_opid As String = ""

Dim m_GCTitle As String = "opidtitle"

'Step 1 - Define a DataTable Member Variables
Private m_operators As Rapid.StrongTypesNS.dsSoperatorDataSet
Private m_details As Rapid.StrongTypesNS.dsSoperatorDataSet
Private m_decodeTitle As Rapid.StrongTypesNS.dsSdecodeDataSet

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
m_Connection = m_OEConnect.Connect
m_AppObject = New RapidApp(m_Connection)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Connect Error")
End Try

If Not (m_AppObject Is Nothing) Then
m_SubAppObject = m_AppObject.CreateAO_RapidSubApp()
m_operators = New Rapid.StrongTypesNS.dsSoperatorDataSet
m_details = New Rapid.StrongTypesNS.dsSoperatorDataSet
m_decodeTitle = New Rapid.StrongTypesNS.dsSdecodeDataSet

m_SubAppObject.getoperator(m_opid, m_operators)
m_SubAppObject.getoperator(m_opid, m_details)
m_SubAppObject.getdecode(m_GCTitle, m_decodeTitle)

dgOperators.DataSource = m_operators
dgOperators.DataBind()

dvOperator.DataSource = m_details
dvOperator.DataBind()

End If

End Sub

All seems syntactically correct but when I run the program I get the following runtime error:

System.Web.HttpException was unhandled by user code ErrorCode=-2147467259
Message="The DataSourceID of 'ddTitle' must be the ID of a control of type IDataSource.
A control with ID 'm_decodeTitle' could not be found."

This error occurs at the line “dvOperator.DataBind()”.
Any ideas anyone? All help most gratefully received.

Thanks, Dave.


 
First you have this property set:
Code:
DataSourceID="m_decodeTitle"
You have set the property to an object that does not exist at design time, so I'm not sure how the page even compiled. You have to set the datasource in your code behind. First you have to use the FindControl method in the DataBinding event of the detailsview. Once you do that, then you can assign the datasource to the control because it has actually been created in your previous code.

There are many posts here on how to use findcontrol as well as in VS help and on-line.
 
Thanks for the reply.

I have removed the datasourceid from the markup as you have suggested. My problem now is that I cannot locate the control "ddTitle" form my codebehind.

I have added these two procedures:
Protected Sub ddTitle_dataBinding(ByVal sender As Object, ByVal e As EventArgs) Handles dvOperator.DataBinding

Dim myControl1 As Control = dvOperator.FindControl("ddTitle")

If (Not myControl1 Is Nothing) Then
MsgBox("ddTitle_dataBinding - Control found.....")
Else
MsgBox("ddTitle_dataBinding - Control not found.....")
End If

End Sub

Protected Sub dvOperator_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles dvOperator.PreRender
Dim myControl1 As Control = dvOperator.FindControl("ddTitle")

If (Not myControl1 Is Nothing) Then
MsgBox("dvOperator_PreRender - Control found.....")
Else
MsgBox("dvOperator_PreRender - Control not found.....")
End If
End Sub However, both procedures fail to find the "ddTitle" control. Am I looking in the right place?

I have tried using both

Dim myControl1 As Control = dvOperator.FindControl("ddTitle")

and

Dim myControl1 As Control = FindControl("ddTitle")

Bit neither of these find the control. Any ideas where I am going wrong?

Thanks for any help.
 
After more experimentation these procedures DO find the control when I run them on the PreRender and DataBinding events of the dropdown list, ddTitle, itself. But I dont seem to be able to bind the datasource at this point!

I dont seem to have the option to bind the datasource, I would like to do this:

Dim myControl1 As Control = dvOperator.FindControl("ddTitle")
myControl1.Datasource = m_decodeTitle
myControl1.DataBind()

But I get "Error 1 'Datasource' is not a member of 'System.Web.UI.Control'. " when I try.

Any ideas where I am going wrong?
 
Code:
Dim DropDownList As Control = CType(DropDownList, dvOperator.FindControl("ddTitle"))
myControl1.Datasource = m_decodeTitle
myControl1.DataBind()
not sure about the exact vb syntax, but it's something like this

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason. I actually did something very similar and it now works fine.
Dim myControl1 As DropDownList = DirectCast(dvOperator.FindControl("ddTitle"), DropDownList)

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top