简体   繁体   中英

How to keep values in textbox inside a binded GridView on ASP.NET?

I have a gridView , and inside is there exist a modifiable textbox .

I also have a dropdownlist , where if I click the dropdownlist , the system will read the database in SQL and display it in gridView .

How do I keep the number I previously typed in the first row of the gridView , when a new data is added after the dropdownlist is clicked?

Normally, the previously entered value of the textbox will be resetted if i rebind the gridView witthout refilling the textbox with a saved list or behaviour

Thank you very much in advance

I'm assuming you are using something like the OnSelectedIndexChanged event of the DropDownList. In there you can use FindControl to find the TextBox, store the data, rebind the GridView and set the value back to the TextBox. You need to use FindControl twice, one for the old and once for the new TextBox.

Note the use of oldValues.Count instead of GridView1.Rows.Count . If there were less rows in the new dataset than in the old you would get Index out of Bounds error.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    List<string> oldValues = new List<string>();

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        TextBox tbOld = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
        oldValues.Add(tbOld.Text);
    }

    //rebind gridview here

    for (int i = 0; i < oldValues.Count; i++)
    {
        TextBox tbNew = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
        tbNew.Text = oldValues[i];
    }
}

You have used dropdownlist and textbox inside gridview?

if you click dropdownlist select value then postback to server and textbox value reset?

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