Solved

Finding a Control in formview insert template

Posted on 2010-11-15
2
276 Views
Last Modified: 2012-08-13
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;Persist Security Info=True;User ID=webuser;Password=password"
        Dim dbcon As New SqlConnection(connectionString)
        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.DataTextField = "cshipno"
        DropDownList1.DataValueField = "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.ToLongDateString

        'This will populate the Drop Down List with the store numbers...
        Dim connectionString As String = "Data Source=iwcrex1;Initial Catalog=IWCR75COMP01;Persist Security Info=True;User ID=webuser;Password=password"
        Dim dbcon As New SqlConnection(connectionString)
        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.FindControl("DropDownList2").datatextfield = "cshipno"
        DropDownList1.DataValueField = "id_col"
        DropDownList1.DataSource = dbcmd.ExecuteReader()
        DropDownList1.DataBind()
        DropDownList1.Items.Insert(0, New ListItem("", "NULL"))
        dbcmd.Dispose()
        dbcon.Dispose()

    End Sub
0
Comment
Question by:Wildone63
2 Comments
 
LVL 6

Accepted Solution

by:
AliSyed earned 500 total points
ID: 34141847
Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")
            ddl2.DateValueField= "id_col"
      ddl2.DatetextField= "cshipno"


Also You do not show your html so I do not know which mode this is in.
if Edit mode then
FormView1.CurrentMode == FormViewMode.EDIT

Dim ddl2 As DropDownList = FormView1.FindControl("DropDownList2")
            ddl2.DateValueField= "id_col"
      ddl2.DatetextField= "cshipno"
Ali Syed
0
 
LVL 1

Author Comment

by:Wildone63
ID: 34141884
I am in the Insert Mode...
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="DropDownList2_SelectedIndexChanged2"
                                SelectedValue='<%# Bind("Store") %>'>

                                </asp:DropDownList>
                        </td>
                    </tr>
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Framework versus framework 64 in IIS 8.5 5 80
Find value in LINQ result ? 4 17
VB.Net - For Loop Error 5 25
XML & .net 5 18
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now