LinkButton click event in custom web control
-
I am working on a webcontrol that contains linkbuttons and I have to act on the click event of them. Can anyone help me to do this. I can not get it to work. Matglas
Matglas wrote:
I can not get it to work.
What exactly does that mean? What do you want to do when a link button is clicked? It would help if you show some code.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
I am working on a webcontrol that contains linkbuttons and I have to act on the click event of them. Can anyone help me to do this. I can not get it to work. Matglas
This is my code. When creating the child controls I add a linkbutton that has is linked to method viewLogLButton_Click(). But this method does not not execute. I deleted some original code because it was a lot longer.
[ToolboxData("<{0}:ExceptionViewer runat=server />")] [DesignTimeVisible(true)] public class ExceptionViewerControl : WebControl { protected override void RenderContents(HtmlTextWriter output) { this.EnsureChildControls(); this.RenderChildren(output); } protected override void OnInit(EventArgs e) { } protected override void CreateChildControls() { this.Controls.Clear(); HtmlTable RenderTbl = LoadLogDirectory(); this.Controls.Add(RenderTbl); } private const string mainTblId = "exViewerTbl"; private String fileLoad = null; private HtmlTable _mainFileTable = new HtmlTable(); private HtmlTable MainFileTable { get { return _mainFileTable; } set { _mainFileTable = value; } } private HtmlTable LoadLogDirectory(FileInfo file) { this.MainFileTable = new HtmlTable(); LinkButton viewLogLButton = new LinkButton(); viewLogLButton.CommandName = "ViewLogFile"; viewLogLButton.CommandArgument = file.Name; viewLogLButton.Text = file.Name; viewLogLButton.Click += new EventHandler(viewLogLButton_Click); ControlCollection controls = new ControlCollection(this); controls.Add(viewLogLButton); if (fileLoad == file.Name) { //action } this.MainFileTable.Rows.Add(GetHeaderRow(controls)); return MainFileTable; } void viewLogLButton_Click(object sender, EventArgs e) { fileLoad = ((LinkButton)sender).CommandArgument; } private HtmlTableRow GetHeaderRow(ControlCollection controls) { //header cell HtmlTableCell headerCell = new HtmlTableCell(); headerCell.Attributes["class"] = styleClass; headerCell.Attributes["colspan"] = span.ToString(); foreach (Control ctrl in controls) { headerCell.Controls.Add(ctrl); } headerRow.Cells.Add(headerCell); return headerRow; }
-
This is my code. When creating the child controls I add a linkbutton that has is linked to method viewLogLButton_Click(). But this method does not not execute. I deleted some original code because it was a lot longer.
[ToolboxData("<{0}:ExceptionViewer runat=server />")] [DesignTimeVisible(true)] public class ExceptionViewerControl : WebControl { protected override void RenderContents(HtmlTextWriter output) { this.EnsureChildControls(); this.RenderChildren(output); } protected override void OnInit(EventArgs e) { } protected override void CreateChildControls() { this.Controls.Clear(); HtmlTable RenderTbl = LoadLogDirectory(); this.Controls.Add(RenderTbl); } private const string mainTblId = "exViewerTbl"; private String fileLoad = null; private HtmlTable _mainFileTable = new HtmlTable(); private HtmlTable MainFileTable { get { return _mainFileTable; } set { _mainFileTable = value; } } private HtmlTable LoadLogDirectory(FileInfo file) { this.MainFileTable = new HtmlTable(); LinkButton viewLogLButton = new LinkButton(); viewLogLButton.CommandName = "ViewLogFile"; viewLogLButton.CommandArgument = file.Name; viewLogLButton.Text = file.Name; viewLogLButton.Click += new EventHandler(viewLogLButton_Click); ControlCollection controls = new ControlCollection(this); controls.Add(viewLogLButton); if (fileLoad == file.Name) { //action } this.MainFileTable.Rows.Add(GetHeaderRow(controls)); return MainFileTable; } void viewLogLButton_Click(object sender, EventArgs e) { fileLoad = ((LinkButton)sender).CommandArgument; } private HtmlTableRow GetHeaderRow(ControlCollection controls) { //header cell HtmlTableCell headerCell = new HtmlTableCell(); headerCell.Attributes["class"] = styleClass; headerCell.Attributes["colspan"] = span.ToString(); foreach (Control ctrl in controls) { headerCell.Controls.Add(ctrl); } headerRow.Cells.Add(headerCell); return headerRow; }
I'm quite sure it has something to do with dynamically adding the link button, but I do not know how to circumvent this. Try asking this in the ASP.NET forum.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
This is my code. When creating the child controls I add a linkbutton that has is linked to method viewLogLButton_Click(). But this method does not not execute. I deleted some original code because it was a lot longer.
[ToolboxData("<{0}:ExceptionViewer runat=server />")] [DesignTimeVisible(true)] public class ExceptionViewerControl : WebControl { protected override void RenderContents(HtmlTextWriter output) { this.EnsureChildControls(); this.RenderChildren(output); } protected override void OnInit(EventArgs e) { } protected override void CreateChildControls() { this.Controls.Clear(); HtmlTable RenderTbl = LoadLogDirectory(); this.Controls.Add(RenderTbl); } private const string mainTblId = "exViewerTbl"; private String fileLoad = null; private HtmlTable _mainFileTable = new HtmlTable(); private HtmlTable MainFileTable { get { return _mainFileTable; } set { _mainFileTable = value; } } private HtmlTable LoadLogDirectory(FileInfo file) { this.MainFileTable = new HtmlTable(); LinkButton viewLogLButton = new LinkButton(); viewLogLButton.CommandName = "ViewLogFile"; viewLogLButton.CommandArgument = file.Name; viewLogLButton.Text = file.Name; viewLogLButton.Click += new EventHandler(viewLogLButton_Click); ControlCollection controls = new ControlCollection(this); controls.Add(viewLogLButton); if (fileLoad == file.Name) { //action } this.MainFileTable.Rows.Add(GetHeaderRow(controls)); return MainFileTable; } void viewLogLButton_Click(object sender, EventArgs e) { fileLoad = ((LinkButton)sender).CommandArgument; } private HtmlTableRow GetHeaderRow(ControlCollection controls) { //header cell HtmlTableCell headerCell = new HtmlTableCell(); headerCell.Attributes["class"] = styleClass; headerCell.Attributes["colspan"] = span.ToString(); foreach (Control ctrl in controls) { headerCell.Controls.Add(ctrl); } headerRow.Cells.Add(headerCell); return headerRow; }
remove this code viewLogLButton.Click += new EventHandler(viewLogLButton_Click) from your LoadLogDirectory method then tranfer to OnInit Web Control EventHandler. Here is a sample code... protected override void OnInit(EventArgs e) { base.OnInit (e); viewLogLButton.Click += new EventHandler(viewLogLButton_Click); } regards, Mark
-
remove this code viewLogLButton.Click += new EventHandler(viewLogLButton_Click) from your LoadLogDirectory method then tranfer to OnInit Web Control EventHandler. Here is a sample code... protected override void OnInit(EventArgs e) { base.OnInit (e); viewLogLButton.Click += new EventHandler(viewLogLButton_Click); } regards, Mark