skdeveloper
skdeveloper

Reputation: 107

How to select item from dropdown list?

I have a drop-down list inside a Gridview:

ASPX:

<ItemTemplate>
<asp:DropDownList ID="status" runat="server" 
     onselectedindexchanged="status_SelectedIndexChanged" 
     ondatabinding="status_DataBinding" ondatabound="status_DataBound">
     <asp:ListItem>-select-</asp:ListItem>
     <asp:ListItem>Winner</asp:ListItem>
     <asp:ListItem>Blocked</asp:ListItem>
     </asp:DropDownList>
</ItemTemplate>

I added my code to fill the DB, with values from page, when I select an item from the list. However, it inserted the first item " - select- ", even though I selected a different item from the list. How can I solve this issue ?

Code:

protected void Open_Click(object sender, EventArgs e)
    {
        DropDownList DDL =new DropDownList();
        for (int i = 0; i < Data.Rows.Count; i++)
        {
           DDL = (DropDownList)Data.Rows[i].FindControl("status");

        }

        LinkButton linl = sender as LinkButton;
        fb_ID = linl.CommandName;

            fbgame.Updatestatus(fb_ID, false,DDL.SelectedIndex.ToString() );

            Data.DataSource = fbgame.GetAll();
            Data.DataBind();

  }

Upvotes: 1

Views: 441

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

Check in your Page_Load that you are only binding when the page is not a postback (i.e. first time it loads), like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Only bind here
    }
}

Upvotes: 1

Related Questions