代码如下:
string js=@"<script language='javascript' defer='true'>
var ReturnValue=window.showModalDialog('Test3.aspx?id=','测试', 'dialogWidth:350px;dialogHeigh:350px;');
document.getElementById('" + Label1.ClientID + "').innerText=ReturnValue;</script>";
Response.Write(js);
string str=this.Label1.Text;
if(str=="a")
{}
else
{}
ReturnValue可以返回值,但是因为js、cs执行顺序问题,导致无法取到Label1控件的值。请教高手应该如何解决。急急!!!!!!!

解决方案 »

  1.   

    把js代码放Label1控件后面不行吗??
      

  2.   

    这个思路是不是可以。假设button的OnClick中执行上面的代码。将Javascript放到aspx中去。
    在button的onclientclick中打开dialog,取得结果,将结果赋给一个隐藏的Label.在button的Onclick中执行:
    string str=this.Label1.Text; 
    if(str=="a") 
    { } 
    else 
    { } 
      

  3.   

    参考:
    Page.Response.Write("<script>var response;</script>");    Page.RegisterStartupScript("RegisterStartupScript","<script>var RegisterStartupScript;</script>");    Page.RegisterClientScriptBlock("RegisterClientScriptBlock","<script>var RegisterClientScriptBlock;</script>");   Response.Write输出后的位置在源文件的第一行.RegisterClientScriptBlock输出后的位置会在<form>的下一行.(在asp.net自带的脚本和一些隐藏域之下).RegisterStartupScript输出后的位置会在</form>的上一行.RegisterClientScriptBlock的原型与RegisterStartupScript相同,两个函数不同在于将其包含的脚本代码写入到HTML文件的不同位置.RegisterClientScriptBlock在 Page 对象的元素的开始标记后立即发出客户端脚本,RegisterStartupScript则是在Page 对象的元素的结束标记之前发出该脚本。如果你的脚本有与页面对象(doucument对象)进行交互的语句,则推荐使用 RegisterStartupScript,反之如果要想客户端脚本尽可能早的执行,则可以使用RegisterClientScriptBlock或 Response.Write。RegisterClientScriptBlock一般返回的是客户端函数的包装,而RegisterStartupScript返回得函数在 document装载完成后会执行,类似于我们平时所说的body onload="f()"里面的函数;这两个方法在客户端呈现的代码位置不同,RegisterClientScriptBlock在<form runat=server>之后,而RegisterStartupScript在</form>之前。补充例子:
    ....
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form name="Form1" method="post" action="WebForm6.aspx" id="Form1">
               <input type="hidden" name="__VIEWSTATE" value="dDw3MzU1MTQ5MzY7Oz6nugrEg+5T6RC7MTIuLoIrMLQLPw==" />
    <script>function block(){}</script>    ------------>RegisterClientScriptBlock()方法输出的在这里
    <input name="TextBox1" type="text" id="TextBox1" style="Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 120px" />            <script>function startup(){}</script> ------------>RegisterStartupScript()方法输出的在这里</form>
    </body>
    </HTML>
      

  4.   

    我调试了一下,可能下面是你取不到值的原因。document.getElementById('" + Label1.ClientID + "').innerText=ReturnValue; </script>"; 
    看起来Label的值会在postback的时候丢掉,我想因为Lable最终被翻译成HTML的Span,而Span得值无法被PostBack回去。
    我使用了一个隐藏的textbox来保存返回值。
    注意不能用Visible="false",asp.net的自动忽略它的viewstate.而要使用style: style="visibility:hidden"我在下面的回复中加入我的代码,和你的有点不同,你可以参考下。
      

  5.   

    HTML:<head runat="server">
        <title></title>
        <script language="javascript">
            function openwindow() 
            {
                var ReturnValue = window.showModalDialog('Test3.aspx?id=', 'test', 'dialogWidth:350px;dialogHeigh:350px;');
                alert(ReturnValue);
                document.getElementById('<%=TextBox1.ClientID%>').innerText = ReturnValue;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="openwindow();"/>
        
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        
            <asp:TextBox ID="TextBox1" runat="server" style="visibility:hidden"></asp:TextBox>
        
            <br />
        
        </div>
        </form>
    </body>
    </html>C#
    protected void Button1_Click(object sender, EventArgs e)
            {
                Label1.Text = TextBox1.Text;
            }