ASP.NET 窗体间传值的方法
ASP.NET窗体间传值的方法介绍
在Web开发中,有时需要在不同的窗体间传递值,比如在父窗体和子窗体之间。以下是一个基于ASP.NET的示例,演示了如何在ParentForm和SubForm之间传递值。
在ParentForm.aspx中,有一个TextBox1文本框和一个Open按钮。当点击Open按钮时,会弹出SubForm.aspx。SubForm.aspx页面包含另一个TextBox1文本框以及一个Close按钮。点击Close按钮后,SubForm.aspx文本框的值将被返回到ParentForm.aspx的文本框中。
父窗体前台代码:
```html
function OpenSubForm(ret) {
var strPath = "subForm.aspx";
var nHeight = 500;
var nWidth = 500;
var feature = "Height=" + nHeight + ",Width=" + nWidth + ",=30,Left=30";
feature += ",dependent=yes,location=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no;";
window.open(strPath + "?Ret_Form=Form1&Ret_Value=" + ret,'subForm',feature).focus();
return false;
}
```
父窗体后台代码:
当页面加载时,调用OpenSubForm函数并传递TextBox1的值作为参数。这样,当点击Open按钮时,会打开SubForm并传递这个值。
子窗体后台代码:当子窗体的按钮被点击时,它从请求参数中获取Ret_Form和Ret_Value的值,并使用JavaScript将这些值设置回父窗体的相应文本框中。然后关闭子窗体。以下是具体的后台代码实现:
在子窗体的Button1_Click事件中:
```csharp
private void Button1_Click(object sender, System.EventArgs e)
{
string strScript = string.Empty;
string strRetForm = String.Empty;
string strRetValue = String.Empty;
strRetForm = Request.Params["Ret_Form"];
strRetValue = Request.Params["Ret_Value"];
if (strRetForm == string.Empty)
{
strRetForm = "document.forms[0]";
}
strScript = "";
Response.Write(strScript);
}
```此代码片段实现了从子窗体向父窗体传递值的功能。通过这种方式,你可以在ASP.NET的窗体间轻松传递值。希望这个例子对你有所帮助!如果你有任何其他问题或需要进一步的解释,请随时提问。
编程语言
- ASP.NET 窗体间传值的方法
- Sql Server里删除数据表中重复记录的例子
- 关于laydate.js加载laydate.css路径错误问题解决
- 解决Jstree 选中父节点时被禁用的子节点也会选中
- Laravel框架Eloquent ORM新增数据、自定义时间戳及批
- typecho插件编写教程(三)-保存配置
- 一种Javascript解释ajax返回的json的好方法(推荐)
- jquery简单实现网页层的展开与收缩效果
- php 升级到 5.3+ 后出现的一些错误,如 ereg(); ere
- 实例讲解PHP中使用命名空间
- Ajax 学习资源 中外都有
- 探究Laravel使用env函数读取环境变量为null的问题
- vue2单元测试环境搭建
- Node.js实用代码段之获取Buffer对象字节长度
- vue动态组件实现选项卡切换效果
- 实例解析jQuery中proxy()函数的用法