Lahib
Lahib

Reputation: 1365

How to Refresh GridView after closing down Dialog in ASP .NET

I have a main page with a Gridview that shows data from a Database. In the gridview there is a button, when this button is clicked a Dialog will appear allowing u to edit the selected row. The dialog is created as an aspx class. When i edit the data and close down the dialog i want to refresh my GridView on the Main page so the edited data is represented. How can i do this?

my code for editing the data and closing down the dialog is this:

 protected void editButton_Click(object sender, EventArgs e)
        {
            string alias = Request.QueryString["alias"];
            string custid = Request.QueryString["custid"];
            controller.EditDeliveries(custid, alias, tdaysField.Text, thoursField.Text, ttypeField.Text, pdaysField.Text, phoursField.Text, ptypeField.Text);
            ClientScript.RegisterClientScriptBlock(GetType(), "onUpload", "<script type='text/javascript'> window.close();</script>");
        }

Can anyone help ? And if u need to see more code please tell me.

Upvotes: 0

Views: 2398

Answers (1)

Mikey Mouse
Mikey Mouse

Reputation: 3098

Just set your datasource again and rebind it in the postback

   gvMyGrid.DataSource = myData; //Fresh from the database
   gvMyGrid.DataBind();          //Bam, fresh data

Edit: Oh and if it's another Control that is the source of the postback, you can use a Bubble Event to trigger the refresh.

Second Edit: To have the Dialog Page tell the Main Page to update that grid:

    RaiseBubbleEvent(this, new CommandEventArgs("DataUpdated", "This could be null, or I could be a message to let the user know things are updated")); 

Then on your main Page

   protected override bool OnBubbleEvent(object sender, EventArgs e)
    {

        if (e is CommandEventArgs)
        {
            var args = (CommandEventArgs)e;
            //Could use args.CommandArgument here
            switch(args.CommandName)
            {
                  case "DataUpdated":

                     gvMyGrid.DataSource = myData; 
                     gvMyGrid.DataBind();          
                     return true;                  //Handled the event LIKE A BOSS
            }
        }
        return false;                              //I didn't handle this event
    }

Upvotes: 3

Related Questions