-
May 14th, 2010, 10:21 AM
#1
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
-
May 16th, 2010, 04:22 AM
#2
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
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
Last edited by JonnyPoet; May 16th, 2010 at 05:09 PM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 04:21 AM
#3
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";
-
May 17th, 2010, 05:35 AM
#4
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.
-
May 17th, 2010, 07:33 AM
#5
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;
}
}
-
May 17th, 2010, 09:39 AM
#6
Re: Disable specific button from set of programatically created buttons?
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 !
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 09:42 AM
#7
Re: Disable specific button from set of programatically created buttons?
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
And BTW it will not hange all other controls to blue !!
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 10:09 AM
#8
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;
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 !
-
May 17th, 2010, 10:31 AM
#9
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
-
May 17th, 2010, 03:48 PM
#10
Re: Disable specific button from set of programatically created buttons?
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.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 03:57 PM
#11
Re: Disable specific button from set of programatically created buttons?
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...
}
}
-
May 17th, 2010, 03:57 PM
#12
Re: Disable specific button from set of programatically created buttons?
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.
Last edited by JonnyPoet; May 17th, 2010 at 04:09 PM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 04:16 PM
#13
Re: Disable specific button from set of programatically created buttons?
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?
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 17th, 2010, 04:28 PM
#14
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 !!!
-
May 18th, 2010, 02:18 AM
#15
Re: Disable specific button from set of programatically created buttons?
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.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|