在c#中多次运行RegisterStartupScript



大家好,

下面是我的部分代码

开始代码
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(info1)</script>",false);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp2", "<script>loadAdditionalInfoDialog(info2)</script>",false);
}
代码结束

loadAdditionalInfoDialog()函数会提示一个小窗口,让用户输入一些信息,然后点击"ok";

但是,当我点击Button1,我只能看到第二个RegisterStartupScript,这是loadAdditionalInfoDialog(info2)的工作,它会提示小窗口,我可以输入一些信息,点击"OK"

因此,我不能为第一个RegisterStartupScript输入信息,即loadAdditionalInfoDialog(info1)

我想问的解决方案是,当我点击Button1,我可以先输入信息为loadAdditionalInfoDialog(info1),然后点击"OK"按钮,然后继续输入loadAdditionalInfoDialog(info2)的信息。

万分感谢。

实际上Button1_Click只是一个按钮,我创建做测试。事实上,我只会调用loadAdditionalInfoDialog()当我在中继器中获得数据:

protected void btnRedeemAll_Click(object sender, EventArgs e)
    {
        foreach( RepeaterItem itm in repGiftResults.Items )
        {
            /*
            code to get all those parameter
            */
            if (pr.AdditionalFieldsEnabled == true)
                    {
                        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(1," + pr.ID + "," + giftId + ",'" + txtQty.ClientID + "'," + tokenId + ")</script>", false);
                        
                    }
        }
    }

因此,我认为这是我很难调用第二次loadAdditonalInfoDialog()当我点击"OK"按钮,因为我需要在中继器中获得许多参数。

好吧,你的代码应该工作得很好,只要确保你的info1和info2定义和修改你的loadAdditionalInfoDialog的逻辑。只是为了测试,在里面放一个警报。你也可以在一个代码块中调用这两个函数。

更新:这是我从你的评论中理解你想做的:

在重复器模板中添加一个隐藏字段

 <asp:HiddenField ID="hdf" runat="server" />

在后面的代码中,你可以尝试这样做

        foreach (RepeaterItem item in cdcatalog.Items)
        {
            // Put your condition here like  if (pr.AdditionalFieldsEnabled == true) in my case to make it simple I'm just using the index
            if (item.ItemIndex == 1)
            {
                //Get the hidden fiels to save your parameters for the next call you can add multiple parameters 1;2;3;4 and read it using js
                HiddenField hdf = item.FindControl("hdf") as HiddenField;
                hdf.Value = "info2";
                // Pass the client ID for the hidden field so you can access it in loadAdditionalInfoDialog to retrieve parameter
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), String.Format("temp_{0}",item.ItemIndex),
                                                   String.Format("<script>loadAdditionalInfoDialog('info1',{0});</script>",hdf.ClientID),
                                                    false);
            }
        }

,在你的脚本中你可以这样做:

 <script type="text/javascript">
        function loadAdditionalInfoDialog(param, hdfId) {
            // Do whatever yout want here
            var info = prompt("Please enter ", param);
            if (info != null) {
                // Do whatever you want with the info you collected 
                // This code should be in your ok click button to check whether you should call the second window.
                if (info !== undefined) {
                    loadAdditionalInfoDialog(hdfId.value);
                }
            }

        }
    </script>

我希望这对你有帮助

如果您还在寻找这样做的方法。这就是我的工作,在你的例子中实现:

protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "    <script>loadAdditionalInfoDialog(info1); loadAdditionalInfoDialog(info2); </script>",false);
}

我将两个过程合并为一个,如果你需要先做一个,然后再做第二个,它可能会有帮助

相关内容

  • 没有找到相关文章

最新更新