Solved

DrownDownList

Posted on 2011-02-24
6
316 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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