Solved

DrownDownList

Posted on 2011-02-24
6
313 Views
Last Modified: 2012-05-11
I am populating a dropdowlist in an asp.net 4.0 form as follows. when I select any item other than the first or second item, it returns to the first item after postback and selectindexchanged event doesn't work. how can I fix the error?


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindPartrs();
        }
    }

    void bindPartrs()
    {
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["WMSConnectionString"].ConnectionString;
        SqlConnection cn = new SqlConnection(connString);

        SqlCommand cmd = new SqlCommand("getParts", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cn.Open();

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();

        da.SelectCommand = cmd;
        da.Fill(ds);

        cboPartno.DataSource = ds;
        cboPartno.DataTextField = "PartNo";
        cboPartno.DataValueField = "partInfo";
        cboPartno.DataBind();
        cboPartno.Items.Insert(0, new ListItem("---", ""));
    }

    protected void cboPartno_SelectedIndexChanged(object sender, EventArgs e)
    {
 if (cboPartno.SelectedIndex != 0)
        {
            string[] myArray2;

            myArray2 = cboPartno.SelectedValue.Split('|');

            lblPartName.Text = myArray2[0];
            lblCategory.Text = myArray2[1];
            lblModel.Text = myArray2[3];
        }
        else
        {
            lblPartName.Text = string.Empty;
            lblCategory.Text = string.Empty;
            lblModel.Text = string.Empty;
        }
    }

Open in new window


thanks

ayha
0
Comment
Question by:ayha1999
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34973790
Can you post the definition (markup) for your dropdownlist, and also the output (view source) when the page is running?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34973856
 
markup:

  <asp:DropDownList ID="cboPartno" runat="server" AutoPostBack="True" 
        onselectedindexchanged="cboPartno_SelectedIndexChanged" ViewStateMode="Inherit">
    </asp:DropDownList>
                            <asp:Label ID="lblPartName" runat="server" 
                                style="font-weight: 700; color: #0000CC;"></asp:Label>
                            <asp:Label ID="lblCategory" runat="server" 
                                style="font-weight: 700; color: #0000CC;"></asp:Label>
                               <asp:Label ID="lblModel" runat="server" 
                                style="font-weight: 700; color: #0000CC;"></asp:Label>


view source:

<select name="ctl00$ctl00$MainContent$ContentPlaceHolder1$cboPartno" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$ctl00$MainContent$ContentPlaceHolder1$cboPartno\&#39;,\&#39;\&#39;)&#39;, 0)" id="MainContent_ContentPlaceHolder1_cboPartno">
	<option value="">---</option>
	<option value="partame|Consumables|RG 11     |RG 11">A_1</option>
	<option selected="selected" value="partame10|Consumables|RG 11     |RG 11">A_10</option>
	<option value="partame|Consumables|RG 11     |RG 11">A_100</option>
	<option value="partame|Consumables|RG 11     |RG 11">A_1000</option>
	<option value="partame|Consumables|RG 11     |RG 11">A_1001</option>
	<option value="partame|Consumables|RG 11     |RG 11">A_1002</option>
...

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34973872
The problem is that most of your dropdownlist values are the same. They need to be unique otherwise the dropdown cannot distinguish which is which.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:ayha1999
ID: 34973890
but the partno is unique and at least the selected part no should there in the ddl, right?

thanks
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 350 total points
ID: 34973905
The partno might be unique, but that is only the display field. The one that matters is the DataValueField, which is the one that the DropDownList uses internally to uniquely identify each item in the list.
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 34973992
you are right. I have only test values now but at the end the SP I added + partno, so every values become unique!

thank you very much for your support.

ayha
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
.Net Web Site IIS Web.Config Content-Security-Policy 1 34
Advice on Xojo as a development tool over VB. 4 31
Variable Event ? 3 19
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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 …

939 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

9 Experts available now in Live!

Get 1:1 Help Now