I have radio buttons, around 200, whose ID are KP1, KP2, KP3... KP200. I would like to run a for loop to check if they are checked or not.
I get a crash at the line no 9:
RbId = CtrlId;
I would like to extract the Radio Button Ctrl from the string similar to how it's done using javascript ie.,
document.getElementById("<%=ID%>").
Please advise.
Code:
int i;
RadioButton RbId = null;
string CtrlId = null;
char[] KPList = new char[200];
for (i = 1; i <= 200; i++)
{
CtrlId = "KP"+i.ToString();
RbId = CtrlId;
if(RbId.Checked)
{
KPList[i] = (char)j;
break;
}
}
You can use FindControl() method in for it:
Control ctrl = this.FindControl(CtrlId);
if (ctl is RadioButton)
{
RadioButton rdBtn = ctrl as RadioButton;
// now do whatever here
if(rdBtn.Checked)
{
}
}
Try this:
foreach (RadioButton rdbtn in myDiv.Controls.OfType<RadioButton>())//Assume the RadioButtons are inside a div tag called myDiv
{
if(rdbtn.Checked)
{
....
}
}
It would be even better like this:
foreach (RadioButton rdbtn in myDiv.Controls.OfType<RadioButton>().Where(rdbtn => rdbtn.Checked))
{
}
Related
I try to find out, how it is possible to access an object through a string which has the same name as the object name.
for example, I want to change the property of n times of Buttons using for loop
public static object GetObject(string ObjectName)
{
// this Method has to return an Object through his name
}
for (int i = 1; i < 4; i++)
{
GetObject(Convert.ToString("Button" +i) ).Text = Convert.ToString(i);
}
}
}
this code has the same function of this code
Button1.Text = "1";
Button2.Text = "2";
Button3.Text = "3";
You can develop different types of applications using C#. i.e. Web, WinForms, WPF. They have different types of Control and Type. I'm assuming that you are developing a WinForms application. In that case, you can use the Controls property of a WinForm to access all the Controls of a Form.
Please check the below code block for the implementation:
public object GetObject(string ObjectName)
{
// this Method has to return an Object through his name
Control myControl = Controls.Find(ObjectName, true).FirstOrDefault();
if (myControl != null)
{
// Do Stuff
return myControl;
}
else return null;
}
private void RenameButtons()
{
for (int i = 1; i < 4; i++)
{
//GetObject(Convert.ToString("Button" + i)).Text = Convert.ToString(i);
object btn = GetObject(Convert.ToString("Button" + i));
if (btn != null) ((Button)btn).Text = Convert.ToString(i);
}
}
You will find more details about the Controls property by following this link.
you can try this
foreach (Control control in Controls)
{
var btn = control as Button;
if ( btn != null && btn.Name.StartsWith("Button") )
{
var i= btn.Name.Substring(6, 1)
//if( i.Convert.ToInt32() <4 ) //optional
btn.Text = i;
}
}
in my project i have 8 dynamic Textboxes and 8 dynamic Labels, which were created in c#.
Now i need to read the text in it and insert it in a db.
My current script looks like
Label labelname1 = this.Controls.Find("label1", false).FirstOrDefault() as Label;
Label labelname2 = this.Controls.Find("label2", false).FirstOrDefault() as Label;
Label labelname3 = this.Controls.Find("label3", false).FirstOrDefault() as Label;
.....
Is it possible, to create a while loop with a variable like:
int i = 1;
while (a < 9)
{
label Labelname+i = this.Controls.Find("label+i" + a, false).FirstOrDefault() as Label;
i++;
}
When I take the "labelname+i" it's not possible, because it isn't a string.
Thank you
Extract method then
private T FindControl<T>(string name) where T : Control {
return this
.Controls
.Find(name, false)
.OfType<T>()
.FirstOrDefault();
}
and use it in a loop (it seems you want for one):
for (int i = 1; i < 9; ++i) {
Label myLabel = FindControl<Label>($"label{i}");
if (myLabel != null) {
//TODO: Put relevant code here
}
}
Same loop if you want to enumerate TextBoxes:
// textBox1..textBox8
for (int i = 1; i < 9; ++i) {
TextBox myTextBox = FindControl<TextBox>($"textBox{i}");
if (myTextBox != null) {
//TODO: Put relevant code here
}
}
You can create a List of Labels and try like:
List<Label> labels = new List<Labels>();
for (int i=1;i<9;i++)
{
Label lbl = this.Controls.Find("label"+i.ToString(), false).FirstOrDefault() as Label;
labels.Add(lbl);
}
And if you want to access i Label you simply do:
labels[i] ...
I am c# developer and i am trying programatically to set all the radiobutton to false.
How to set all the radiobutton to IsEnabled= false ?
EDIT:Please note that i have kind of situation where i have to keep this radio.IsEnabled = false; outside the loop only so is there any way that i still can have all button IsEnable= false ?
try below code and take isEnable inside the block of code as shown below
foreach (String item in param.Component.Attributes[0].Item)
{
radio = new RadioButton()
{
Content = item,
GroupName = "MyRadioButtonGroup",
// Name = "param_"+param.Name
};
radio.Checked += (o, e) =>
{
txtblkShowStatus.Text = item;
};
sp.Children.Add(radio);
radio.IsEnabled = false;
count++;
}
You are using variable radio declared outside the function, declare it as local variable like this, and set IsEnabled each time you create it:
foreach (String item in param.Component.Attributes[0].Item)
{
RadioButton radio = new RadioButton()
{
Content = item,
GroupName = "MyRadioButtonGroup",
// Name = "param_"+param.Name
};
radio.Checked += (o, e) =>
{
txtblkShowStatus.Text = item;
};
radio.IsEnabled = false;
sp.Children.Add(radio);
count++;
}
Thats because you put the radio.IsEnabled = false; outside of the loop.
{
radio = new RadioButton()
{
Content = item,
GroupName = "MyRadioButtonGroup",
// Name = "param_"+param.Name
};
radio.Checked += (o, e) =>
{
txtblkShowStatus.Text = item;
};
sp.Children.Add(radio);
radio.IsEnabled = false;
count++;
}
I assume putting radio.IsEnabled = false; inside your loop will do the trick..
EDIT :
I am not sure I quite understand why you want to set to false in second step however you can try:
foreach (Radiobutton r in sp.Childern)
{
r.IsEnabled = false;
}
Is there a way to reference buttons using a numerical value in C#? I am trying to manipulate the text on buttons using one reusable method. Here is my current coding:
One button click method (there are a total of 16):
private void Card1_Click(object sender, EventArgs e)
{
buff = CardClick(1);
if (buff != null)
{
Card1.Text = buff;
}
}
And the reusable method (the code does have holes, it's in development):
private string CardClick(int card)
{
guesses[g++] = card; //alternate g
if ((guesses[0] != null) && (guesses[1] != null))
{
//Reset Card guesses[0]
//Reset Card guesses[1]
return null;
}
else
{
if (card > 8)
{
return map[2, card];
}
else
{
return map[1, card];
}
}
You can also use Controls.Find() to get a reference to your desired button based on its name:
int i = 1;
Control[] matches = this.Controls.Find("Card" + i.ToString(), true);
if (matches.Length > 0 && matches[0] is Button)
{
Button btn = (Button)matches[0];
// ... do something with "btn" ...
btn.PerformClick();
}
You can use an array of buttons
Button[] buttonArray = new Button[10];
You can get all the buttons from your form by Type and then extract an array:
public Button[] AllButtons()
{
var buttons = new List<Button>();
foreach (var control in this.Controls)
{
if (control.GetType() == typeof(Button))
buttons.Add((Button)control);
}
return buttons.ToArray();
}
I have created a web form in which I have take certain fields like Name, Age and two radio button list (Required and ID). I want to enable disable certain fields on the value of one radiobutton list "Required". The "Required" Radiobuttonlist has two items, "YES", "NO". If I select yes, then certain fields should get enabled disabled and vice versa.
I am able to disable the texboxes, however I am not able to disable a radiobutton list "ID" which has to list items in it as taxId and PAN. I have used the following code for it
function EnableDisableID() {
if (document.getElementById("<%=rdID.ClientID %>") != null) {
var IDList = document.getElementById('<%= rdID.ClientID %>');
var isOpenID;
if (IDList != null) {
var openSubID = IDList.getElementsByTagName("input");
for (var i = 0; i < openSubID.length; i++) {
if (openSubID[i].checked) {
openSubID = openSubID[i].value;
}
}
}
if (openSubID == 'true') {
document.getElementById('<%=fbo1RadioButtonList.ClientID %>').disabled = false;
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = false;
}
if (isOpenSubAccount == 'false') {
alert("Printing..." + isOpenSubAccount);
document.getElementById('<%=fbo1RadioButtonList.ClientID %>').disabled = true;
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = true;
}
}
}
I am able to disable the FBO1TaxId, however, I am not able to disable the radiobutton list "fbo1RadioButtonList". How will I achieve it. do I have to treat its value individually?
I got the answer. I played with my code just a bit and came to a solution below:
function EnableDisableTaxID() {
if (document.getElementById("<%=rdOpeningSubAccount.ClientID %>") != null) {
var openSubAccountList = document.getElementById('<%= rdOpeningSubAccount.ClientID %>');
var rdFbo1TaxId = document.getElementById('<%=fbo1RadioButtonList.ClientID %>');
var rdFBO1Items = rdFbo1TaxId.getElementsByTagName('input');
var isOpenSubAccount;
if (openSubAccountList != null) {
var openSubAccount = openSubAccountList.getElementsByTagName("input");
for (var i = 0; i < openSubAccount.length; i++) {
if (openSubAccount[i].checked) {
isOpenSubAccount = openSubAccount[i].value;
}
}
}
if (isOpenSubAccount == 'true') {
for (var i = 0; i < rdFBO1Items.length; i++) {
rdFBO1Items[i].disabled = false;
}
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = false;
}
if (isOpenSubAccount == 'false') {
for (var i = 0; i < rdFBO1Items.length; i++) {
rdFBO1Items[i].disabled = true;
}
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = true;
}
}
}
You will have to loop over the radio buttons and disable each one. If your buttons are in a form, and you have a reference to the form, then you can use the common name for the buttons to get a collection. Loop over the collection and set each button's disabled property.
If you might either enable or disable the buttons, you can use a condition to set the value, something like:
var rbuttons = form.radioName;
var disabled = true; // disables buttons, set to false to enable
for (var i=0, iLen=rbuttons.length; i<iLen; i++) {
rbuttons[i].disabled = disabled;
}
To enable the buttons, set the disabled variable to false.