vb.net .NET 4.5 中 Textmode Time 属性上的文本在代码隐藏中消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25295065/
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 18:02:49  来源:igfitidea点击:

Text on Textmode Time property in .NET 4.5 disappears in Code Behind

asp.net.netvb.nethtmltextbox

提问by Emi

I read many topics about this but I'm having a problem with the Textmode = "Time" property on .NET 4.5, as well.

我阅读了很多关于此的主题,但我也遇到了 .NET 4.5 上的 Textmode = "Time" 属性问题。

I have this on aspx side:

我在aspx方面有这个:

<asp:TextBox runat="server" ID="txtBoxStartTime" TextMode="Time"></asp:TextBox>

On vb.net side, when I try to get the Text value from this textbox, I have nothing but empty string ("").

在 vb.net 方面,当我尝试从此文本框中获取 Text 值时,我只有空字符串 ("")。

CDate(txtBoxStartTime.Text)

On client-side, in webInspector I can easily take the value from the textbox, but I need it in server-side...

在客户端,在 webInspector 中,我可以轻松地从文本框中获取值,但我在服务器端需要它......

Even though I try with HTML5 tags with runat="server" I encounter the same problem.

即使我尝试使用带有 runat="server" 的 HTML5 标签,我也遇到了同样的问题。

I've noticed the problem is the same with all of the Textmode Property on Framework 4.5

我注意到问题与 Framework 4.5 上的所有 Textmode 属性相同

Any suggestions/solutions on this? I truly appreciate your input!

对此有任何建议/解决方案吗?我真的很感谢您的意见!

Thanks!

谢谢!

回答by Abdu Imam

Use this Attribute: "format"

使用此属性:“格式”

<asp:TextBox ID="txtTime" runat="server" TextMode="Time" format="HH:mm" />

it will return the format of 24 for hours.

它将返回 24 小时的格式。

if your value is 10:00 PM then in server side the (txtTime.Text) value will be "22:00".

如果您的值是 10:00 PM,那么在服务器端 (txtTime.Text) 值将是“22:00”。

I Test it in Chrome and it's work

我在 Chrome 中测试它,它的工作

回答by Adam Wall

Use the HH:mmtime format:

使用HH:mm时间格式:

Time.Text = SomeTime.ToString("HH:mm");

回答by alessandro

TextBoxMode Enumeration

TextBoxMode 枚举

SingleLine mode displays the TextBox control as a single row. If the user enters text that exceeds the physical size of the TextBox control, the text will scroll horizontally. MultiLine mode displays the height of the TextBox based on the Rows property, and allows data entry on multiple lines. The text will automatically wrap if the Wrap property is set to true. If the user enters text that exceeds the physical size of the TextBox, the text will scroll accordingly and scroll bars will appear. The behavior of Password mode is similar to SingleLine mode except that all characters entered in the TextBox control are masked and are not saved in view state.

SingleLine 模式将 TextBox 控件显示为单行。如果用户输入的文本超过 TextBox 控件的物理大小,则文本将水平滚动。MultiLine 模式根据 Rows 属性显示 TextBox 的高度,并允许在多行上输入数据。如果 Wrap 属性设置为 true,文本将自动换行。如果用户输入的文本超过 TextBox 的物理大小,文本将相应滚动并出现滚动条。Password 模式的行为与 SingleLine 模式类似,只是在 TextBox 控件中输入的所有字符都被屏蔽并且不保存在视图状态中。

and then

进而

The remaining options correspond to type attribute values for the input element in the HTML5 specification.

其余选项对应于 HTML5 规范中 input 元素的 type 属性值。

So there is not any change to server side behaviours of textbox, the following code works as expected

因此文本框的服务器端行为没有任何变化,以下代码按预期工作

Markup

标记

<asp:TextBox runat="server" TextMode="Time" ID="test"></asp:TextBox>
   <asp:Button runat="server" ID="btn" Text="ok" />

C# code behind

后面的 C# 代码

protected void Page_Load(object sender, EventArgs e)
    {
      string value = test.Text;
    }

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Emi

So, to have an answer, here's the code it's on my Page.Load (for testing)

所以,要得到答案,这是我的 Page.Load 上的代码(用于测试)

enter image description hereYou can see there's not value on txtBoxStarTime.Text

在此处输入图片说明您可以看到 txtBoxStarTime.Text 上没有值

enter image description here

在此处输入图片说明

For my situation, I have this inside an update panel:

对于我的情况,我在更新面板中有这个:

<asp:TextBox runat="server" ID="txtBoxStartTime" TextMode="Time" Width="102px"></asp:TextBox>


<asp:TextBox runat="server" ID="txtBoxEndTime" TextMode="Time" Width="102px"></asp:TextBox>


<asp:Button CssClass="positive" ID="btnOtherSendRequest" runat="server" Text="&nbsp;Send Request" ToolTip="The selected request would be sent to X" OnClick="btnOtherSendRequest_Click"/>

This is the code in VB.NET:

这是VB.NET中的代码:

