简体   繁体   中英

How to save in the dropdown the value choosed?

  • I have a simple gridview and a column with dropdownlist.
    • The dropdownlist is containing 2 values : Yes/No.
    • If I choose Yes //my code will execute but always the No value is at the top and I can't choose it .

How can I make if I choose Yes , the value to be saved in the dropdown everythime I click edit ?

I know I asked once and I used if selected value = 0 ( Yes ) //my code will execute but it's not bringing Yet at the top ,

 DropDownList id = (DropDownList)sender;
            GridViewRow row = GridView1.Rows[GridView1.EditIndex];
            if (id.SelectedIndex == 1)
            {
                ((BoundField)GridView1.Columns[5]).ReadOnly = false;
                ((BoundField)GridView1.Columns[6]).ReadOnly = false;
            }
            if (id.SelectedIndex == 0)
            {
               ((BoundField)GridView1.Columns[5]).ReadOnly = true;
                ((BoundField)GridView1.Columns[6]).ReadOnly = true;
            } 

Try to set data source for dropdownlist only once on GET request. This issue happens cause you set data source everytime and you didn't persist selected item information.

 protected override OnLoad(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Ddl.DataSource = new DataSource();
            DdlIssues.DataBind();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM