Solved

how to get dropdown value on page load

Posted on 2014-04-14
2
637 Views
Last Modified: 2014-04-15
I have a drop down value that gets populated via sql. How do I get the value of the drop down to appear on page load?

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            lblErrMsg.Visible = false;
            string selectSql = "SELECT * FROM tblassets INNER JOIN tblassetcustom ON tblassets.assetid=tblassetcustom.assetid Left JOIN tblsystemenclosure  ON tblassetcustom.assetid=tblsystemenclosure.assetid";
            selectSql += " Where tblassets.assetid=" + DropDownList7.SelectedValue;
            // Response.Write(selectSql);


            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["la"].ConnectionString;
            SqlCommand myCommand = new SqlCommand(selectSql, myConnection);
            SqlDataReader myReader;





            try
            {
                myConnection.Open();
                myReader = myCommand.ExecuteReader();



                myReader.Read();

                cname.Text = myReader["assetname"].ToString();
                ipaddr.Text = myReader["ipaddress"].ToString();
                lanid.Text = myReader["assetid"].ToString();
                lanid2.Value = myReader["assetid"].ToString();
                cmodel.Text = myReader["model"].ToString();
                txt_dept.Text = myReader["department"].ToString();
                txt_domain.Text = myReader["dnsname"].ToString();
                cserial.Text = myReader["serialnumber"].ToString();
                txt_assettag.Text = myReader["smbiosassettag"].ToString();
                txt_installer.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();


            }
            catch (Exception err)
            {
                lblErrMsg.Text = "Error: " + err.ToString();
            }

            finally
            {
                myConnection.Close();
            }


        }

Open in new window


markup
<asp:DropDownList ID="DropDownList7" runat="server" 
             DataSourceID="SqlDataSource4" DataTextField="assetid" 
             DataValueField="assetid" AutoPostBack="True" AppendDataBoundItems="true"
     OnSelectedIndexChanged="DropDownList7_SelectedIndexChanged"  >
 

        </asp:DropDownList>

Open in new window

0
Comment
Question by:cseink
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40000159
//ensure the list is loaded
SqlDataSource4.DataBind();

DropDownList7.SelectedIndex = DropDownList7.Items.IndexOf(DropDownList7.Items.FindByValue(myreader["assetid"].ToString()));
0
 

Author Closing Comment

by:cseink
ID: 40001483
thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

10 Experts available now in Live!

Get 1:1 Help Now