Click here to Skip to main content
15,891,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I must audit the changes into a database. On most pages this works perfect, as there were just textboxes - however on a page I am working on right now, with dropdownlists, my code isn't working.

ASP.NET
<td>
  <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDatacountry" DataTextField="country_name" DataValueField="country_id">
  

  <asp:SqlDataSource ID="SqlDatacountry" runat="server"  ConnectionString="<%$ ConnectionStrings:songtypecons %>" SelectCommand="SELECT * FROM [country_detail]">
</td>

code behind:

C#
string sql1 = "selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id ='" + ds.Tables["filldata"].Rows[0].ItemArray[0].ToString() + "' ";
            SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, con);
            DataSet ds1 = new DataSet();
            adpt1.Fill(ds1, "custdata");
            if (ds1.Tables["custdata"].Rows.Count > 0)
            {

             for (int d = 0; d < DropDownList4.Items.Count; d++)
            {
               if (ds1.Tables["custdata"].Rows[0].ItemArray[7].ToString() == DropDownList4.Items[d].Text)
              {
                   DropDownList4.Items[d].Selected = true;
                   break;
               }
           }
        }
Posted
Updated 14-Apr-13 21:03pm
v2
Comments
AshishChaudha 15-Apr-13 3:05am    
what problem you are getting??

Use SelectedValue property of DropDownList.
 
Share this answer
 
XML
<asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown" />
    <asp:updatepanel runat="server" id="UpdatePanel1" xmlns:asp="#unknown">
        <contenttemplate>
            <asp:dropdownlist runat="server" id="ddlCaseFilesNew" datasourceid="dsCaseFiles">
                DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" Width="300px"
                AutoPostBack="true" OnSelectedIndexChanged="ddlCaseFilesNew_SelectedIndexChanged" Visible="False">
                <asp:listitem>Item 1</asp:listitem>
            </asp:dropdownlist>
        </contenttemplate>
        <triggers>
            <asp:asyncpostbacktrigger controlid="ddlCaseFilesNew" eventname="SelectedIndexChanged" />
        </triggers>
    </asp:updatepanel>


<script  runat="server">
    protected void ddlCaseFilesNew_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidNewCaseFile.Value = ddlCaseFilesNew.SelectedItem.Value;
    }
</script>
 
Share this answer
 
v2
Use
ddl.SelectedItem.Value; (or)
ddl.SelectedValue
 
Share this answer
 
dropdownList_selectIndexChanged event

string SelectedAValue;
SelectedAValue = dropdownList.selectedValue;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900