Solved

how to get dropdown value on page load

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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