Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get dropdown value on page load

Posted on 2014-04-14
2
Medium Priority
?
689 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 41

Accepted Solution

by:
Kyle Abrahams earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

782 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