cant get the value of a readonly textbox in asp.net - asp.net

i have an asp,net textbox like this
<asp:TextBox readonly="true" ID="txtLastService" runat="server"></asp:TextBox>
when i tried to get its value to a date variable LastService in code behind, i get this error
Conversion from string "" to type 'Date' is not valid.
Exception
Details: System.InvalidCastException: Conversion from string "" to
type 'Date' is not valid.
Source Error: Line 26: oItem.LastService = txtLastService.Text Source File: .\admin\vehicle\add.aspx.vb Line:
26
i have used this same code in other pages, and it works fine, except for this page
any help
EDIt
Please note that the value of the textbox is not empty when i click submit on the form. And also when i remove the readonly attributes, i don;t get the error. But i need this textbox to be readonly since am using javascript to select date and setting its value

Instead of Readonly="true", try using Enabled="false"

The error seems extremely self-explanatory to me. The string is "" and you are trying to convert it to a Date. So how the heck can an empty string be converted to a Date? What date would that be?

Why not use a HiddenField instead?
<asp:hiddenfield id="txtLastService" runat="server"/>

Try using Request.Form[txtLastService.UniqueID] if C# and
Request.Form(txtLastService.UniqueID) if vb.net

Related

Can I Define an Filteredtextboxextender FilterType(s) using the web.config file

So I have a scenario where I need to change the filter type of an Filteredtextboxextender based on a value in the config file in my asp.net project.
This change propagates through about 80 input forms currently.
Since my web.config value is a string I need to do an if statement on every page to define the filter type.
e.g.
If ConfigurationManager.AppSettings("SomeVariable") = "A" Then
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.LowercaseLetters Or AjaxControlToolkit.FilterTypes.UppercaseLetters Or AjaxControlToolkit.FilterTypes.Numbers
ElseIf ConfigurationManager.AppSettings("SomeVariable") = "B" Then
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.Numbers
Else
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.Custom
txtLTWo_Filteredtextboxextender.ValidChars = "!QW"
End If
The problem here is that a change to a filter type for a particular result would have me changing every page.
I would really like a way to define it in the config file directly.
Something like this, but obviously doesn't work.
txtML_Wo_Filteredtextboxextender.FilterType = ConfigurationManager.AppSettings("FilterType")
The only way to do it that I can come up with is to keep the filter as AjaxControlToolkit.FilterTypes.Custom
And then define the string of valid characters in the config file. However this would be a large string and seems senseless when the filter has the attributes I need.
Any ideas to streamline this would be great, thanks in advance.
Hum some ideas to float here:
On page pre render (not 100% sure of the event), the you can/could inject that code.
Or you simple have a global function in a code module that fetches the config settings you have, and then use this:
this:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender"
runat="server" BehaviorID="TextBox1_FilteredTextBoxExtender"
TargetControlID="TextBox1"
FilterType="LowercaseLetters" >
</ajaxToolkit:FilteredTextBoxExtender>
becomes:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender"
runat="server" BehaviorID="TextBox1_FilteredTextBoxExtender"
TargetControlID="TextBox1"
FilterType="<%= MyFilterType() %>" >
</ajaxToolkit:FilteredTextBoxExtender>
Or say Module1.FilterType().
So, we could say have a global vb function, and then use a sever side expression in the markup settings.
I don't know what (if) those values like Filter Type "LowercaseLetters" are string or enum???
However, a server side expression in the markup could and should work.
Public Function MyFilterType() as string ???
code here to fetch the setting from config
return strSetting
End Function

Serve Tag not well formed when using EVAL() in OnClientClick event

