Refresh GridView from ClientSide (Javascript) in asp.net

依然范特西╮ 提交于 2020-01-03 04:14:27

问题


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.


回答1:


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..



来源:https://stackoverflow.com/questions/12215384/refresh-gridview-from-clientside-javascript-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!