Solved

Save DropDownList Selected Value

Posted on 2010-11-12
14
687 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server error in 'XXXXXXXX' application. 12 69
creating a flowchart from an algorithm 5 30
Jquery autocomplete 10 68
Syntax Error 2 45
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

16 Experts available now in Live!

Get 1:1 Help Now