I'm trying to create an onclick event like so
onClientClick="document.querySelector('[KPI="31"]').value = '';return false;"
The KPI= value has to be data bound, so I use code like so
onClientClick="<%# document.querySelector('[KPI=Eval("KPI_ID")]').value = '';return false; %>"
But I get an error saying the server code is not well defined. Could anyone help please..
If your datasource is a collection of objects and you are requesting a property called KPI_ID you can avoid using Eval and use Item or Container.DataItem.
Easiest way for me to show this is Container.DataItem, since I don't know what kind of control you are using to bind to. Container.DataItem should be your object, so you only need to cast it to your dataType
onClientClick="document.querySelector('[KPI=<%# ((YourDataType)Container.DataItem).KPI_ID %>)]').value = '';return false;"
If you use a Repeater in .Net 4.5 you can set the ItemType for the Repeater to your DataType so you can replace ((YourDataType)Container.DataItem) to Item:
onClientClick="document.querySelector('[KPI=<%# Item.KPI_ID %>)]').value = '';return false;"
If you are not using a property from an object and KPI_ID is a string to use to get the actual value, e.g. if it's in a DataRow, you have to avoid using " in the whole string, without letting the compiler know that it's not the end of the attribute. You can do that by using ' to indicate start and end of attribute and use string.Format so the single quote ' in your javascript is part of the text and the compiler knows it's not the end of the attribute:
onClientClick='<%# string.Format("document.querySelector('[KPI={0}]').value = '';
return false;", Eval("KPI_ID")) %>'
UPDATE:
To support the the additional double quote you can do:
onClientClick='<%# string.Format("document.querySelector('[KPI=\"{0}\"]').value = '';
return false;", Eval("KPI_ID")) %>'
You can replace Eval("KPI_ID") by Item.KPI_ID or ((YourDataType)Container.DataItem).KPI_ID if you wanna avoid using Eval.

ASP.NET 4.5 TextBox TextMode

It's possible to block past dates in ASP.net TextBox TextMode = "DateTime" or "Date"
I assume this is WebForms, in which case yes, you can use a RangeValidator. Like so:
<asp:TextBox runat="server" ID="dateField" />
<asp:RangeValidator runat="server" Type="Date" ControlToValidate="dateField"
MaximumValue="9999/12/28"
MinimumValue="1000/12/28"
ErrorMessage="enter valid date" />
You'll need to set the MinimumValue attribute as appropriate. You can do this from your codebehind, or an in-page <script> block:
this.rangeValidator.MinimumValue = DateTime.UtcNow.Subtract( new TimeSpan(13,0,0) ).ToString("G", CultureInfo.InvariantCulture);
Note my use of UtcNow and subtracting 13 hours to get a true, globally-acceptable, minimum date value. You might want to change this as appropriate for your application (such as restricting it to the timezone of your server provided that's what your users expect).
This post should help you
Input element specification The min attribute, if specified, must
have a value that is a valid global date and time string. The max
attribute, if specified, must have a value that is a valid global date
and time string
You will probably do this from code behind
yourTextBox.Attributes.Add("min", DateTime.Now.ToString());

Inline in aspx, stripping date off of datetime

I think if I just show you the following what I'm asking will make sense.
In a link in my aspx, this works:
' title='<%# Container.DataItem["EventTime"].ToString()
But the above contains the date portion of the string.
This does not work (nor does any variant of it):
' title='<%# Container.DataItem["EventTime"].ToString("hh:mm:ss tt",
CultureInfo.InvariantCulture) %>
Evantually, we want the title/hover for the link to read something like "4:30 PM : #Forbes Field"
Everything is working with the exception of the 1/1/1900 being in front of the 4:30
How do I get the date off that EventTime datetime field?
The DataItem indexer returns an object reference, so you can't use DateTime specific methods without casting it. You can also use the String.Format method:
<%# String.Format("{0:hh:mm:ss tt}", Container.DataItem["EventTime"]) %>

How do I deal with null or string.Empty when ASP.NET Repeater renders Binder.Eval Table TDs?

Here's my situtation. I have a Repeater bound to a List, which is rendering Table rows. I'm using <%# DataBinder.Eval(Container.DataItem, "SomeStringProperty")%> to display my data. The problem is that when the SomeStringProperty is null, string.Empty, or " " (some whitespace), the border for my TD is not getting rendered. Its as if its empty and quite ugly. I'm not sure how to handle this. Googling hasn't yielded the ans. Maybe someone here can tell me. Thanks..
I would replace the call to DataBinder.Eval with a Literal control and handle the Repeater's ItemDataBound event (OnItemDataBound). In the handler, check if the item is null and return a non-breaking space if it is.
You could also wrap the Eval in a method call for the same effect:
<%# CheckNull(Eval("SomeStringProperty")) %>
in the code behind:
protected string CheckNull(object value)
{
return string.IsNullOrEmpty(value) ? "SEE BELOW" : value.ToString();
}
I can't get this to display correctly -- SEE BELOW should be replaced with
You may need to compare the value to System.DBNull.Value in addition to string.IsNullOrEmpty if it's coming from a database.

Resources