Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here is what I am trying to do:

I have a DropDownList in a Formview that if a website user needs to add a new value, they click a button. The button hide the DropDownList and shows a text box. This all works fine. The TextBox and DropDownList are not bind. How to I pass the value from the TextBox or the DropDownlist top my ASP.net update parameter?

ASP:
ASP.NET
UpdateCommand="UPDATE [zEdits] SET [Supervisor] = @Supervisor WHERE [ID] = @ID">
            <asp:Parameter Name="Supervisor" Type="String" />
        <%--other parameters removed--%>


    <asp:Label ID="lblSupervisor" runat="server" Text="Supervisor:" class="styleDBLabeltag" />
    <asp:Button ID="AddSupervisorItem" runat="server" OnClick="btnAddSupervisor_Click" />
    <asp:Button ID="SupervisorItemAdded" runat="server" OnClick="btnSupervisorAdded_Click" Visible="false" />
    <asp:DropDownList ID="DropDownListSupervisor" runat="server" AutoPostBack="true"
           DataSourceID="SupervisorDDLSelect" DataValueField="Name" >

    <asp:SqlDataSource ID="SupervisorDDLSelect" runat="server"
         ConnectionString="<%$ ConnectionStrings:PKLConnectionString %>"
            SelectCommand="SELECT [LastName] + ', ' + [FirstName] as Name FROM [tblPersonnel] ORDER BY [Name]">


vb.net code:
VB
Protected Sub btnAddSupervisor_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim txtSupervisorValue As TextBox = Me.FormView1.FindControl("SupervisorLabel1")
    Dim ddlSupervisor As DropDownList = Me.FormView1.FindControl("DropDownListSupervisor")
    'Dim lstSupervisorItem As ListItem
    'Dim SupervisorStringToFind As String

    txtSupervisorValue.Visible = True
    ddlSupervisor.Visible = True
    Me.FormView1.FindControl("SupervisorItemAdded").Visible = True
    Me.FormView1.FindControl("AddSupervisorItem").Visible = False
End Sub

Protected Sub btnSupervisorAdded_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim txtSupervisorValue As TextBox = Me.FormView1.FindControl("SupervisorLabel1")
    Dim ddlSupervisor As DropDownList = Me.FormView1.FindControl("DropDownListSupervisor")
    'Dim lstSupervisorItem As ListItem
    Dim SupervisorStringToFind As String

    SupervisorStringToFind = txtSupervisorValue.Text
    ddlSupervisor.Items.Insert(0, SupervisorStringToFind)
    ddlSupervisor.SelectedValue = SupervisorStringToFind
End Sub

NOTE: If I try use the SELECTEDVALUE property, I can not edit FormView button doesn't work.
Posted
Updated 29-Dec-11 19:18pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900