Emi
Emi

Reputation: 1215

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

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

I have this on aspx side:

<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 ("").

CDate(txtBoxStartTime.Text)

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

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

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

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

Thanks!

Upvotes: 6

Views: 15141

Answers (6)

Abdu Imam
Abdu Imam

Reputation: 413

Use this Attribute: "format"

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

it will return the format of 24 for hours.

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

I Test it in Chrome and it's work

Upvotes: 5

Adam Wall
Adam Wall

Reputation: 21

Use the HH:mm time format:

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

Upvotes: 2

Emi
Emi

Reputation: 1215

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:

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

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

JavaScript Function:

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!

Upvotes: 0

Emi
Emi

Reputation: 1215

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

enter image description here You can see there's not value on 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:

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! :)

Upvotes: 0

invernomuto
invernomuto

Reputation: 10211

TextBoxMode Enumeration

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.

and then

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

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

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

enter image description here

enter image description here

Upvotes: 2

killQuotes
killQuotes

Reputation: 178

Have you tried placing this in your page_load?

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

Upvotes: 0

Related Questions