从asp.net中的ClientSide(Javascript)刷新GridView

时间:2012-08-31 12:26:33

标签: c# javascript asp.net gridview

我在网页上添加了Gridview控件。

我通过调用 PageMethod 删除任何行(一次一行),如下所示:

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

其中“ row_id ”是该行的主键。

它完美地显示了警报,但没有用少一个删除的行刷新Gridview。
我应该写什么代码来更新gridview?
注意:我不想刷新整个页面。

1 个答案:

答案 0 :(得分:1)

编写回调功能以实现此功能...您可以在http://msdn.microsoft.com/en-us/library/ms178208http://msdn.microsoft.com/en-us/library/ms178210

找到回拨功能

修改: -

   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();
    }

现在您看到 strDataGridHtml 将被发送到Javascript函数ReceiveServerData ...

<script type="text/ecmascript">

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

    }
  </script>

希望这会帮助你..因为我没有你的完整代码我不能写出确切的代码......但这应该会让你知道如何继续......还请通过“CallBack”功能,以充分理解这一功能..