Disable specific button from set of programatically created buttons?
Not knowing the exact number of labels needing to be created, I have created multiple instances of Linkbuttons "mLbtnPage" and have assigned each a different ID. On clicking a certain linklabel it successfully changes to gray. All the other linklabels are then supposed to turn blue. My method however only checks if the last created linklabel needs to be turned to blue instead of checking every label. I have been stuck on this for AGES and the only way I can think of tackling this problem is by sticking all the labels in a Gridview but Im hoping there is a simpler way!!
Code:
protected void CreateSetOfLinklabels()
{
for (int i = 1; i < miPages + 1; i++)
{
// Add label with corresponding page no
mLbtnPage = new LinkButton();
mLbtnPage.ID = i.ToString();
mLbtnPage.Text = i.ToString();
holderPages.Controls.Add(mLbtnPage);
mLbtnPage.Click += new EventHandler(lbtnPage_Click);
Literal literalSpace = new Literal();
literalSpace.Text = " ";
holderPages.Controls.Add(literalSpace);
// Current page
if (Convert.ToInt32(mLbtnPage.ID) == miSelectedPage)
{
mLbtnPage.ForeColor = System.Drawing.Color.Gray;
}
}
protected void lbtnPage_Click(object sender, EventArgs e)
{
if (sender is LinkButton)
{
LinkButton linkbutton = sender as LinkButton;
miSelectedPage = int.Parse(linkbutton.ID);
SetDefaultLinkButtonProperties();
// Set properties of selected linkbutton
linkbutton.ForeColor = System.Drawing.Color.Gray;
// Create a table with the loaded details
CreateTblReportSelection(miSelectedPage);
}
}
protected void SetDefaultLinkButtonProperties()
{
// Return all other linklabels to default color
mLbtnPage.ForeColor = mLbtnPage.ID != Convert.ToString(miSelectedPage) ? System.Drawing.Color.Blue : System.Drawing.Color.Gray;
}
#endregion
Re: Disable specific button from set of programatically created buttons?
I would suggest to use the Controls collection of that form. There i would fo through that collection and and checking the type of the control if it is a link label and doesn't have the ID of the actual control I would change the color. You dont need to do your own collection as there is already 'Controls' there to use it :D
In the creation method you need to add the labels positions !! and additional add
Code:
mLbtnPage.Name = "LinkButton";
Then in the click event do
Code:
void mLbtnPage_Click(object sender, EventArgs e) {
LinkButton lb = sender as LinkButton;
string id = lb.ID;
foreach (Control c in this.Controls ) {
if (c.GetType().Name == "LinkButton" ){
if (((LinkButton)c).ID != id ) {
((LinkButton)c).ForeColor = Color.Red;
}else{
((LinkButton)c).ForeColor = Color.Gray;
}
}
}
}
This checks which control is the selected and coloers all others to red in my example.
BTW I dont know if you are able to change LinkButtons color. ( I did no test ) LinkLabels can not change the color. So simple test it
Re: Disable specific button from set of programatically created buttons?
The following code is not recognized: mLbtnPage.Name = "LinkButton". I tried using the NamingContainer instead but I received a syntax error:
Code:
mLbtnPage.NamingContainer = "LinkButton";
Re: Disable specific button from set of programatically created buttons?
And if you don't want to do the parsing of the Controls, a simple member variable that keeps a reference on the currently selected button would do the job. On a click, just disable the old button and replace it with the new.
Re: Disable specific button from set of programatically created buttons?
I think thats what I was originally trying to do.... I have replaced the ".Name" of JonnyPoet´s method with a different method to check if the control that is currently been looped through is a Linkbutton. Only one control is however found and that is of type "ASP.input_master_master" (my master page!), not of type LinkButton! I have tried various other methods of reaching the LinkButton, incl. the use of "is LinkButton" but each time I receive the response that none exist.... strange...
Code:
void mLbtnPage_Click(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
string id = lb.ID;
foreach (LinkButton lbtn in Controls)
{
if (lbtn.ID == id)
{
lbtn.ForeColor = System.Drawing.Color.Red;
}
else
{
lbtn.ForeColor = System.Drawing.Color.PaleGoldenrod;
}
}
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
stephsh
The following code is not recognized: mLbtnPage.Name = "LinkButton". I tried using the NamingContainer instead but I received a syntax error:
Code:
mLbtnPage.NamingContainer = "LinkButton";
Tobe able to be recognized as "LinkButton" you need to set the name Property during creation of that buttons as I have already told you You need to add that line into your CreateSetOfLinkLabels Method
Code:
protected void CreateSetOfLinklabels() {
for (int i = 1; i < miPages + 1; i++){
// Add label with corresponding page no
mLbtnPage = new LinkButton();
mLbtnPage.ID = i.ToString();
mLbtnPage.Text = i.ToString();
mLbtnPage.Name ="LinkButton";
holderPages.Controls.Add(mLbtnPage);
mLbtnPage.Click += new EventHandler(lbtnPage_Click);
Literal literalSpace = new Literal();
literalSpace.Text = " ";
holderPages.Controls.Add(literalSpace);
}
}
If you dont add this, my other code will not work! If you do it will ! :D
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
TechnoGilles
And if you don't want to do the parsing of the Controls, a simple member variable that keeps a reference on the currently selected button would do the job. On a click, just disable the old button and replace it with the new.
Sorry this is shooting with a gun to ants, like if I neeed the wall painted in green let the bricklayers bbuilt up a new one :lol::lol:
And BTW it will not hange all other controls to blue !!
Re: Disable specific button from set of programatically created buttons?
The point is that "Name" is not in the LinkLabel's property list, it's not a recognized command... Maybe Im missing an import? My current imports are:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO;
using System.Collections;
Quote:
Originally Posted by
JonnyPoet
Tobe able to be recognized as "LinkButton" you need to set the name Property during creation of that buttons as I have already told you You need to add that line into your CreateSetOfLinkLabels Method
Code:
protected void CreateSetOfLinklabels() {
for (int i = 1; i < miPages + 1; i++){
// Add label with corresponding page no
mLbtnPage = new LinkButton();
mLbtnPage.ID = i.ToString();
mLbtnPage.Text = i.ToString();
mLbtnPage.Name ="LinkButton";
holderPages.Controls.Add(mLbtnPage);
mLbtnPage.Click += new EventHandler(lbtnPage_Click);
Literal literalSpace = new Literal();
literalSpace.Text = " ";
holderPages.Controls.Add(literalSpace);
}
}
If you dont add this, my other code will not work! If you do it will ! :D
Re: Disable specific button from set of programatically created buttons?
Maybe it's my simple mind at work, but what about putting your created LunkButtons into a List<LinkButton> ... in the click event, you have the Sender object, type it to a LinkButton, then loop through the List... if it matches, set the link color to gray... if it doesn't match, set it to blue.
Or am I oversimplifying it?
-tg
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
TechGnome
Maybe it's my simple mind at work, but what about putting your created LunkButtons into a List<LinkButton> ... in the click event, you have the Sender object, type it to a LinkButton, then loop through the List... if it matches, set the link color to gray... if it doesn't match, set it to blue.
Or am I oversimplifying it?
-tg
This is impossible as this would only add buttons which are clicked and you would not be able to set all the not selected buttons to blue as he wanted to do. And as we already have a Controls Collection why not simpleusing it ??? Sorry, Strange idea. :D
Re: Disable specific button from set of programatically created buttons?
Quote:
Sorry this is shooting with a gun to ants
Hum... Parsing through the Controls is somewhat dangerous. You never know what you'll find... What if other LinkButtons are also present in the page ? You'll be shooting at flies with the intent of killing ants !!
I stick to what I said. A simple member variable will do the job with the less amount of code, memory, and processing. And maximum robustness !!
Code:
private LinkButton currentlySelectedLinkLabel;
protected void CreateSetOfLinklabels()
{
// Add label with corresponding page no
for (int i = 1; i < miPages + 1; i++)
{
...
mLbtnPage.ForeColor = your "not selected" color;
...
// Current page
if (Convert.ToInt32(mLbtnPage.ID) == miSelectedPage)
{
SelectLinkLabel(mLbtnPage);
}
}
}
void mLbtnPage_Click(object sender, EventArgs e)
{
SelectLinkLabel(sender as LinkButton);
}
private SelectLinkLabel(LinkButton linkLabel)
{
if (currentlySelectedLinkLabel != null)
{
currentlySelectedLinkLabel.ForeColor = your "not selected" color;
... other processing...
}
currentlySelectedLinkLabel = linkLabel;
if (currentlySelectedLinkLabel != null) // This allows to call this method with null to deselect everything
{
currentlySelectedLinkLabel.ForeColor = your "selected" color;
... other processing...
}
}
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
stephsh
The point is that "Name" is not in the LinkLabel's property list, it's not a recognized command... Maybe Im missing an import? My current imports are:
The missing information is another thing. Is LinkButton something created by you or is it a LinkLabel?
But if you miss that Property simple add it
Code:
public class MyLinkButton: LinkButton {
public string Name{get;set;}
}
BTW LinkLabel has a Name Property so please explain what Framework are you using?
Looking at your references this basically seems to be a Web page, but the method of adding controls to the form seems to be done as we do in Windows Form projects. But in Windows form we are using LinkLabels and in Web you are using LinkButtons. So maybe show your project or what already is done and works.
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
TechnoGilles
Hum... Parsing through the Controls is somewhat dangerous. You never know what you'll find...
What if other LinkButtons are also present in the page ? ...
Thats a simple question. Who has coded the project? That one normally SHOULD know which control are available on his form. There is no 'mystery' in coding never. If you are not sure all controls would have Name Property put the code into a try-catch and you will be secure to only get controls which have a Name Property and also where the name is set to "LinkButton" You also can use another name if you want. Simple set the name and ask for that name, thats all.
But there are other moe important questions than this, so what type of project this is?
Re: Disable specific button from set of programatically created buttons?
No mystery ? Obviously !! ... All I'm saying is that the day you need to add a LinkButton in that page for whatever reason, in your case, you'll have to refactor the code somehow. In my case you won't.
I just think it is a good development rule not to assume things. I like plain and simple code that can resist to changes over time !!!
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
TechnoGilles
No mystery ? Obviously !! ... All I'm saying is that the day you need to add a LinkButton in that page for whatever reason, in your case, you'll have to refactor the code somehow. In my case you won't.
I just think it is a good development rule not to assume things. I like plain and simple code that can resist to changes over time !!!
Basically I can agree with your code in post #11
As this will work to set a selection color and to reset unselected buttons if he wants something like that. Bus as seen in your code this per sure doesn't need a List<LinkButton>
And if you would have done using such a list you would have a great need of code changes when adding something in your page too. So thats not the point IMHO.
IMHO the point is that the shown code seeml me like a code for Widows Forms, but the References he showed point to a web application. So what is he really doing here.