Solved

how to get dropdown value on page load

Posted on 2014-04-14
2
643 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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