Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Calling method in Usercontrol

Hi,

I am working on C#, Asp.net 2.0
I have a user control with id WucMainPage1 and inside this control there is another control used with id WucPages1

I want to call a method fillPageType(); which is inside usercontrol WucPages1

How should I do it.

Please guide.

I am trying with various method:

      UserControl WucPages1 = (UserControl)WucMainPage1.FindControl("WucPages1");
//      Type myType = typeof(WucPages1);
//      MethodInfo myMethodInfo = myType.GetMethod("fillPageType");
//      myType.InvokeMember(function,BindingFlags.InvokeMethod, null, null, null);

      //WucPages1.LoadControl

      // WucPages1.fillPageType();
      //  WucPages1.BindPageGrid();
      
      
Thanks,
Avatar of Thejaka
Thejaka
Flag of Sri Lanka image

Assuming your UserControl is named "WucPages"...

WucPages WucPages1 = (WucPages)WucMainPage1.FindControl("WucPages1");
WucPages1.fillPageType();

Open in new window

Avatar of tia_kamakshi

ASKER

Error Says:

Error      1      
The type or namespace name 'WucPages' could not be found (are you missing a using directive or an assembly reference?)


I have control where wucMainPage.ascx is registered. Inside wucMainPage.ascx control wucPages.ascx is registered.

Now in Page wucPages.ascx, I have method fillPageType(), which I wish to access from wucCourseContent.ascx.cs

Thanks


-- wucCourseContent.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="wucCourseContent.ascx.cs" Inherits="VW_Controls_wucCourseContent" %>
<%@ Register Src="wucMainPage.ascx" TagName="wucMainPage" TagPrefix="uc1" %>
<%@ Register Src="wucMainModule.ascx" TagName="wucMainModule" TagPrefix="uc2" %>
<%@ Register Src="wucMainCourse.ascx" TagName="wucMainCourse" TagPrefix="uc3" %>

<div class="LnkBtndiv"><asp:LinkButton ID="btnPage" runat="server" Text="Page" OnClick ="btnPage_Click" /></div>
<div class="LnkBtndiv"><asp:LinkButton ID="btnModule" runat="server" Text="Module" OnClick ="btnModule_Click"/></div>
<div class="LnkBtndiv"><asp:LinkButton ID="btnCourse" runat="server" Text="Course" OnClick ="btnCourse_Click"/></div>

<br clear="all" />

<asp:HiddenField runat="server" ID="hdnTabClick" Value="0" />

      <asp:MultiView runat="server" ID="mvCourseContent" ActiveViewIndex="0" OnActiveViewChanged="mvCourseContent_ActiveViewChanged" >
                <asp:View ID="vwPages" runat="server">
                    <uc1:wucMainPage ID="WucMainPage1" runat="server" />
                </asp:View>
                <asp:View ID="vwModule" runat="server">
                    <uc2:wucMainModule ID="WucMainModule1" runat="server" />
                </asp:View>                
                <asp:View ID="vwCourse" runat="server">
                    <uc3:wucMainCourse ID="WucMainCourse1" runat="server" />
                </asp:View>
               
    </asp:MultiView>
   
   
   
    -- wucMainPage.ascx


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="wucMainPage.ascx.cs" Inherits="VW_Controls_wucMainPage" %>
<%@ Register Src="wucUploadImages.ascx" TagName="wucUploadImages" TagPrefix="uc1" %>
<%@ Register Src="wucPages.ascx" TagName="wucPages" TagPrefix="uc3" %>
<%@ Register Src="wucQuestions.ascx" TagName="wucQuestions" TagPrefix="uc5" %>
<%@ Register Src="wucGlossary.ascx"  TagName="wucGlossary" TagPrefix="uc6" %>
<div class="LnkBtndiv1"><asp:LinkButton ID="btnPage" runat="server" Text="Page" OnClick="btnPage_Click" /></div>
<div class="LnkBtndiv1"><asp:LinkButton ID="btnQuestion" runat="server" Text="Question" OnClick="btnQuestion_Click" /></div>
<div class="LnkBtndiv1"><asp:LinkButton ID="btnGlossary" runat="server" Text="Glossary" OnClick="btnGlossary_Click" /></div>
<div class="LnkBtndiv1"><asp:LinkButton ID="btnImages" runat="server" Text="Upload Images" OnClick="btnImages_Click" /></div>
<asp:HiddenField runat="server" ID="hdnTabClick" Value="1" />

<br clear="all" />
<asp:MultiView runat="server" ID="mvCourseManagement" ActiveViewIndex="0" OnActiveViewChanged="mvCourseManagement_ActiveViewChanged" >
      <asp:View ID="vwPages" runat="server">
            <uc3:wucPages ID="WucPages1" runat="server" />
      </asp:View>
      <asp:View ID="vwQuestion" runat="server">
            <uc5:wucQuestions ID="WucQuestions1" runat="server" />
      </asp:View>
      <asp:View ID="vwGlossary" runat="server">
            <uc6:wucGlossary ID="WucGlossary1" runat="server" />
      </asp:View>
    <asp:View ID="vwImages" runat="server">
        <uc1:wucUploadImages ID="WucUploadImages1" runat="server" />
            
      </asp:View>
</asp:MultiView>



Now Inside control file wucPages.ascx, I have a method


public void fillPageType()
{
      oTypeMstrBL = new TypeMstrBL();
      int iApplyLevel = 3;

      DataTable odt = oTypeMstrBL.GetCategoryListing(iApplyLevel);

      ddlPageType.DataSource = odt;
      ddlPageType.DataBind();

      ddlPageType.Items.Insert(0, new ListItem("-- Select Type --", ""));
}
In this case you'll have to declare a public property or method to expose the method in the inner control to the outer one.

You should also add the following reference:

-- wucCourseContent.ascx

<%@ Reference Control="wucPages.ascx"%>


/*** wucMainPage.ascx.cs ***/
 
public property ASP_WucPages WucPages1_1
{
  get { return WucPages1; }
}
 
 
/*** wucCourseContent.ascx.cs ***/
 
WucMainPage1.WucPages1_1.fillPageType();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Thejaka
Thejaka
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool and Many Thanks :)