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

I read a lot of topics about this, but I had a problem with the Textmode = "Time" property on .NET 4.5.

I have this on the aspx side:

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

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

 CDate(txtBoxStartTime.Text) 

On the client side, in webInspector, I can easily take the value from the text box, but I need it on the server side ...

Even if I try to use HTML5 tags with runat = "server", I am facing the same problem.

I noticed that the problem is the same with all Textmode property in Framework 4.5

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

Thanks!

+5
source share
6 answers

Enumeration TextBoxMode

SingleLine mode displays a TextBox as a single line. If the user enters text that exceeds the physical size of the TextBox control, the text will scroll horizontally. MultiLine height TextBox modes are displayed based on the Rows property and allows data recording on multiple lines. The text will be automatically wrapped if the Wrap property is 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 the password mode is similar to SingleLine, except that all characters entered in the TextBox control are masked and are not saved in the viewing state.

and then

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

Thus, there are no changes regarding server-side behavior in the text box, 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 for

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

enter image description here

enter image description here

+2
source

Use this attribute: "format"

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

it will return 24 format in a few hours.

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

I test it in Chrome and work

+2
source

Use the time format HH:mm :

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

Have you tried posting this on your page_load?

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

So, to get the answer, here is the code that it is on my .Load page (for testing)

enter image description here You can see that there is no value on txtBoxStarTime.Text

enter image description here

In my situation, I have this inside the 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 so 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 a value from a text field in a hidden field, then access codebehind ... but it is still a "last resort" solution.

The fact is that, as I said, in PostBack, the value "Text" disappears. I do not know why.

Still waiting for suggestions! :)

0
source

So,

I struggled with this for several hours. I did not find any "code" solution. So I came up with a solution. Perhaps this will help you with something:

Using JavaScript to store a value in a hidden field, for example:

 <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 this helps if you run into this problem!

0
source

Source: https://habr.com/ru/post/1200265/


All Articles