-
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.
-
Re: Disable specific button from set of programatically created buttons?
Thank you all very much for your comments! I'll have a read through and try those different methods out.... I have created an ASP.Net/C# Web Application in MS Visual C# V9.0 2008 and am thus using LinkButtons.
P.s. Steph is just my nickname, I'm actually female :P
-
Re: Disable specific button from set of programatically created buttons?
My event handler no longer fires... I tried deleting the whole page and creating a new one but no luck there...
Code:
protected void CreateSetOfLinklabels()
{
...
mLbtnPage.Click += new EventHandler(mLbtnPage_Click);
}
private void mLbtnPage_Click(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton; // Breakpoint not reached!
string id = lb.ID;
SData.miSelectedPage = int.Parse(id);
SelectLinkLabel(sender as LinkButton);
}
-
Re: Disable specific button from set of programatically created buttons?
Excuse my request but something is strange for me in the whole thing. But I normally don't do asp.net but C# Windows forms most times, so I'm really mnot very familar with asp.net and HTML pages.
As much as I know about HTML pages they all are created as a stream of text so how do you create your linkbuttons by use of code like you did? On the other hand you write that you have creted a new page, so whats the background here?
I remembered to a page containing a repeater and there we also had LinkButtons on it, but as much as I remember this was very challenging in that time and there was a property of the repeater which contains a wrong value and then the linkbuttons refused to fire. But this is more then 3 years ago so I really forgot how this worked and what in the end was the problem.
But a really dynamically created page is done totally other then you showed in your code as you really need to create the whole HTML text by code within all tags position commands and all that.
So please tell me, what is the purpose of your code and what is already working and what it fails to do. Also explain what sort of Control is 'holderpages' where you add your LinkButton
-
Re: Disable specific button from set of programatically created buttons?
Interesting!!
I have tried applying both your theories but strangely my event handler is no longer being fired (i didnt change the syntax at all)! Form posts state this problem can occur with Visual Studio and that it can sometimes be solved by deleting the ASP.NET page and then recreating it. I tried that but no luck there... Any ideas?
-
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
stephsh
Interesting!!
I have tried applying both your theories but strangely my event handler is no longer being fired (i didnt change the syntax at all)! Form posts state this problem can occur with Visual Studio and that it can sometimes be solved by deleting the ASP.NET page and then recreating it. I tried that but no luck there... Any ideas?
Sorry this doesn't answer to get informed about how you did the whole thing. What controls are you using on your page to be able t add some LinkButtons. ???
-
Re: Disable specific button from set of programatically created buttons?
My ASP.NET page contains the ASP.NET PlaceHolder control "holderPages". In my Code Behind page I programmed the PlaceHolder to be filled at run-time with a series of linklabels (post #17).
This is what my ASP.NET page looks like:
Code:
<%@ Page Title="MyPage" Language="C#" MasterPageFile="~/Input_master.Master" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyProject.MyPage" %>
<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server">
<div class="title">
MyPage
</div>
<asp:PlaceHolder ID="holderPages" runat="server"></asp:PlaceHolder>
<p>
<asp:Table ID="tblReportSelection" runat="server" CssClass="tblReportSelection">
</asp:Table>
</p>
</asp:Content>
On creating a linklabel button, the eventhandler "mLbtnPage_Click" is successfully added. On clicking on a linklabel the event is however not called. I debugged the whole code step for step multiple times and not once did it reach the mouse event function "mLbtnPage_Click"...
-
Re: Disable specific button from set of programatically created buttons?
Please zip the project and post it as I still cannot create a Page like you have. There are too much things undefined like why do you have selected Page and all that stuff
-
Re: Disable specific button from set of programatically created buttons?
On a similar page (named "MyPage") with an identical problem I inserted JonnyPoet's recommended method of searching through all controls for a LinkButton. Only one object is however displayed in the list of controls and that is MyPage! I think this problem stems from MyPage deriving from both a masterpage and a base page. How do I access the controls IN MyPage??
I tried using the method below but received a syntax error for "MyProject.MyPage.Controls":
Code:
foreach (Control c in MyProject.MyPage.Controls)
{
if (c is Button)
{
Button b = (Button)c;
b.BackColor = System.Drawing.Color.Red;
}
}
-
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
stephsh
On a similar page (named "MyPage") with an identical problem I inserted JonnyPoet's recommended method of searching through all controls for a LinkButton. Only one object is however displayed in the list of controls and that is MyPage! I think this problem stems from MyPage deriving from both a masterpage and a base page. How do I access the controls IN MyPage??
I tried using the method below but received a syntax error for "MyProject.MyPage.Controls":
As already told please zip the project and post it here so we can have a look on it what happens
-
Re: Disable specific button from set of programatically created buttons?
Firstly my project wont work without the related database and webservice and secondly I dont have permission to make my whole project public... :S
would it help if i would post a specific part of the project and if so which part?
-
Re: Disable specific button from set of programatically created buttons?
Quote:
Originally Posted by
stephsh
Firstly my project wont work without the related database and webservice and secondly I dont have permission to make my whole project public... :S
would it help if i would post a specific part of the project and if so which part?
Simple make a runable demoproject which shows exactly that part of code, where you actually have your problem.
That way we can debug ourself and see whats going on.