玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

ASP.net/javascript異步資料存取

Posted on 2007-09-20 10:27 玄铁剑 阅读(480) 评论(0)  编辑 收藏 引用 所属分类: Javascript
//document.getElementById("ctl00_ContentPlace_lnkTest").click();Execute linkbutton

aspx:
           
 <table>
  <tr><td id="tdGridData">
     <asp:GridView ID="gvData" runat="server">
       </asp:GridView>
     </td></tr>
  </table>    
      
      
  function RefreshData()
  {  
     var strPara = "...";//User $ split multiParameters
     callToServer(strPara,"MarkOne");
  }  
              
  function ReceiveServerData(result, content)
  {
      if( content == "MarkOne")
      {   
          if (result!="")
          {
              var tdGridData = document.getElementById("tdGridData");
              tdGridData.innerHTML  = result;
          }
          else
          {
              alert("No data to show.");
          }
      }
  }               
 
 
CS:
public partial class frmDemo : System.Web.UI.Page, ICallbackEventHandler
{
    public string returnString="";

    protected void Page_Load(object sender, EventArgs e)
    {
        RegEventEntry();
        if (!IsPostBack)
        {
            DataTable dt = InitialFromDB();
            gvData.DataSource = dt;
            gvData.DataBind();            
        }
    }

    protected void RegEventEntry()
    {
        if (Page.ClientScript.IsClientScriptBlockRegistered("callToServer") == false)
        {
            String cbReference =
                Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context", true);
            String callbackScript;
            callbackScript = "function callToServer(arg, context)" +
                             "{ " + cbReference + "} ;";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                                                        "callToServer", callbackScript, true);
        }
    }

    string ICallbackEventHandler.GetCallbackResult()
    {
        return returnString;
    }

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        if (eventArgument.Trim().Equals("") == false)
        {
            string[] strArgs = eventArgument.Split('$');
                        DataTable dt = getDataFromDB();
            gvData.DataSource = dt;
            gvData.DataBind();
            HtmlTextWriter htw = new HtmlTextWriter(new StringWriter());
            gvData.RenderControl(htw);
            htw.Flush();
            htw.Close();
            returnString = htw.InnerWriter.ToString();
        }
        else
        {
            returnString = "";
        }
    }
}  
 
只有注册用户登录后才能发表评论。