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.
Related
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.
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 want to get the value of disabled textbox control.
First I am finding the textbox
TextBox txt1 = (TextBox)(Page.FindControl("txt1"))
Then saving the value of the textbox
decimal val1 = Convert.ToDecimal(txt1.Text.Trim().ToString())
But I am not getting any value because it is not able to find the control because my Textbox control is disabled.
Thanks in advance
TextBox might be nested inside other control. If so, you want to find it recursively.
For example,
Helper Method
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
return root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id))
.FirstOrDefault(c => c != null);
}
Usage
var txt1 = (TextBox)FindControlRecursive(Page, "txt1");
A few more details would be helpful. It should be as simple as:
string val1 = txt1.text;
You shouldn't even need to use FindControl unless there is a specific reason.
Are you sure you are getting the textbox in txt1
also
string val1 = Convert.ToDecimal(txt1.Text.Trim().ToString())
Your code is wrong.. how can you convert a decimal into a string
I'm assuming that you have a numeric value in your textbox, so try doing it like this:
decimal value = 0;
string money = String.Empty;
if(!string.IsNullOrEmpty(txtMoney.Text))
name = txtMoney.Text
value = Convert.ToDecimal(name);
That will validate your textbox to avoid a null, but will also convert it into a decimal value for it. Hope it helps.
You shouldn't need to FindControl before you can assign the value, you should be able to pass values in and out, as long as your markup has runat="server". The server side postback will provide access to the inner content of the control without a problem.
After testing, a disabled control won't push the value during a postback. You really should use the HiddenField.
However, a <asp:HiddenField> may be better suited to your requirements. Rather than a disabled textbox. Which would like like this:
string cash = String.Empty;
if(!string.IsNullOrEmpty(hfMoney.Value))
cash = hfMoney.Value;
The hidden field will always be hidden, thus it is always accessible.
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 trying to set a combobox.selectedValue to a string which works but when its nullorempty it errors out. I have tried the following code to no avail:
if (string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
The combobox is databound but in theory it could be nullorempty in certain situations and I need to be able to pass the other value at those times. Any help would be great.
You probably need:
if ((docRelComboBox.SelectedValue==null) || string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
Since SelectedValue itself might be null.
Calling ToString() when the SelectedValue is null is probably causing the error. I would try:
if (docRelComboBox.SelectedValue == null ||
string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
instead.