User Control Question: Button Onclick Event won't work (won't fire)

Discussion in 'ASP .Net' started by Chad, May 26, 2005.

  1. Chad

    Chad Guest

    I have a link (.ascx) and that generates an Add form on that page. The
    autopostback dropdown is within a "If Not IsPostBack Then" statement.
    The form that is created is all via static html in the user control.
    The problem is, no matter what even when no data is filled out in the
    form, the submit button just postsback the page and nothing happens. I
    have 2 dummy links on the page right now, and the form is automatically
    created without a link click for testing purposes. I thought that
    onclick events work in a user control but maybe I'm wrong.

    Oh, and stuff works in the .ascx, for instance response.write's appear
    from the Page_Load, it's just this onclick event that won't work.

    Here's the code:
    Sub Page_Load(Sender As Object, E As EventArgs)
    'Constants, link, local path, and sql connection.
    strLink =
    System.Configuration.ConfigurationSettings.AppSettings("LinkPath")
    strPath =
    System.Configuration.ConfigurationSettings.AppSettings("SitePath")
    Conn = New
    SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("SQLConnect"))
    strBaseQuery = "Select TableName from TableGenData where Display = 4
    Order By TableName Asc;"

    If Not IsPostBack Then
    conn.open()
    'Populates the dropdown with the Status Names.
    cmdSelect = New SqlCommand(strBaseQuery, Conn)
    dtrTableData = cmdSelect.ExecuteReader()
    TableDropDown.DataSource = dtrTableData
    TableDropDown.DataTextField = "TableName"
    TableDropDown.DataValueField = "TableName"
    TableDropDown.DataBind()
    dtrTableData.Close
    conn.close()

    Dim li As ListItem
    li = New ListItem("Select a Table", "0")
    TableDropDown.Items.Insert(0, li)
    End If

    'The control is added here.
    ctlControl = LoadControl( "AddDB.ascx" )
    plhContent.Controls.Add( ctlControl )
    '<ManageDB:AddDB ID="ctlAddDB" Runat="Server" />
    End Sub

    ***********************
    Here is the control:

    'On button submit, grab the data from each form and submit it to SQL.
    Sub SubmitForm_Click(s As Object, e as EventArgs)
    Response.write( "omg it worked" )
    End Sub

    Now, of course there is a form below that has all the .text fields but
    I'm hoping you can imagine those being there. The .ascx also contains
    the <asp:Button OnClick=SubmitForm_Click runat=server /> as well. If
    anything, I tried getting it to just error on me and nothing would
    happen. I'm not sure if you would need more info, but if so I have
    absolutely no problem providing it.

    I looked around groups, and around forums for about an hour but I
    couldn't find an answer. Maybe I'm just looking in the wrong places,
    with the wrong search terms.

    Thanks for the help in advance!
    Chad, May 26, 2005
    #1
    1. Advertising

  2. Chad

    Chad Guest

    Can anyone at least answer this question?

    Can onclick events work in user controls? If so, can someone give me a
    basic example?
    Chad, May 27, 2005
    #2
    1. Advertising

  3. Chad

    Guest

    Chad,

    Yes, the events will work, but everything has to be in place.
    I typically let the IDE sort of generate the button click routine, so
    it looks like this:
    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnOk.Click

    and then you shouldn't need the "OnClick" in the html. I've seen it
    both ways.

    Note that the pageload for the containing page will occur before the
    event
    for the button click. It helps to understand the sequence of events.

    One thing to watch for is if you have multiple controls you should
    assign them each an id , otherwise things can get confused when
    postbacks occur.
    , May 27, 2005
    #3
  4. Chad

    Chad Guest

    Thank you, I figured the events would work... I guess I'm just missing
    something.

    That does shed some light on the issue.

    I have been doing asp.NET development for a while, but I just started
    using user controls, so this concept is all a bit foreign to me and
    most resources I look at don't tend to cover events in the user
    control, but let the even get handled in the page containing the user
    control.

    Thanks.
    Chad, May 27, 2005
    #4
  5. Chad

    Chad Guest

    I found the problem, and it was just me being stupid.

    I had:
    Sub SubmitForm_Click(s As Object, e as EventArgs)
    If blnIsValid = True Then
    Response.write( "omg it worked" )
    End If
    End Sub

    I erased the entry on accident that made the boolean value = true. So,
    thanks, and I apologize for making such a ridiculous post. I also can't
    believe how long this took me to figure out.
    Chad, May 27, 2005
    #5
    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. Ryan
    Replies:
    4
    Views:
    8,994
    Chris Botha
    Nov 8, 2005
  2. -Karl

    OnClick event won't fire

    -Karl, Jul 5, 2007, in forum: ASP .Net
    Replies:
    13
    Views:
    884
    -Karl
    Jul 9, 2007
  3. Stefan

    Button in WebCustomControl don't fire the onclick event, why?

    Stefan, Jul 8, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    225
    Stefan
    Jul 9, 2003
  4. mbs57

    ASP.NET button control OnClick event doesn't fire

    mbs57, Jun 18, 2009, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    1,420
  5. Replies:
    2
    Views:
    255
Loading...

Share This Page