Solved

Save DropDownList Selected Value

Posted on 2010-11-12
14
686 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

21 Experts available now in Live!

Get 1:1 Help Now