?
Solved

DrownDownList

Posted on 2011-02-24
6
Medium Priority
?
321 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
Technology Partners: 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 1400 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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