[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1150
  • Last Modified:

How to make Button Click event fire in usercontrol

I have an asp.net 2 web app. with an ASPX page linked to a master page.
There is NO code-behind page for the ASPX page.
I have placed a usercontrol in the ASPX page.  The usercontrol contains two gridviews and a button with code behind to bind the gridviews and a button click event.

The gridviews and databinding display fine when debugged. but I cannot get the usercontrol button event to fire.
Can anybody please provide a simple step-by-step guide to help me get the button click event to fire?
Thank you.
ASPX page:
<%@ Page Language="VB" MasterPageFile="~/Pages/MasterPage.master" Title="DW module - Data Analysis" %>

<%@ Register TagPrefix="DWM" Namespace="DWM" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<DWM:DWselector SkinFileName="DWmodule/DWselector.ascx" ID="DWselector1" runat="server" Secured="true" />
</asp:Content>

Open in new window

0
Al230762
Asked:
Al230762
  • 3
  • 2
1 Solution
 
guru_samiCommented:
just make sure that the button click event is assigned the proper handler.
e.g.
protected ....Button1_Click(....) Handles Button1.Click

or your button markup has ... OnClick="Button1_Click"

Note: Button1_Click could be something else but you get the idea...
0
 
Al230762Author Commented:
guru sami: in the code behind for the usercontrol I already have:
Protected WithEvents btnSelectAll As Button
Private Sub btnSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click

I should explain that the ASPX page has TWO usercontrols placed inside it and buttons on neither of the usercontrols work.

Can you explain to me why button events are fired when there is just ONE usercontrol on an ASPX page and why they do not fire when there are multiple usercontrols on the page?
0
 
guru_samiCommented:
-->Can you explain to me why button events are fired when there is just ONE usercontrol on an ASPX page and why they do not fire when there are multiple usercontrols on the page?

I don't know why it's behaving like that but may be you can try it in a new page...
0
 
Al230762Author Commented:
guru_same: that problem is now solved. I added the attributes for the button in the Page Load event and it now fires an event:

   If Not Me.IsPostBack Then
                             btnSaveSelection.Attributes.Add("onclick", "return clickButton(event,'" + btnSaveSelection.ClientID + "')")

Can you help with a supplementary problem. I now need to fire an event when a checkbox is clicked in one of the gridviews inside the usercontrol. I need to know how to raise that checkbox click event in the Page Load event as I did with the button (above) because the event is not firing if I code it in the ususal way (see attached code). Can you help with the code for that please?
ASCX page:
 <asp:GridView ID="grdGroups" runat="server" CssClass="text" AutoGenerateColumns="False" ForeColor="#333333" DataKeyNames="GroupID" AllowSorting="True" CellPadding="4" PageSize="5" Height="107px" Width="166px">
                            <Columns>
                             <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label runat="server" ID="HeaderLevelCheckBox" Text="" />
                    </HeaderTemplate>
                    <ItemTemplate>
<asp:CheckBox runat="server" ID="selectgroup" AutoPostBack="true" />
                    </ItemTemplate>

CODE BEHIND:
   Private Sub grdGroups_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdGroups.RowDataBound

            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                Dim chk As CheckBox = DirectCast(e.Row.FindControl("selectgroup"), CheckBox)
                chk.Attributes.Add("OnCheckedChanged", "Checkbox1_CheckedChanged")
            End If

        End Sub


 Protected Sub Checkbox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
            
'THINGS TO HAPPEN HERE BUT EVENT NEVER FIRES..
        End Sub

Open in new window

0
 
guru_samiCommented:
not sure how to do exactly with VB.NET these changes should attach handler to OnCheckChanged...
1: Add AutoEventWireup="true" to your .asxc :

<%@ Control Language="VB" AutoEventWireup="true" ......%>

2: To your checkbox:
<asp:CheckBox runat="server" ID="selectgroup" AutoPostBack="true" OnCheckedChanged="Checkbox1_CheckedChanged" />

3: No need of this:
 chk.Attributes.Add("OnCheckedChanged", "Checkbox1_CheckedChanged")



0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now