Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > User Control Question: Button Onclick Event won't work (won't fire)

Thread Tools

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

Posts: n/a
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.AppSett ings("LinkPath")
strPath =
System.Configuration.ConfigurationSettings.AppSett ings("SitePath")
Conn = New
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings("SQLConnect"))
strBaseQuery = "Select TableName from TableGenData where Display = 4
Order By TableName Asc;"

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

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!

Reply With Quote
Posts: n/a
Can anyone at least answer this question?

Can onclick events work in user controls? If so, can someone give me a
basic example?

Reply With Quote
Posts: n/a

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
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.

Reply With Quote
Posts: n/a
Thank you, I figured the events would work... I guess I'm just missing

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


Reply With Quote
Posts: n/a
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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET button control OnClick event doesn't fire mbs57 ASP .Net Web Controls 2 06-18-2009 09:18 PM
Ajax popup control extender-onclick button event Paul ASP .Net 0 06-12-2008 10:56 PM
onclick button event dont work in IE, only works in FIREFOX meskaline ASP .Net 0 11-02-2006 10:12 PM
button.onclick = new Function("func2()") + button.onclick Javascript 2 09-26-2005 08:13 AM
Radio Button List ---> onclick event. You can never know when the user clicked? Leeor Geva ASP .Net 1 08-30-2003 01:09 AM