将下拉列表的选定值分配给会话变量 [英] assign the selected value of dropdown list to a session variable
本文介绍了将下拉列表的选定值分配给会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在动态创建下拉列表。我想将下拉列表的选定值分配给javascript中的会话变量。
我有一个javascript fn。如下:
如何完成这项任务。请帮助
function assignSession()
{ var valDropdown = document.getElementById( countName)。 value 跨度>;
<%Response.Session [ cId + valDropdown); %>
}
解决方案
创建SelectedIndexChanged事件现在在代码隐藏中的下拉列表
受保护的 void DropDownList1_SelectedIndexChanged( object sender,EventArgs e)
{
Session [ value] = DropDownList1.SelectedValue;
}
不,这是不可能的。 JavaScript是客户端脚本,会话变量是存储在服务器端。 ASP.Net将始终在JavaScript之前执行。有关详细信息,请查看:
ASP .NET应用程序和页面生命周期 [ ^ ]
ASP.NET页面生命周期概述 [ ^ ]
为了防止整页PostBack你可以使用 Asp.Net更新面板 [ ^ ]。在更新面板中保留动态创建的控件。
或者你可以使用Javascript调用Web服务 [ ^ ]并在其中分配会话变量。
- 阿米特
I'm creating the dropdown list dynamically. I want to assign the selected value of dropdown list to a session variable in javascript.
I'm having a javascript fn. as follows:
How to attain this task. Please help
function assignSession()
{var valDropdown = document.getElementById("countName").value;
<% Response.Session["cId"+valDropdown); %>
}
解决方案
Create SelectedIndexChanged Event for dropdownlist now in codebehind
Protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Session["value"] = DropDownList1.SelectedValue; }
No, it's not possible. JavaScript is client-side scripting, and Session variables are stored server-side. The ASP.Net will always execute before the JavaScript. For more details you can check:
ASP.NET Application and Page Life Cycle[^]
ASP.NET Page Life Cycle Overview[^]
To prevent entire page PostBack you can use Asp.Net Update Panel[^]. Keep your dynamic created controls in Update Panel.
Or you can Call a Web Service using Javascript[^] and assign the session variable in that.
--Amit
这篇关于将下拉列表的选定值分配给会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文