Tuesday, December 6, 2016

How To Fire Child Web Control Event in Parent Web Control in ASP.NET

Consider two web parts "BooKTicket.ascx" (Parent web part) and "Passenger.ascx" (Child web part). Passenger.ascx has a RadioButtonList and on selected index changed event of that control, I need to perform some activity in the parent web part.

<%-- Passenger.ascx --%>
<div class="passenger-number-holder row form-group" style="padding-top:5px">
        <div class="col-md-3">
            <asp:Label ID="lblBoardingPass" runat="server" Text="Boarding Assistance Required" AssociatedControlID="ddlTravelPass" />
        </div>
        <div class="col-md-6">
            <asp:RadioButtonList ID="rdoListBoardingAssistance" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="rdoListBoardingAssistance_SelectedIndexChanged" OnPreRender="rdoListBoardingAssistance_PreRender">
                <asp:ListItem Value="1">Yes</asp:ListItem>
                <asp:ListItem  Selected="True" Value="0">No</asp:ListItem>

            </asp:RadioButtonList>
        </div>

    </div>

//Passenger.ascx.cs

//Create a Event Handler
public event EventHandler rdoUpdated;

//Call the evnt handler on SelectedIndexChanged event of RadioButtonList
 protected void rdoListBoardingAssistance_SelectedIndexChanged(object sender, EventArgs e)
 {
    rdoUpdated(this, EventArgs.Empty);
 }
<%-- BookTicket.ascx --%>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BookTickets.ascx.cs" Inherits="Day3.VLine.Website.WebParts.CMSWebParts_VLine_JourneyPlanner_BookTickets" %>
<%@ Register Src="~/CMSWebParts/VLine/JourneyPlanner/Passengers.ascx" TagPrefix="line" TagName="Passengers" %>

<asp:UpdatePanel ID="udpUpdate" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
  <ContentTemplate>
    <%-- Hook up the event OnrdoUpdated --%>
    <line:Passengers runat="server" ID="Passengers" OnrdoUpdated="Passengers_rdoUpdated"  />
    <asp:Button ID="btnNext" runat="server" Text="Next"/>
  </ContentTemplate>
</asp:UpdatePanel>
//Add this method in BookTicket.ascx.cs
protected void Passengers_rdoUpdated(object sender, EventArgs e) { UpdateButtonTextOnChange("passenger"); } private void UpdateButtonTextOnChange(string sender) { if(sender == "passenger") { btnNext.Text = "Next - Updated Text"; udpUpdate.Update(); } }

No comments:
Write comments
Recommended Posts × +