代码如下:
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控件的值。请教高手应该如何解决。急急!!!!!!!
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控件的值。请教高手应该如何解决。急急!!!!!!!
解决方案 »
- 关于二个表的数据查询读取的方法
- datagrid动态生成列,如何在所有列的后面再添加个编辑列?
- 发送邮件的内容可以是一个html页吗?怎么弄
- 简单的问个问题:如何在网页中确定用户是内网用户还是外网用户,求助。
- 在DataGrid里的模板TextBox新增一行,如何使TextBox获得焦点(高分求教)
- 有1万个节点的树结构用iewebcontrols的treeview控件做是否可以,几百人同时访问可以吗?主要是担心性能不行。
- 给TextBox加了onkeyup之后的奇怪问题
- 怎么我的asp.net应用程序又建不了了. 总是这样的问题也不知道什么原因??
- 如何实现 点击一个链接 弹出一个文件保存对话框
- ASP.NET(C#)中Excel命名空间下没有Application()對象
- 一个很棘手的问题
- 问个导航站问题,类似好123
在button的onclientclick中打开dialog,取得结果,将结果赋给一个隐藏的Label.在button的Onclick中执行:
string str=this.Label1.Text;
if(str=="a")
{ }
else
{ }
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>
看起来Label的值会在postback的时候丢掉,我想因为Lable最终被翻译成HTML的Span,而Span得值无法被PostBack回去。
我使用了一个隐藏的textbox来保存返回值。
注意不能用Visible="false",asp.net的自动忽略它的viewstate.而要使用style: style="visibility:hidden"我在下面的回复中加入我的代码,和你的有点不同,你可以参考下。
<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;
}