updatepanel注册js

来源:互联网 发布:xstream java 编辑:程序博客网 时间:2024/06/05 17:37

1、关于updatepanel注册js 最近在项目里需要用到altas,本人也是新手,老用最简单的updatepanel,在注册脚本时也遇到了困难,无法注册

。本来是在updatepanel中放了一个gridview,偶想在girdview中一个模板列点击弹出一个窗体,注册window.open()来解决问题。本来不是在

updatepanel中,所以用ClientScript.RegisterStartupScript直接注册挺好使。 在拖入updatepanel后发现无法注册脚本,想想

RegisterStartupScript本来是在页面加载时启动js的,在updatepanel中部分刷新,肯定是无法注册的。
后来发现了ScriptManager.RegisterStartupScript方法,挺好使,呵呵。 ScriptManager.RegisterClientScriptBlock(UpdatePanelName,

typeof(UpdatePanel), "标识key", "脚本", true); 下面是一个demo,
模板列定义如下:
<asp:TemplateField HeaderText="客户ID">
<ItemTemplate>
<asp:LinkButton ID="linkbtnCID" runat="server" Text='<%# Eval("CID") %>' CommandName="linkbtnCID" > </asp:LinkButton>

</ItemTemplate>
</asp:TemplateField>
在GridView对应的RowCommand事件中如下操作:
protected void gvClientInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
 //如果是linkButton被点击
 if(e.CommandName.Equals("linkbtnCID"))
 {
  LinkButton lbtn = (LinkButton)e.CommandSource;
  GridViewRow dgRow = (GridViewRow)lbtn.Parent.Parent;
  string tmpText = lbtn.Text.ToString();
  tmpText ="window.open('customerDetailsInfo.aspx?CID=" + tmpText + "' ,'newwindow','height=550, width=700, menubar=no ')";
  ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "click", tmpText, true); } }
  2、关于RegisterStartupScript,RegisterClientScriptBlock RegisterStartupScript 将 js嵌入到页面的底部,</form> 的前面

RegisterClientScriptBlock 将 js嵌入到页面中开启元素 <form> 后面

 


 protected void btnRecving_Click(object sender, EventArgs e)
        {
            string content = CheckRecvingSelected();
            if(content != string.Empty)
            {
                //其中注册的script的,不要带标记对,true表示自动加script标记对
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('" + content + "');",

true);
                return;
            }
        }

 

ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('请输入关键词!')", true);

ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "click", "window.open('index.aspx');", true);
运用的方法就是给UpdatePanel注册JS脚本!