在c#中多次运行RegisterStartupScript

时间:2013-06-10 07:17:00

标签: c# javascript code-behind 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()功能会提示一个小窗口,让用户输入一些信息,然后点击&#34; OK&#34;按钮继续下一步。

但是,当我点击Button1时,我只能看到RegisterStartupScriptloadAdditionalInfoDialog(info2)正常工作,它会提示小窗口,我可以输入一些信息并点击& #34; OK&#34;按钮继续下一步。

因此,我无法输入第一个RegisterStartupScript的信息,即loadAdditionalInfoDialog(info1)

想要求解决方案,当我点击Button1时,我可以先输入loadAdditionalInfoDialog(info1)的信息,然后点击&#34; OK&#34;按钮,然后继续输入loadAdditionalInfoDialog(info2)的信息。

万分感谢。

实际上Button1_Click只是我创建的用于测试的按钮。 实际上,当我在Repeater中获取数据时,我只会调用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);

                    }
        }
    }

因此,我认为当我点击&#34; OK&#34;时,我很难再次调用loadAdditonalInfoDialog()。按钮,因为我需要在转发器中获得许多参数。

2 个答案:

答案 0 :(得分:1)

嗯,您的代码应该可以正常工作,只需确保定义了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>

我希望这可以提供帮助。

答案 1 :(得分:1)

如果您仍在寻找方法。 这对我有用,在你的例子中实现:

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

}

我将两个流程合并为一个,如果你需要先做一个,然后第二个就可以帮忙