How to choose what control will receive action

Discussion in 'ASP .Net Web Controls' started by Robson Carvalho Machado, Aug 11, 2004.

  1. Dear Friends,

    I have the following code that creates dynamic controls at my form.

    My dificult is how to correct this code to put distinct AddHandler to each
    button and how to choose what label will receive text when Button1 is
    clickedor Button2 is clicked.

    I want to make possible to change text on Label1 when Button1 is clicked and
    change text on Label2 when Button2 is clicked .. and so on...

    Regards
    Robson Machado

    ASPX CODE:
    ---------------
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="dynamic.aspx.vb"
    Inherits="corretoresdeseguros.dynamic"%>
    <HTML>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:placeHolder id="PlaceHolder1" runat="server"></asp:placeHolder>
    </form>
    </body>
    </HTML>


    CODEBEHIND:
    ----------------

    Public Class dynamic
    Inherits System.Web.UI.Page
    Dim myLabel As Label
    Dim myRadio As RadioButtonList
    Dim myRFV As RequiredFieldValidator
    Dim myText As TextBox
    Dim myButton As Button
    Dim myUnits As System.Web.UI.WebControls.Unit

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    AddControls(1, 1, "Label1", "Label")
    AddControls(1, 2, "Label2", "Label")
    AddControls(1, 1, "Button1", "Button")
    AddControls(1, 2, "Button2", "Button")
    End Sub
    Sub AddControls(ByVal Index As Integer, ByVal QNum As Long, ByVal
    LabelText As String, ByVal Type As String)
    If Type = "Label" Then
    ' Add Label
    myLabel = New Label
    myLabel.ID = "LabelQ" & QNum
    myLabel.Text = LabelText & "<br><br>"
    myLabel.BorderStyle = 2
    PlaceHolder1.Controls.Add(myLabel)
    End If

    If Type = "Radio" Then
    ' Add Radio
    myRadio = New RadioButtonList
    myRadio.ID = "RadioQ" & QNum
    myRadio.Items.Add("0")
    myRadio.Items.Add("1")
    myRadio.Items.Add("2")
    myRadio.Items.Add("3")
    myRadio.Items.Add("4")
    myRadio.Items.Add("5")
    myUnits = New System.Web.UI.WebControls.Unit(402)
    myRadio.Width = myUnits
    myRadio.RepeatDirection = 0
    PlaceHolder1.Controls.Add(myRadio)
    End If

    If Type = "RFV" Then
    ' Add RFV
    myRFV = New RequiredFieldValidator
    myRFV.ID = "RFVQ" & QNum
    myRFV.ErrorMessage = "Unanswered"
    myRFV.ControlToValidate = "RadioQ" & QNum
    PlaceHolder1.Controls.Add(myRFV)
    End If

    If Type = "Button" Then
    myButton = New Button
    myButton.ID = "Button" & QNum
    myButton.Text = LabelText
    PlaceHolder1.Controls.Add(myButton)
    With myButton
    AddHandler .Click, AddressOf Button1_Click
    End With
    End If

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    myLabel.Text = "Button Clicked"
    End Sub
    End Class
     
    Robson Carvalho Machado, Aug 11, 2004
    #1
    1. Advertising

  2. Robson Carvalho Machado

    jagadeesh Guest

    Hi
    You can use the sender parameter of the button click event. Typecast the
    sender to button.Access the button properties to know the button (text or
    tag etc)
    Jagadeesh
    "Robson Carvalho Machado" <>
    wrote in message news:...
    > Dear Friends,
    >
    > I have the following code that creates dynamic controls at my form.
    >
    > My dificult is how to correct this code to put distinct AddHandler to each
    > button and how to choose what label will receive text when Button1 is
    > clickedor Button2 is clicked.
    >
    > I want to make possible to change text on Label1 when Button1 is clicked
    > and
    > change text on Label2 when Button2 is clicked .. and so on...
    >
    > Regards
    > Robson Machado
    >
    > ASPX CODE:
    > ---------------
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="dynamic.aspx.vb"
    > Inherits="corretoresdeseguros.dynamic"%>
    > <HTML>
    > <body MS_POSITIONING="GridLayout">
    > <form id="Form1" method="post" runat="server">
    > <asp:placeHolder id="PlaceHolder1" runat="server"></asp:placeHolder>
    > </form>
    > </body>
    > </HTML>
    >
    >
    > CODEBEHIND:
    > ----------------
    >
    > Public Class dynamic
    > Inherits System.Web.UI.Page
    > Dim myLabel As Label
    > Dim myRadio As RadioButtonList
    > Dim myRFV As RequiredFieldValidator
    > Dim myText As TextBox
    > Dim myButton As Button
    > Dim myUnits As System.Web.UI.WebControls.Unit
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > AddControls(1, 1, "Label1", "Label")
    > AddControls(1, 2, "Label2", "Label")
    > AddControls(1, 1, "Button1", "Button")
    > AddControls(1, 2, "Button2", "Button")
    > End Sub
    > Sub AddControls(ByVal Index As Integer, ByVal QNum As Long, ByVal
    > LabelText As String, ByVal Type As String)
    > If Type = "Label" Then
    > ' Add Label
    > myLabel = New Label
    > myLabel.ID = "LabelQ" & QNum
    > myLabel.Text = LabelText & "<br><br>"
    > myLabel.BorderStyle = 2
    > PlaceHolder1.Controls.Add(myLabel)
    > End If
    >
    > If Type = "Radio" Then
    > ' Add Radio
    > myRadio = New RadioButtonList
    > myRadio.ID = "RadioQ" & QNum
    > myRadio.Items.Add("0")
    > myRadio.Items.Add("1")
    > myRadio.Items.Add("2")
    > myRadio.Items.Add("3")
    > myRadio.Items.Add("4")
    > myRadio.Items.Add("5")
    > myUnits = New System.Web.UI.WebControls.Unit(402)
    > myRadio.Width = myUnits
    > myRadio.RepeatDirection = 0
    > PlaceHolder1.Controls.Add(myRadio)
    > End If
    >
    > If Type = "RFV" Then
    > ' Add RFV
    > myRFV = New RequiredFieldValidator
    > myRFV.ID = "RFVQ" & QNum
    > myRFV.ErrorMessage = "Unanswered"
    > myRFV.ControlToValidate = "RadioQ" & QNum
    > PlaceHolder1.Controls.Add(myRFV)
    > End If
    >
    > If Type = "Button" Then
    > myButton = New Button
    > myButton.ID = "Button" & QNum
    > myButton.Text = LabelText
    > PlaceHolder1.Controls.Add(myButton)
    > With myButton
    > AddHandler .Click, AddressOf Button1_Click
    > End With
    > End If
    >
    > End Sub
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs)
    > myLabel.Text = "Button Clicked"
    > End Sub
    > End Class
    >
     
    jagadeesh, Aug 11, 2004
    #2
    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?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=

    How to choose what control will receive action

    =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=, Aug 11, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    291
    =?Utf-8?B?Um9ic29uIENhcnZhbGhvIE1hY2hhZG8=?=
    Aug 11, 2004
  2. Joe Bloggs
    Replies:
    1
    Views:
    993
    Sudsy
    Aug 3, 2003
  3. rjweytens
    Replies:
    6
    Views:
    16,357
    rjweytens
    Jun 25, 2004
  4. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,955
    runescience
    Feb 7, 2006
  5. Robson Carvalho Machado

    How to choose what control will receive action on button click?

    Robson Carvalho Machado, Aug 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    135
    Robson Carvalho Machado
    Aug 7, 2004
Loading...

Share This Page