Protected Sub btnOtherSendRequest_Click(sender As Object, e As EventArgs)
    Try

        lblSubmitted.Text = ""
        lblValidateNotInfo.Text = ""
        hdnRequestType.Value = "OTH"

        Dim messageSuccessful As String = "Thank you for submitting your Request(s)!"
        Dim messageUnSuccessful As String = "The Request was not submitted due to some errors. Please 'Go Back' and re-send!"

        Select Case ddlRequest.SelectedValue
            Case "18" ' Schedule Update Call
                If Not (txtBoxStartDate.Text.Length > 0 And txtBoxEndDate.Text.Length > 0 And IsDate(txtBoxStartDate.Text) And IsDate(txtBoxEndDate.Text)) Then
                    ' AI - if dates are in invalid format
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertScript", "alert ('Please make sure the dates are in a valid format!');", True)
                    Exit Sub
                ElseIf Not CDate(txtBoxStartDate.Text).CompareTo(Date.Now) > 0 Then
                    ' AI - Start Date must be greater than today.
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertScript", "alert ('""Start"" date must be a future date!');", True)
                    Exit Sub
                ElseIf Not CDate(txtBoxEndDate.Text).CompareTo(CDate(txtBoxStartDate.Text)) >= 0 Then
                    ' AI - Start Date must be greater than today.
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertScript", "alert ('""End"" date must be the same as the ""Start"" date or a date after the ""Start"" date!');", True)
                    Exit Sub
                ElseIf CDate(txtBoxStartDate.Text).DayOfWeek = DayOfWeek.Saturday Or CDate(txtBoxStartDate.Text).DayOfWeek = DayOfWeek.Sunday Or CDate(txtBoxEndDate.Text).DayOfWeek = DayOfWeek.Saturday Or CDate(txtBoxEndDate.Text).DayOfWeek = DayOfWeek.Sunday Then
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertScript", "alert ('Weekends excluded!');", True)
                    Exit Sub
                Else
                    lblSubmitted.Text = messageSuccessful
                End If
            Case "19" ' Request Training Call
            Case "20" ' Request Call from Cust. Service
            Case "21" ' Other Request
                If txtBoxOther.Text <> "" Then
                    lblSubmitted.Text = messageSuccessful
                Else
                    lblSubmitted.Text = messageUnSuccessful
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "AlertScript", "alert ('Please enter your request into the textarea first!');", True)
                    txtBoxOther.Focus()
                    Exit Sub
                End If
        End Select

        Call sendRequest()


        Call JSEffect(True)
    Catch ex As Exception
        lblMessage.Text = "Error when taking the request to be inserted - " & ex.Message
    Finally
        hlServCenterPend.Text = "Pending Requests (" & getServCenterStatus("PEND").ToString & ")"
        hlServCenterComp.Text = "Completed Requests (" & getServCenterStatus("COMP").ToString & ")"
    End Try
End Sub

And Here I got the error:

在这里我得到了错误:

Public Sub sendRequest(Optional pid As Integer = 0, Optional claim As Integer = 0, Optional insured As Integer = 0)

    Dim req As New cls_services.request
    Try

        req.clinicID = CInt(MyMod.FetchStoredData("ClinicID"))
        req.pid = pid
        req.type = CInt(ddlRequest.SelectedValue)
        Select Case req.type
            Case 1, 11, 12, 13, 14
                req.status = "COMP"
            Case Else
                req.status = "PEND"
        End Select
        req.claimID = claim
        req.insuranceID = insured
        req.dueDate = req.getDueDate(req.type)
        req.other = Trim(txtBoxOther.Text)
        Select Case req.type
            Case 18
                req.fromDate = CDate(txtBoxStartDate.Text)
                req.fromTime = CDate(txtBoxStartTime.Text)//ERROR HERE! - EXCEPTION
                req.toDate = CDate(txtBoxEndDate.Text)
                req.toTime = CDate(txtBoxEndTime.Text)
        End Select
        req.fax = Trim(txtFaxTreatmentRecords.Text)
        req.createUser = Request.ServerVariables("AUTH_USER")

My solution here, is trying to store the value from textbox in a hiddenfield, then access in codebehind... but it is still a "last-measure" solution.

我的解决方案是尝试将文本框中的值存储在隐藏字段中,然后在代码隐藏中访问......但它仍然是“最后措施”的解决方案。

The fact is, as I said - On PostBack - the "Text" value disappears. Don't know why.

事实是,正如我所说的——在回发时——“文本”值消失了。不知道为什么。

Still waiting for suggestions! :)

还在等待建议!:)

回答by Emi

So,

所以,

I've been struggling with this for some hours. I did not find any "code-behind" solution. So I come with a solution. Maybe it will help you in some ways:

我已经为此苦苦挣扎了几个小时。我没有找到任何“代码隐藏”解决方案。所以我提出了一个解决方案。也许它会在某些方面帮助你:

Using JavaScript to store the value into a hiddenfield, like this:

使用 JavaScript 将值存储到隐藏字段中,如下所示:

<asp:TextBox runat="server" ID="txtBoxStartTime" TextMode="Time" Width="102px" onchange="storeStartTime()"></asp:TextBox>

<asp:HiddenField runat="server" ID="hdnStartTime"/>

JavaScript Function:

JavaScript 函数:

function storeStartTime() {
var x = document.getElementById("ctl00_mainPageBody_txtBoxStartTime");
var y = document.getElementById("ctl00_mainPageBody_hdnStartTime");
y.value = x.value;
alert(y.value);

}

}

Hope it will help if you encounter this problem!

如果您遇到此问题,希望对您有所帮助!

回答by killQuotes

Have you tried placing this in your page_load?

你有没有试过把它放在你的 page_load 中?

txtBoxStartTime.Attributes["type"] = "Time"