更改Javascript函数中未更改的代码中的隐藏字段值以使用showModalDialog

时间:2013-07-24 15:04:25

标签: javascript .net vb.net hiddenfield

在我的Vb .net代码隐藏(Visual Studio 2005)中,在由click事件触发的方法中:

hdnUrl和hdnParameters是hiddenFields

Protected Sub btnExample_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExample.Click
.
.
.
hdnUrl.Value = "Mypage.aspx?i=" & SomeCveDefinedInCodeBehind.Tostring & "&u=" & OtherCveDefinedInCodeBehind.Tostring
hdnParameters.value = "resizable: no; scroll: no; center: yes; dialogHeight: 525px; dialogWidth:750px; status: no;"

ScriptManager.RegisterClientScriptBlock(Page, Page.GetType, DateTime.Now.ToString, "<script type='text/javascript'> ShowWindow(); </script>", False)

.
.
.

在我的页面中:

<asp:Content ID="Content3" ContentPlaceHolderID="cph3" runat="Server">
.
.
.
<asp:HiddenField ID="hdnUrl" runat="server" Value="" />
<asp:HiddenField ID="hdnParameters" runat="server" Value="" />
<asp:HiddenField ID="hdnResult" runat="server" Value="" />

<script language="javascript" type="text/javascript">

    function ShowWindow()
    {

      alert('i am here');

      var url = document.getElementById('<%= hdnUrl.ClientID %>').value;
      var Parameters = document.getElementById('<%= hdnParameters.ClientID %>').value;

      //For test:      
      alert(url);  //------ i need to get here: "My page.aspx?...", but i always get: ""
      alert(parameters); // i need to get here my parameters,   but i always get: ""
      .
      .
      .

      var dialogWin = window.showModalDialog(url, "some text", parameters);  //showModalDialog window, will return a data that i need in CodeBehind 
      document.getElementById('<%= hdnResult.ClientID %>').value=dialogWin.result;
      //Then i could manage the result, in code-behind      
    }

</script>

</asp:Content>

只有在我设置的隐藏字段定义中:

<asp:HiddenField ID="hdnUrl" runat="server" Value="My text" />

我可以在javascript警报中获取此文本,但我需要在代码隐藏中定义文本

感谢您的帮助和建议。 传递url和参数的另一种方法是window.showModalDialog ??? 或者在code.behind ???

中获取window.showModalDialog结果的另一种方法

1 个答案:

答案 0 :(得分:0)

观察Parameters变量的大小写。另请尝试使用RegisterStartupScript而不是RegisterClientScriptBlock。不同之处在于前者会将您的javascript放在页面底部,而后者会将其置于顶部。这将导致脚本在完全加载的文档之前运行。

ScriptManager.RegisterStartupScript(Page, Page.GetType, DateTime.Now.ToString, "<script type='text/javascript'> ShowWindow(); </script>", False)