Wildone63
asked on
Finding a Control in formview insert template
Hi,
I am using VS-2008 VB and MS-SQL 2005.
I have an ASPX page with a Formview and a dropdown list on it. My Goal is to populate the dropdown list from a table in a database.
This works IF it is not in a formview.
Imports System.Data.SqlClient
Partial Public Class Test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi st Security Info=True;User ID=webuser;Password=passwo rd"
Dim dbcon As New SqlConnection(connectionSt ring)
dbcon.open()
Dim dbcmd As New sqlcommand
dbcmd.CommandText = "select * from arcadr where custno = 'LES-01' order by case when isnumeric(cshipno)=1 then cshipno*1 else 0 end"
dbcmd.Connection = dbcon
DropDownList1.DataTextFiel d = "cshipno"
DropDownList1.DataValueFie ld = "id_col"
DropDownList1.DataSource = dbcmd.ExecuteReader()
DropDownList1.DataBind()
DropDownList1.Items.Insert (0, New ListItem("", "NULL"))
dbcmd.Dispose()
dbcon.Dispose()
End Sub
End Class
But. If I try it in a formview I can get it to work.
Here is what I am trying, Can someone help me with this Please.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = System.DateTime.Now.ToLong DateString
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi st Security Info=True;User ID=webuser;Password=passwo rd"
Dim dbcon As New SqlConnection(connectionSt ring)
dbcon.Open()
Dim dbcmd As New SqlCommand
dbcmd.CommandText = "select * from arcadr where custno = 'LES-01' order by case when isnumeric(cshipno)=1 then cshipno*1 else 0 end"
dbcmd.Connection = dbcon
CType(FormView1.FindContro l("DropDow nList2").d atatextfie ld = "cshipno"
DropDownList1.DataValueFie ld = "id_col"
DropDownList1.DataSource = dbcmd.ExecuteReader()
DropDownList1.DataBind()
DropDownList1.Items.Insert (0, New ListItem("", "NULL"))
dbcmd.Dispose()
dbcon.Dispose()
End Sub
I am using VS-2008 VB and MS-SQL 2005.
I have an ASPX page with a Formview and a dropdown list on it. My Goal is to populate the dropdown list from a table in a database.
This works IF it is not in a formview.
Imports System.Data.SqlClient
Partial Public Class Test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi
Dim dbcon As New SqlConnection(connectionSt
dbcon.open()
Dim dbcmd As New sqlcommand
dbcmd.CommandText = "select * from arcadr where custno = 'LES-01' order by case when isnumeric(cshipno)=1 then cshipno*1 else 0 end"
dbcmd.Connection = dbcon
DropDownList1.DataTextFiel
DropDownList1.DataValueFie
DropDownList1.DataSource = dbcmd.ExecuteReader()
DropDownList1.DataBind()
DropDownList1.Items.Insert
dbcmd.Dispose()
dbcon.Dispose()
End Sub
End Class
But. If I try it in a formview I can get it to work.
Here is what I am trying, Can someone help me with this Please.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = System.DateTime.Now.ToLong
'This will populate the Drop Down List with the store numbers...
Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persi
Dim dbcon As New SqlConnection(connectionSt
dbcon.Open()
Dim dbcmd As New SqlCommand
dbcmd.CommandText = "select * from arcadr where custno = 'LES-01' order by case when isnumeric(cshipno)=1 then cshipno*1 else 0 end"
dbcmd.Connection = dbcon
CType(FormView1.FindContro
DropDownList1.DataValueFie
DropDownList1.DataSource = dbcmd.ExecuteReader()
DropDownList1.DataBind()
DropDownList1.Items.Insert
dbcmd.Dispose()
dbcon.Dispose()
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried what you said but now I get this error...
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is my HTML
<InsertItemTemplate>
<div class="style2">
<br />
<table class="style3">
<tr>
<td class="style7" height="23">
<b>Store:</b></td>
<td style="text-align: left" height="23">
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="Dr
SelectedValue='<%# Bind("Store") %>'>
</asp:DropDownList>
</td>
</tr>