Solved

DrownDownList

Posted on 2011-02-24
6
317 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
[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
  • 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
Industry Leaders: 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!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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