Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. LinkButton click event in custom web control

LinkButton click event in custom web control

Scheduled Pinned Locked Moved C#
help
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matglas
    wrote on last edited by
    #1

    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

    S M 2 Replies Last reply
    0
    • M Matglas

      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

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      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

      www.troschuetz.de

      1 Reply Last reply
      0
      • M Matglas

        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

        M Offline
        M Offline
        Matglas
        wrote on last edited by
        #3

        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;
                }
        
        S T 2 Replies Last reply
        0
        • M 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;
                  }
          
          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          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

          www.troschuetz.de

          1 Reply Last reply
          0
          • M 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;
                    }
            
            T Offline
            T Offline
            Tuwing Sabado
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • T Tuwing Sabado

              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

              M Offline
              M Offline
              Matglas
              wrote on last edited by
              #6

              Thanks I'll try it tomorrow when I am back at work. Matglas

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups