Solved

DrownDownList

Posted on 2011-02-24
6
310 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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: …

707 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

14 Experts available now in Live!

Get 1:1 Help Now