Solved

Save DropDownList Selected Value

Posted on 2010-11-12
14
691 Views
Last Modified: 2012-06-21
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
Comment
Question by:H-SC
  • 8
  • 6
14 Comments
 
LVL 11

Expert Comment

by:jasonduan
ID: 34123798
"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
 
LVL 1

Author Comment

by:H-SC
ID: 34124156
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
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 34124411
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:H-SC
ID: 34125084
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
 
LVL 11

Expert Comment

by:jasonduan
ID: 34125484
try:

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

Author Comment

by:H-SC
ID: 34137578
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
 
LVL 1

Author Comment

by:H-SC
ID: 34137676
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
 
LVL 11

Expert Comment

by:jasonduan
ID: 34138563
use "txt2.Text = txt.Selectedtext" instead of "txt2.Text = txt.Text"
0
 
LVL 1

Author Comment

by:H-SC
ID: 34138729
yes, I have even tried that one and also
 "txt2.Text = "Testing123" and still nothing
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34138834
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
 
LVL 1

Author Comment

by:H-SC
ID: 34139562
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
 
LVL 1

Author Comment

by:H-SC
ID: 34145105
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
 
LVL 1

Author Comment

by:H-SC
ID: 34145152
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
 
LVL 11

Expert Comment

by:jasonduan
ID: 34145267
you are welcome. Glad you found the solution.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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