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;
Related
I am very new to C# and am trying to make a form program with a lot of radio buttons, i was hoping to have something to the effect of
radioButton1.value = 1;
radioButton2.value = 2;
panel1.Value = Selected radiobuttons value;
or just something like that without having to write out so many if/switch statements.
You can use LINQ to get the selected button.
Assume that your radioButtons are in form, so the variable this means the class form.
var selectedButton = this.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
Console.WriteLine(selectedButton.Text);
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 only need this one variable in the front end of the page. I am trying to achieve something like this:
<% bool YesNo = Eval("isParent") == "True" ? true : false %>
Data binding doesnt allow me to do this
Does anyone know a way around this?
Thank you for all your answers, Instead of trying to find bool value, I made the If return as text true or false and worked my way around that.(with control properties and css classes)
My Solution
Visible='<%#(string)Eval("isParent") == "True" ? false : true %>'
class="<%#(string)Eval("isParent") == "True" ? "LegendHeader" : "" %>"
the reason I did it this way is because the data is bound to the specific fields, as Vladimir Sachek mentioned, if you do it the way I wanted to you'll have to loop the data and set variable accordingly
use DataBinder.Eval instead and specify the model you are taking the data from
<% bool YesNo = DataBinder.Eval(new{isParent = "True"}, "isParent") == "True" ? true : false; %>
<%= YesNo %>
try
var YesNo = <%= Eval("isParent").ToString() %> == "True"
Try something like
<% bool YesNo(Eval("isParent").toString()) %>
Where YesNo is a method in your code behind. Like following
public bool YesNo(string sIsParent)
{
if(sIsParent.equals("1")){ return true; } else { return false; }
}
Jack said:
it gives me error Databinding methods such as Eval(), XPath(), and
Bind() can only be used in the context of a databound control.
You can use Eval only inside Repeater, GridView and similar controls. This is because Eval
uses the current data item to retrieve the requested property (the 'IsParent')
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 am stuck on this issue and cannot seem to find a way around it.
I have a CheckBoxList control. If you did not know, the FindControl() method on the CheckBoxList control returns "this". Microsoft did it because internally they dont create many ListItem objects but just one.
Anyway, I am trying to find out if a posted back control is one of the controls in my CheckBoxList. My code looks something along the lines of:
if (!(System.Web.UI.ScriptManager.GetCurrent(Page) == null)) {
string postbackControlId = System.Web.UI.ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;
if (!string.IsNullOrEmpty(postbackControlId))
{
Control control = ControlFinder.RecursiveFindChildControl(Controls, postbackControlId);
if (!(control == null))
{ }
}
}
Is there anyway to enumerate the child controls of a CheckBoxList or find if an ID that I have is equal to one of theirs?
Thanks,
Mike
The UniqueID of a CheckBox within a CheckBoxList is the UniqueID of the CheckBoxList plus a $ plus the index of the item, so you can check whether postbackControlId is one of the CheckBox controls like this:
if (postbackControlId.StartsWith(this.checkBoxList.UniqueID + "$"))
{
int itemIndex = Convert.ToInt32(
postbackControlId.Substring(this.checkBoxList.UniqueID.Length + 1), 10);
// ...
}
If you're only looking to find out whether the postback was caused by one of the items in the CheckBoxList, you don't need to traverse the entire control hierarchy. You don't even need to drill down into the list. Something like this should work fine:
string elementID = ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;
if (elementID.Contains(chkList.UniqueID))
{
//one of the checkboxes caused the postback
}