General Event Handler For Runtime Buttons

Discussion in 'ASP .Net Web Controls' started by roblaro, Nov 4, 2005.

  1. roblaro

    roblaro Guest

    Hello,

    I have an asp.net 2.0 app that will create a group of linkbuttons at
    runtime. The ID of these links buttons will autogenerated.

    I want to write a method that will be fired when any one of these buttons is
    clicked. For some reason I can not figure out how to do this.

    Here is some code:

    Private Sub Create_Buttons()
    ...some code is run here to execute a stored proc and fill a datareader...
    While rd.Read()
    btn = New LinkButton

    btn.Text = rd("CONSIGNEE_INFO").ToString() & "<br><br>"
    btn.ID = rd("ADDR_ID").ToString()
    btn.ForeColor = System.Drawing.Color.FromArgb(100, 51,
    51, 51)
    btn.Attributes.Add("style", "text-decoration: none;")
    btn.Attributes.Add("onMouseOver",
    "this.style.backgroundColor='#93c0ee';")
    btn.Attributes.Add("onMouseOut",
    "this.style.backgroundColor='#ecf0f4';")
    btn.CommandName = "MyCommand"
    btn.CommandArgument = rd("ADDR_ID").ToString()

    ...popAddr is a <div></div> layer that is set to runat="server" and
    will
    hold all of the runtime buttons...

    popAddr.Controls.Add(btn)
    End While
    rd.Close()
    End Sub

    I thought that I would simply be able to use btn.OnCommand to pass the
    method I want to execute, however, it says that its protected.

    How can I get this to work?

    Thanks!

    --
    Bob Gibilaro
    Applications Developer
    roblaro, Nov 4, 2005
    #1
    1. Advertising

  2. Hi Bob,

    Adding an event handler syntax in C# is:
    btn.Command +=new CommandEventHandler(btn_Command);

    Then you would write the event handling method as:
    private void btn_Command(object sender, CommandEventArgs e)
    {
    switch(e.CommandName )
    {
    case "Command1":
    // process command 1
    break;
    case "Command2":
    //process command 2
    break;
    }
    }

    But the important issue here is that your controls must be created during
    the page.init event handling otherwise their ViewState will not be preserved.
    In other words, when the user clicks on any of those Hyperlinks nothing
    will happen, unless you create those controls upon page.init.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "roblaro" wrote:

    > Hello,
    >
    > I have an asp.net 2.0 app that will create a group of linkbuttons at
    > runtime. The ID of these links buttons will autogenerated.
    >
    > I want to write a method that will be fired when any one of these buttons is
    > clicked. For some reason I can not figure out how to do this.
    >
    > Here is some code:
    >
    > Private Sub Create_Buttons()
    > ...some code is run here to execute a stored proc and fill a datareader...
    > While rd.Read()
    > btn = New LinkButton
    >
    > btn.Text = rd("CONSIGNEE_INFO").ToString() & "<br><br>"
    > btn.ID = rd("ADDR_ID").ToString()
    > btn.ForeColor = System.Drawing.Color.FromArgb(100, 51,
    > 51, 51)
    > btn.Attributes.Add("style", "text-decoration: none;")
    > btn.Attributes.Add("onMouseOver",
    > "this.style.backgroundColor='#93c0ee';")
    > btn.Attributes.Add("onMouseOut",
    > "this.style.backgroundColor='#ecf0f4';")
    > btn.CommandName = "MyCommand"
    > btn.CommandArgument = rd("ADDR_ID").ToString()
    >
    > ...popAddr is a <div></div> layer that is set to runat="server" and
    > will
    > hold all of the runtime buttons...
    >
    > popAddr.Controls.Add(btn)
    > End While
    > rd.Close()
    > End Sub
    >
    > I thought that I would simply be able to use btn.OnCommand to pass the
    > method I want to execute, however, it says that its protected.
    >
    > How can I get this to work?
    >
    > Thanks!
    >
    > --
    > Bob Gibilaro
    > Applications Developer
    Phillip Williams, Nov 4, 2005
    #2
    1. Advertising

  3. roblaro

    roblaro Guest

    Phillip,

    Thanks. Actually, not long after I posted this, I found the answer. You are
    right about creating the buttons in the Init, which I did.

    In VB.NET, to add the additional handler, I simply added the following code
    to the method that creates the buttons:

    AddHandler btn.Command, AddressOf Command_Button_Click

    Command_Button_Click is the method I wanted to execute when any one of the
    buttons was created.

    This, combined with creating the buttons in the Page Init stage did the trick.

    Thanks for the help.

    --
    Bob Gibilaro
    Applications Developer


    "Phillip Williams" wrote:

    > Hi Bob,
    >
    > Adding an event handler syntax in C# is:
    > btn.Command +=new CommandEventHandler(btn_Command);
    >
    > Then you would write the event handling method as:
    > private void btn_Command(object sender, CommandEventArgs e)
    > {
    > switch(e.CommandName )
    > {
    > case "Command1":
    > // process command 1
    > break;
    > case "Command2":
    > //process command 2
    > break;
    > }
    > }
    >
    > But the important issue here is that your controls must be created during
    > the page.init event handling otherwise their ViewState will not be preserved.
    > In other words, when the user clicks on any of those Hyperlinks nothing
    > will happen, unless you create those controls upon page.init.
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "roblaro" wrote:
    >
    > > Hello,
    > >
    > > I have an asp.net 2.0 app that will create a group of linkbuttons at
    > > runtime. The ID of these links buttons will autogenerated.
    > >
    > > I want to write a method that will be fired when any one of these buttons is
    > > clicked. For some reason I can not figure out how to do this.
    > >
    > > Here is some code:
    > >
    > > Private Sub Create_Buttons()
    > > ...some code is run here to execute a stored proc and fill a datareader...
    > > While rd.Read()
    > > btn = New LinkButton
    > >
    > > btn.Text = rd("CONSIGNEE_INFO").ToString() & "<br><br>"
    > > btn.ID = rd("ADDR_ID").ToString()
    > > btn.ForeColor = System.Drawing.Color.FromArgb(100, 51,
    > > 51, 51)
    > > btn.Attributes.Add("style", "text-decoration: none;")
    > > btn.Attributes.Add("onMouseOver",
    > > "this.style.backgroundColor='#93c0ee';")
    > > btn.Attributes.Add("onMouseOut",
    > > "this.style.backgroundColor='#ecf0f4';")
    > > btn.CommandName = "MyCommand"
    > > btn.CommandArgument = rd("ADDR_ID").ToString()
    > >
    > > ...popAddr is a <div></div> layer that is set to runat="server" and
    > > will
    > > hold all of the runtime buttons...
    > >
    > > popAddr.Controls.Add(btn)
    > > End While
    > > rd.Close()
    > > End Sub
    > >
    > > I thought that I would simply be able to use btn.OnCommand to pass the
    > > method I want to execute, however, it says that its protected.
    > >
    > > How can I get this to work?
    > >
    > > Thanks!
    > >
    > > --
    > > Bob Gibilaro
    > > Applications Developer
    roblaro, Nov 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David A. Beck

    Event handler for multiple command buttons

    David A. Beck, Dec 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    328
    Chris Jackson
    Dec 4, 2003
  2. Roger
    Replies:
    1
    Views:
    613
    GreggTB
    May 20, 2005
  3. akki
    Replies:
    3
    Views:
    598
    Ken Cox - Microsoft MVP
    Feb 6, 2006
  4. Replies:
    1
    Views:
    682
    Damien
    Feb 22, 2007
  5. Bill
    Replies:
    1
    Views:
    147
    Thomas 'PointedEars' Lahn
    Oct 23, 2003
Loading...

Share This Page