Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with selecting values from dropdowjn boxes

Status
Not open for further replies.

wrexhamafc234

Programmer
Oct 23, 2006
18
GB
Hello,
im having some problems selecting values from dropdown boxes:

Code:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<script runat="server">
    protected void AddToList()
    {
        DropDownList1.Items.Clear();
        DropDownList2.Items.Clear();

        ListItem item1 = new ListItem();
        ListItem item2 = new ListItem();

        item1.Text = "0";
        item1.Value = "0";
        DropDownList1.Items.Add(item1);
        DropDownList2.Items.Add(item1);

        item2.Text = "1";
        item2.Value = "1";
        DropDownList1.Items.Add(item2);
        DropDownList2.Items.Add(item2);
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AddToList();
        }
        
        DropDownList1.SelectedIndex = 1;
        DropDownList1.DataBind();

        DropDownList2.SelectedIndex = 0;
        DropDownList2.DataBind();
    }
</script>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        </asp:DropDownList>
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Both dropdown selected values are exactly the same, they shouldnt be.
Any ideas why the code is not selecting the correct values?
 
Put these lines in your If(!IsPostBack) block:

DropDownList1.SelectedIndex = 1;
'' DropDownList1.DataBind();

DropDownList2.SelectedIndex = 0;
'' DropDownList2.DataBind();

Also, you don't need the databind event called. You are not binding any data to the ddls.

Jim
 
ok, ive changed my code to the following, however, still same error:

Code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AddToList();
            DropDownList1.SelectedIndex = 1;
            DropDownList2.SelectedIndex = 0;
        }
    }
 
do you are saying when the page first loads both ddls have the same selected index?
 
yes, both ddls have the same selected value (0). However, this should not be as im selecting different values:

Code:
DropDownList1.SelectedIndex = 1;
DropDownList2.SelectedIndex = 0;

e.g. in the above example, i want ddl1 to select 0 (selectedindex = 0) and ddl2 to select 1(selectedindex = 1).
 
I am having the same issue on page load. Once that is done, there isn't a problem changing and getting the selected index. This is very strange. The only thing I can think of is that we are setting the same items to the each ddl and somehow they are linked, but why that is? I have no idea. I will see if I can find an answer. Post back if you figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top