![]() |
User Control Question: Button Onclick Event won't work (won't fire)
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 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! |
Re: User Control Question: Button Onclick Event won't work (won't fire)
Can anyone at least answer this question?
Can onclick events work in user controls? If so, can someone give me a basic example? |
Re: User Control Question: Button Onclick Event won't work (won't fire)
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. |
Re: User Control Question: Button Onclick Event won't work (won't fire)
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. |
Re: User Control Question: Button Onclick Event won't work (won't fire)
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. |
| All times are GMT. The time now is 08:38 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.