简体   繁体   中英

gridview on edit event , on update event?

I would like to know how I can access the edit and the update button from GridView ?
I mean, if I click the edit button from the website in the GridView , I want to get some values from the tables.
For example: the button1 event.

You could make use of the GridView rowupdating event.

protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)

The GridViewUpdateEventArgs contain properties for NewValues and OldValues .

I often use the following to cancel the update if nothing has actually been changed.

foreach (DictionaryEntry d in e.NewValues)
{
    String oldValue = e.OldValues[d.Key] == null ? "" : e.OldValues[d.Key].ToString();
    String newValue = d.Value == null ? "" : d.Value.ToString();
    if (oldValue != newValue)
    {                    
        return; // -- change detected
    }
}
e.Cancel = true; // -- don't execute the update
myGridView.EditIndex = -1;
  1. Use a rowCommand on your button in template field
  2. Check for your command name under rowCommand event
  3. Open a database connection & execute SQL/stored procedure to fetch your records

Also if you use a SQLDataSource, it can automatically do it for you if you specify right queries.

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