简体   繁体   中英

Refresh GridView from ClientSide (Javascript) in asp.net

I have added the Gridview control on a webPage.

I am deleting any row (one row at a time) by calling PageMethod as follow:

    <script type="text/javascript">
      function Delete_Row(){
        PageMethods.DeleteRow(row_id, GetTimeCallback, ErrorHandler, TimeOutHandler);
      }
      GetTimeCallback = function (result) 
      {
         if (result) {
            alert('Row is deleted');
            // I want to refresh the Gridview here
          }
      }
    <script type="text/javascript">

where " row_id " is primery key of the row.

It shows the alert perfectly but does not refresh the Gridview with one less deleted row.
what code should i write to Update the gridview?
NOTE: I dont want to refresh entire page.

Write CallBack Function to acheive this...You can find the Callback Functionality at http://msdn.microsoft.com/en-us/library/ms178208 and http://msdn.microsoft.com/en-us/library/ms178210

Edit:-

   protected void Page_Load(object sender, EventArgs e)
   {
    String cbReference =Page.ClientScript.GetCallbackEventReference(this,
        "arg", "ReceiveServerData", "context");
    String callbackScript;
    callbackScript = "function CallServer(arg, context)" +
        "{ " + cbReference + ";}";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
        "CallServer", callbackScript, true);

   }


 System.IO.StringWriter strDataGridHtml= new System.IO.StringWriter(); 

 public void RaiseCallbackEvent(String eventArgument)
    {
         string idToBeDeleted=eventArgument;
         //Write deleteCode
         //DataBind the Grid
         HtmlTextWriter htwObject = new HtmlTextWriter(strDataGridHtml);
         GridViewControl.RenderControl(htwObject);
    }        

public String GetCallbackResult()
    {
        return strDataGridHtml.ToString();
    }

Now as you see this strDataGridHtml will be sent to Javascript Function ReceiveServerData...

<script type="text/ecmascript">

    function ReceiveServerData(rValue)
    {   
        document.getElementById("divIDWhichEncapsulategridView").innerHTML = rValue;

    }
  </script>

Hope this Will Help you..As i don't i have your full code i can't write the exact one...but this should give you some idea on how to proceed...And also please go through the "CallBack" Functionality in order to understand this functionality to the fullest..

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