使用 VB.NET 形式的会话变量在页面之间传递值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21030565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:24:17  来源:igfitidea点击:

Pass values between pages using session variable in VB.NET form

asp.netvb.netsession-variables

提问by Nita

I am trying to create session variable for 2 items so I can use it between pages. However the variable will be the search results displayed in the form. In the code behind how can I get the value of the control.

我正在尝试为 2 个项目创建会话变量,以便我可以在页面之间使用它。但是,变量将是表单中显示的搜索结果。在后面的代码中,我如何获得控件的值。

aspx page:

aspx页面:

<asp:Label ID="txtdate" runat="server" Text='<%# Eval("Inci_date", "{0:d}")%>'></asp:Label>

VB.NetCode:

VB.Net代码:

Dim strDate As String = ""
If strDate Is Nothing Then
        strDate = DirectCast(frmUpdateIncident.FindControl("txtInciDate"), Label).Text
End If
Session("IncidentDate") = strDate

My Varialbe strDate is still empty.

我的 Varialbe strDate 仍然是空的。

回答by Jo?o Pinho

Dim strDate As String = ""

This condition is always false, since "" != Nothing, String.IsNullOrEmpty is better suited to test string emptiness conditions.

此条件始终为假,因为 "" != Nothing,String.IsNullOrEmpty 更适合测试字符串空性条件。

If strDate Is Nothing Then

Also in your label, is your label inside a databing control like GridView? If no then you cannot set your label text with server side tags, you must use code behind txtDate.Text="abc", maybe in Page_Load.

同样在您的标签中,您的标签是否在像 GridView 这样的数据控件中?如果否,则您无法使用服务器端标签设置标签文本,您必须使用后面的代码txtDate.Text="abc",也许在 Page_Load 中。

Regards.

问候。

回答by Mych

I would check that it is a date not just not empty using IsDate. So

我会使用 IsDate 检查它是否是一个不为空的日期。所以

Dim strDate As String = ""
If IsDate(txtInciDate.Text) Then
        strDate = txtInciDate.Text
End If
Session("IncidentDate") = strDate