Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

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>
0
H-SC
Asked:
H-SC
  • 8
  • 6
1 Solution
 
jasonduanCommented:
"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?

0
 
H-SCAuthor Commented:
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>
0
 
jasonduanCommented:
change to:
<asp:DropDownList DataSourceID="LinqDataSource2" ID="d1"     DataTextField= "state" ValueField="state" runat="server"    AutoPostBack="true" AppendDataBoundItems="True"
OnSelectedIndexChanged = "ddlState_SelectedIndexChanged"> </asp:DropDownList>

in code:
protected void ddlState_SelectedIndexChanged(object sender, EvengArgs e)
{
    string state = (sender as DropDownList).SelectedValue;

    // put your code here
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
H-SCAuthor Commented:
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") %>' />
0
 
jasonduanCommented:
try:

   TextBox txt = ListView1.Controls[0].FindControl("fl_3") as TextBox;
   if (c != null)
   {
        txt.Text = ...;
   }
0
 
H-SCAuthor Commented:
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
0
 
H-SCAuthor Commented:
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.
0
 
jasonduanCommented:
use "txt2.Text = txt.Selectedtext" instead of "txt2.Text = txt.Text"
0
 
H-SCAuthor Commented:
yes, I have even tried that one and also
 "txt2.Text = "Testing123" and still nothing
0
 
jasonduanCommented:
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.
0
 
H-SCAuthor Commented:
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
0
 
H-SCAuthor Commented:
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
0
 
H-SCAuthor Commented:
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!
0
 
jasonduanCommented:
you are welcome. Glad you found the solution.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now