I would like to check if the value of a DatePicker is null (== no date in the box).
By default the Text of my DatePicker is set to something like Select a date, so I can't use the Text property to check.
The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.
if (datePicker.SelectedDate == null)
{
// Do what you have to do
}
Although the default text of the DatePicker is Select a date but you can use the Text property to check by using Text.Length. Or check SelectedDate property like this:
if (datePicker.Text.Length == 0)
{
//Do your stuff
}
Or:
if (datePicker.SelectedDate == null)
{
//Do your stuff
}
to = $('#to');// hopefully you defined this yourself and the options
//I am writing coffeeScript here, below I will add pure JS
if to.datepicker('getDate') == null
//basically do your stuff here -
//using month_day_year_string (m_d_y_s) func to format a date object here and add 6 to the date's year so passed e.g. 2014 gives 2020.
return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)))
return// I have this on a call back so I am returning from the call back
In pure JS it would simply be
var to = $('#to');// hope you defined the options and datepicker
if (to.datepicker('getDate') === null) {
#do your stuff - I have an example code below as I needed this check in a callback
return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)));
}
return;// if on a call back e.g. onChange or onRender.
Related
For the sitecore item testItem how can I make sure that this item has the field "Title".
I am asking because I am creating some fields in an item's template programmatically. So a field should not be created again if it already exists.
Because with this code I can get if the field has some value or not.
testItem["Title"]
testItem.Fields["Title"]
Please check this code, you are checking if item, fields collection and field value is not null
if(testItem!= null && testItem.Fields != null && testItem.Fields["Value"] != null)
{
string name = testItem.Fields["Title"].Value;
}
Code below will return value, including Standard or Default value for the field:
if (testItem.Fields["Title"] != null && testItem.Fields["Title"].HasValue)
{
string title = testItem["Title"].Value;
}
To save having to check the field against your testItem more than once you could cast to a field and then: check the field for null, that it has a value and then retrieve the value.
The advantage here is that if you need to access the field in several places you don't have to retrieve from testItem each time.
e.g.
Field titleField = testItem.Fields["Title"];
if (titleField != null && titleField.HasValue)
{
//do something with value
string fieldValue1 = titleField.Value;
//or (see intellisense for params)
string fieldValue2 = titleField.GetValue(true);
}
I am transfering my program from a WPF to ASP.Net.
I want to check a DropDownList if it contains a Item which I did in WPF this way.
else if (!_cbSlot3.Items.Contains("Jump") && !_cbSlot4.Items.Contains("Jump"))
{
foreach (string s in Stats2)
{
_cbSlot3.Items.Add(s);
_cbSlot4.Items.Add(s);
}
}
Simply it checks if it does not contain Jump in the 2 DropDown's.
Visual Studio tells me that it want a ListItem instead of a string now when doing this.
ListControl.Items is ListItemCollection, so to check for values you indeed need to use property of ListItem similar to:
!_cbSlot3.Items.Cast<ListItem>().Contains(v => v.Value == "Jump")
Note that ListItemCollection.Contains searches for value and text of ListItem where you seem to want to check just value. See ListItem.Equals for comparison details.
You can use inbuilt function for DropDownList, those are FindByValue and FindByText. MSDN
You can use it like this - _cbSlot3.Items.FindByValue("jump") != null or _cbSlot3.Items.FindByText("jump") != null .
I have a radio button in my .aspx page and in the code behind I have written the following:
if (rbOnlyCreatedorUpdated.Checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyCreatedOrUpdated;
}
else if (rbOnlyOld.checked == true)
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyOld;
}
else
{
searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.AllChanged;
}
And I really dislike the above. It feels clumsy and unclean. I would like a value to be returned by the radio button itself (named rbOnlyCreatedOrUpdatedOnValidDate, i.e the GroupName).
Is it possible or is the above the correct way to get the value?
If you use a RadioButtonList control, you can access the selected radiobuttons value by using IdOfRadioButtonList.SelectedValue.
This will give you a string, so you will still have to do the conversion to one of the SearchCriteria class' properties yourself...
If it makes you feel any better, you can make it into a ternary operator, like so:
searchObject.CreatedOrUpdatedOnValidDate =
rbOnlyCreatedorUpdated.Checked ? SearchCritera.OnlyCreatedOrUpdated :
rbOnlyOld.checked ? SearchCritera.OnlyOld :
SearchCritera.AllChanged;
I am setting certain controls using this
divCorporateId.Style.Add("Visibility", "hidden");
How do i check what the value is in another method?
I thought something like:
if (divCorporateId.Style........ == "hidden")
{
do something
}
Something like:
if (divCorporateId.Style[HtmlTextWriterStyle.Visibility] == "hidden")
{
do something
}
You can get Css property value in C# like this
string value = divCorporateId.Style["Key"]
then you can apply your conditions based on this value.
I have a form databound to a customer object, and one of the fields is a nullable int representing a "type". This is displayed as a combobox, and the combobox is bound to the "Types" table.
When a customer with a null type is loaded into the form's datasource, the combo box displays no value, but then upon clicking it you must select a value. The form/combobox will never let you change back to a blank item (to represent "null" on the customer object).
I don't want "dummy rows" in the database, and currently do this by adding a dummy object, and nulling it out in a submit event (not clean!).
Is it possible to do this cleanly, keeping with the nullable primary key?
Additional links and resources concerning nullable databinding:
I've been told and so far it has panned out, that you should have a layer of business objects in between your database datasource and your UI where you could just add an item as shahkalpesh recommends without concern of it going into the database.
Jez Humble has some information on binding nullable types at the bottom of this post and in comments Where it suggests binding to nullable types is doable if "you explicitly set the DataSourceUpdateMode to DataSourceUpdateMode.OnPropertyChanged".
Another article on databinding Nullable types: The Joy of Code - Databinding and Nullable types in WinForms.NET
Maybe this code for binding a nullable DateTimePicker could help you find addditional solutions for this or other nullable issues.
Also check out Dan Hannan for the source of where I came up with my extension method.
/// <summary>
/// From BReusable
/// </summary>
/// <param name="dtp"></param>
/// <param name="dataSource"></param>
/// <param name="valueMember"></param>
/// <remarks>With help from Dan Hanan at http://blogs.interknowlogy.com/danhanan/archive/2007/01/21/10847.aspx</remarks>
public static void BindNullableValue(this DateTimePicker dateTimePicker, BindingSource dataSource, String valueMember,bool showCheckBox)
{
var binding = new Binding("Value", dataSource, valueMember, true);
//OBJECT PROPERTY --> CONTROL VALUE
binding.Format += new ConvertEventHandler((sender, e) =>
{
Binding b = sender as Binding;
if (b != null)
{
DateTimePicker dtp = (binding.Control as DateTimePicker);
if (dtp != null)
{
if (e.Value == null)
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = false;
// have to set e.Value to SOMETHING, since it's coming in as NULL
// if i set to DateTime.Today, and that's DIFFERENT than the control's current
// value, then it triggers a CHANGE to the value, which CHECKS the box (not ok)
// the trick - set e.Value to whatever value the control currently has.
// This does NOT cause a CHANGE, and the checkbox stays OFF.
e.Value = dtp.Value;
}
else
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = true;
// leave e.Value unchanged - it's not null, so the DTP is fine with it.
}
}
}
});
// CONTROL VALUE --> OBJECT PROPERTY
binding.Parse += new ConvertEventHandler((sender, e) =>
{
// e.value is the formatted value coming from the control.
// we change it to be the value we want to stuff in the object.
Binding b = sender as Binding;
if (b != null)
{
DateTimePicker dtp = (b.Control as DateTimePicker);
if (dtp != null)
{
if (dtp.Checked == false)
{
dtp.ShowCheckBox = showCheckBox;
dtp.Checked = false;
e.Value = (Nullable<DateTime>)null;
}
else
{
DateTime val = Convert.ToDateTime(e.Value);
e.Value = val;
}
}
}
});
dateTimePicker.DataBindings.Add(binding);
}
If your main goal is to restore the combo to blank and the value to null, just press Ctrl+0 when editing the row.
It took me two days of frantic research and heart attack risk just to find this out hidden in a small post somewhere.
The datasource which is used to bind to the Type combo, could have 1 more entry with NULL value in it.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeID Name
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 Business
2 Government
-1 NULL
You could store -1 for Customer who doesn't have a type (if it is allowed that a customer need not have a type).