Solved

how to get dropdown value on page load

Posted on 2014-04-14
2
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Json and ajax 1 44
c#, datatable, aspx 4 53
Best book for Internet security 4 55
Display Error Message in Asp.net Modal Validation Partial View 3 40
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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