Avatar of H-SC
H-SC
Flag for United States of America asked on

Save DropDownList Selected Value

This may seem like a simple one... but after searching the internet I can't seem to find a straight-foward solution to something that should be simple...

How do you save the selected value of a DropDownList.

I currently have the DropDownList getting its data(list items) from a LINQ connection and that part works great in my EditTemplate, but ..

I would like to send the selected item within the dropdown to my database when I click the update button.  How can this be done??

Here is what I have for the ddl

<asp:DropDownList
DataSourceID="LinqDataSource2"
ID="d1"
DataTextField= "state"
DataValueField="state"
runat="server"
AutoPostBack="true"
AppendDataBoundItems="True">
</asp:DropDownList>
ASP.NET

Avatar of undefined
Last Comment
jasonduan

8/22/2022 - Mon
jasonduan

"I would like to send the selected item within the dropdown to my database when I click the update button."

can you post the related code?

d1.SelectedValue should give you value of the selected item, why it does not work?

H-SC

ASKER
jasonduan,

I have 2 linq sources
one for the data on my webform and the other is for getting the dropdown values

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Frm1.aspx.vb" Inherits="LCS.WebForm2" %>

  <Script runat="server">
     

</Script>


<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        .style1
        {
            width: 100%;
            float: left;
        }
        .newStyle1
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: large;
            font-weight: bold;
            font-style: normal;
            text-decoration: underline;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table class="style1">
        <tr>
            <td>
                <asp:ListView ID="ListView1" runat="server" DataKeyNames="ctl_num" DataSourceID="LinqDataSource1" InsertItemPosition="LastItem">
                    <EditItemTemplate>
                        <p style="font-size: large; font-weight: bold; text-decoration: underline">
                            General Information
                       
                        field_1:<asp:TextBox ID="fl_1" runat="server" Text='<%# Eval("last_name") %>' />
                        <br />
                        <p style="font-size: large; font-weight: bold; text-decoration: underline; color: #008080;">
                            Hello World
                        </p>
                        field_2:<asp:TextBox ID="fl_2" runat="server" Text='<%# Eval("first_name") %>' />
                        <br />
                         field_3:<asp:TextBox ID="fl_3" runat="server" Text='<%# Eval("address_state") %>' />
                        <br />
                         <asp:DropDownList DataSourceID="LinqDataSource2" ID="d1"     DataTextField= "state" ValueField="state" runat="server"    AutoPostBack="true" AppendDataBoundItems="True"> </asp:DropDownList>
                                       
           
                        <br />
                       
                       
                            <p>
                                <br />
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                                <br />
                                <br />
                        </span>
                    </EditItemTemplate>
                 
                                           
                   
                </asp:ListView>
             
                <asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="A02.DataClasses3DataContext" EntityTypeName="" OrderBy="state" Select="new (state)" TableName="my_table_1">
                </asp:LinqDataSource>
             
                <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="A01.DataClasses1DataContext" EnableDelete="True" EnableUpdate="True" EntityTypeName="" TableName="my_table_2" OrderBy="last_name" AutoGenerateWhereClause="True">
                 
                   
                </asp:LinqDataSource>
               
               
            </td>
        </tr>
    </table>
     
</asp:Content>
ASKER CERTIFIED SOLUTION
jasonduan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
H-SC

ASKER
jasonduan,

many thanks for your help

I adjusted the code to be VB ..

  Dim state As String = TryCast(sender, DropDownList).SelectedValue
        TextBox2.Text = state

this works fine for a textbox placed outside of the ListView element but I can't expose anything within tagged items in vb code
how would I set ..for example

my bound field within the EditTemplate to be state:
              field_3:<asp:TextBox ID="fl_3" runat="server" Text='<%# Eval("address_state") %>' />
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
jasonduan

try:

   TextBox txt = ListView1.Controls[0].FindControl("fl_3") as TextBox;
   if (c != null)
   {
        txt.Text = ...;
   }
H-SC

ASKER
here is what I have so far and does not seem to work..  Just not sure what I am doing wrong..

vb code:

Protected Sub d1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim txt As DropDownList = TryCast(ListView1.Controls(0).FindControl("d1"), DropDownList)
        Dim txt2 As TextBox = TryCast(ListView1.Controls(0).FindControl("address_state"), TextBox)
        If txt IsNot Nothing Then
            txt2.Text = txt.Text
        End If
    End Sub
H-SC

ASKER
jasonduan,

on my last post, I do not get any errors,
however address_state textbox does not get populated with the selected text of the ddl("d1")

I have done  breaks on the selectedindexchanged and it appears to be firing correctly but nothing is effected.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jasonduan

use "txt2.Text = txt.Selectedtext" instead of "txt2.Text = txt.Text"
H-SC

ASKER
yes, I have even tried that one and also
 "txt2.Text = "Testing123" and still nothing
jasonduan

I'm afraid the postback causes the listview being repopulated and the textbox got overriden when the listview being repopulated. I would suggest you put a few breakpoints in the code to find out what exactly happened during postback.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
H-SC

ASKER
Here is where the error is...

"Object reference not set to an instance of an object."

this occured on the following
 txt2.Text = "test"


 Dim txt As DropDownList = TryCast(ListView1.Controls(0).FindControl("d1"), DropDownList)
        Dim txt2 As TextBox = TryCast(ListView1.Controls(0).FindControl("textbox2"), TextBox)
        If txt IsNot Nothing Then
            txt2.Text = "test"
        Else
  >>>>>>  txt2.Text = "test"
     End If
    End Sub
H-SC

ASKER
I think that I have it!
for some reason it likes the DirectCast object

 Protected Sub d1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(ListView1.EditItem.FindControl("d1"), DropDownList)
        Dim txt2 As TextBox = DirectCast(ListView1.EditItem.FindControl("in_beds_conditionLabel"), TextBox)
        txt2.Text = ddl.Text
    End Sub
H-SC

ASKER
jasonduan,

this project is a room furniture condition checklist that has many elements of data, I think that by getting this one item working, that I can simply apply it throughout the rest of the checklist data.  Many thanks for getting me on the right track!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jasonduan

you are welcome. Glad you found the solution.