Dynamic control and onClick event Server-side

Discussion in 'ASP .Net' started by RA, Nov 5, 2004.

  1. RA

    RA Guest

    I have created a button dynamically; which has been added to a TableCell of
    a TableRow of a Table control.

    Is there a way to add onclick event which calls a procedure on the
    Server-side itself.
    Any suggestions?

    I tried with
    btnAdd.Attributes.Add("onClick","AddItem();")


    Public Sub AddItem()
    '
    dim Test as String
    Test = "Testing OnClcik"
    End Sub

    I get "Object Expected" error.

    I also tried
    btnAdd.Attributes.Add("onClick","AddItem;")
    Error message is " 'AddItem' is undefined"
     
    RA, Nov 5, 2004
    #1
    1. Advertising

  2. RA

    Alex Guest

    As you create the button dynamically, the asp:button has an OnCommand
    eventhandler, set that to be some event name, like Button_OnClick. Create a
    function in your code behind, Button_OnClick, and this should help out.

    For example...
    Page...
    <asp:Button id="Button1"
    Text="Sort Ascending"
    CommandName="Sort"
    CommandArgument="Ascending"
    OnCommand="CommandBtn_Click"
    runat="server"/>

    Page behind...
    void CommandBtn_Click(Object sender, CommandEventArgs e) {
    ...
    }

    --
    Alex
     
    Alex, Nov 5, 2004
    #2
    1. Advertising

  3. RA

    RA Guest

    This seems code on client-side. I want to write a procedure(AddItem) in
    webform1.aspx.vb and want onclick event to call that procedure(AddItem).

    I am not much familiar with Javascript.
    For this procedure I have to go through table's each row find the ID cell
    and then add/update information in dataset and subsequently in SQL Server
    table.


    "Alex" <> wrote in message
    news:...
    > As you create the button dynamically, the asp:button has an OnCommand
    > eventhandler, set that to be some event name, like Button_OnClick. Create
    > a
    > function in your code behind, Button_OnClick, and this should help out.
    >
    > For example...
    > Page...
    > <asp:Button id="Button1"
    > Text="Sort Ascending"
    > CommandName="Sort"
    > CommandArgument="Ascending"
    > OnCommand="CommandBtn_Click"
    > runat="server"/>
    >
    > Page behind...
    > void CommandBtn_Click(Object sender, CommandEventArgs e) {
    > ...
    > }
    >
    > --
    > Alex
    >
    >
     
    RA, Nov 5, 2004
    #3
  4. RA

    Alex Guest

    Sorry, I am not sure what you mean. The code I gave you is what you need for
    your client side page's control to post back to the code behind. You'll
    notice the asp:Button is runat=server, so it will post back to your
    page.ascx.vb, where you can write your OnCommand eventhandler,
    CommandBtn_Click. When you specify the runat=server, your controls will
    trigger the code behind.

    This was not javascript at all. The code behind had C# signatures, but you
    can change them to VB. If you need more clarification, go here...
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
    bControlsButtonClassOnCommandTopic.htm

    --
    Alex Mueller
     
    Alex, Nov 5, 2004
    #4
  5. RA

    RA Guest

    This is what I am doing.

    Private Sub PopulateTable()
    Dim tRow as New TableRow
    Dim tCell as new TableCell

    Dim btnAdd As New Button
    With btnAdd
    .ID = "AddLink"
    .Enabled = False
    .Font.Bold = True
    .Font.Underline = True
    .ForeColor = System.Drawing.Color.Blue
    .BackColor = System.Drawing.Color.Transparent
    .BorderStyle = BorderStyle.None
    .Attributes("onClick") = "AddRCode();"
    End With

    tCell.Controls.Add(btnAdd)
    tRow.Cells.Add(tCell)
    TableTest.Rows.Add(tRow)
    ' Also have code to add more data.
    End Sub

    Public Sub AddItem()
    '
    dim Test as String
    Test = "Testing OnClcik"
    End Sub

    "Alex" <> wrote in message
    news:...
    > Sorry, I am not sure what you mean. The code I gave you is what you need
    > for
    > your client side page's control to post back to the code behind. You'll
    > notice the asp:Button is runat=server, so it will post back to your
    > page.ascx.vb, where you can write your OnCommand eventhandler,
    > CommandBtn_Click. When you specify the runat=server, your controls will
    > trigger the code behind.
    >
    > This was not javascript at all. The code behind had C# signatures, but you
    > can change them to VB. If you need more clarification, go here...
    > ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
    > bControlsButtonClassOnCommandTopic.htm
    >
    > --
    > Alex Mueller
    >
    >
     
    RA, Nov 5, 2004
    #5
  6. Hello RA,

    I just saw your post and I know that this may be a little bit late. The way
    you add an event handler to the dynamically created control is as follows:

    1. First create a procedure with the exact signature as an event handler
    2. After adding the dynamic control, bind this procedure to the dynamic
    control

    Note: You can have multiple dynamic controls bind to the same event handler.

    As an example:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    Dim btnAdd as New Button
    Dim bSomeCondition as Boolean = False

    If bSomeCondition = True Then
    'add the new button to the form
    With btnAdd
    .ID = "btnAdd"
    .Text = "Add"
    'any other attributes
    End With
    WebForm1.Controls.Add btnAdd

    'bind this control to an event handler
    AddHandler btnAdd.Click, AddressOf btnAddHandler
    End If


    'code for the handler
    Public Sub btnAddHandler(ByVal sender As System.Object, ByVal e As
    System.EventArgs)

    ' your code goes here

    End Sub

    Hope this helps.
    End Sub

    "RA" wrote:

    > This is what I am doing.
    >
    > Private Sub PopulateTable()
    > Dim tRow as New TableRow
    > Dim tCell as new TableCell
    >
    > Dim btnAdd As New Button
    > With btnAdd
    > .ID = "AddLink"
    > .Enabled = False
    > .Font.Bold = True
    > .Font.Underline = True
    > .ForeColor = System.Drawing.Color.Blue
    > .BackColor = System.Drawing.Color.Transparent
    > .BorderStyle = BorderStyle.None
    > .Attributes("onClick") = "AddRCode();"
    > End With
    >
    > tCell.Controls.Add(btnAdd)
    > tRow.Cells.Add(tCell)
    > TableTest.Rows.Add(tRow)
    > ' Also have code to add more data.
    > End Sub
    >
    > Public Sub AddItem()
    > '
    > dim Test as String
    > Test = "Testing OnClcik"
    > End Sub
    >
    > "Alex" <> wrote in message
    > news:...
    > > Sorry, I am not sure what you mean. The code I gave you is what you need
    > > for
    > > your client side page's control to post back to the code behind. You'll
    > > notice the asp:Button is runat=server, so it will post back to your
    > > page.ascx.vb, where you can write your OnCommand eventhandler,
    > > CommandBtn_Click. When you specify the runat=server, your controls will
    > > trigger the code behind.
    > >
    > > This was not javascript at all. The code behind had C# signatures, but you
    > > can change them to VB. If you need more clarification, go here...
    > > ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
    > > bControlsButtonClassOnCommandTopic.htm
    > >
    > > --
    > > Alex Mueller
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?YW1pcm1pcmE=?=, Apr 1, 2005
    #6
    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. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Replies:
    2
    Views:
    6,565
    rick74
    May 27, 2008
  2. Fred
    Replies:
    2
    Views:
    1,097
    Craig Deelsnyder
    Jul 12, 2004
  3. WilsonSmith

    Clint side and server side event

    WilsonSmith, Jul 16, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    545
    WilsonSmith
    Jul 17, 2004
  4. Mythran
    Replies:
    2
    Views:
    549
    Mythran
    Jan 22, 2005
  5. Guest

    Fire server side event before client side event

    Guest, Mar 17, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    632
    Ken Cox [Microsoft MVP]
    Mar 18, 2005
Loading...

Share This Page