【问题标题】:Calling button function from user control从用户控件调用按钮功能
【发布时间】:2012-05-08 05:19:25
【问题描述】:

以前,我有一个默认的 aspx 页面,其中包含一个主用户控件。

主要的用户控件是用于动态加载菜单项的用户。例如联系人销售订单

当用户点击联系人时,联系人的内容是从其他子用户控件加载的。

默认 aspx 前端

Here is the aspx page which include the main user control
     <div class="rightColumnModule2">
                                <asp:Panel ID="pnUserControl" runat="server">
                                        <uc2:SubMenuItem ID="MainMenuItem1" runat="server"/>
                                </asp:Panel>
                            </div>

子菜单后端代码

   //dynamic load other content of user control
  //I am using placeholder to load from other user control
        private void loadUserControl()
        {
            if (this.hfSelectSubItemURL.Value != "")
            {
                UserControl ucSimpleControl = LoadControl(this.hfSelectSubItemURL.Value) as UserControl;
                PlaceHolder1.Controls.Add(ucSimpleControl);
            }
        }

ContactPersonList.ascx 前端

div class="rightColumnModule2">


                            <div class="rightColumnModule2TitleContainer">
                                <table class="rightColumnModule2Table">

                                    <tr>
                                        <td>
                                            <div class="rightColumnModule2Title">
                                                Contact Person</div>
                                        </td>
                                    </tr>

                                    <tr>
                                        <td>
                                           <cc1:cusAcesslevelBtn ID="btncAdd" runat="server" aclType="Add" 
                                                CssClass="inputButonS" onclick="btncAdd_Click" Text="Add" />
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <!-- end of rightColumnModule2TitleContainer -->
                            <div id="Div2" class="rightColumnModule2Content" runat="server">


                                            <div class="rightColumnModule2ContentSub">
                                    <div class="rightColumnModule2TitleSub">
                                        System Information 

                                        </div>

                                        <table class="table100">
                                        <tr>
                                            <td class="tableVT" style="width: 49%">
                                                <table class="table100">
                                                    <tr>
                                                        <td class="rightColumnModule2DetailLabel" style="width: 30%">
                                                            Create by &nbsp; </td><td class="rightColumnModule2DetailColon" style="width: 5%">
                                                            : </td><td class="rightColumnModule2DetailValue" style="width: 65%">
                                                            <asp:LinkButton ID="LinkButton1" runat="server" 
                                                               ></asp:LinkButton>&nbsp;<asp:Label
                                                                ID="Label3" runat="server"></asp:Label></td></tr></table></td><td class="gapHori1" style="width: 2%">
                                            </td>
                                            <td class="tableVT" style="width: 49%">
                                                <table class="table100">
                                                    <tr>
                                                        <td class="rightColumnModule2DetailLabel" style="width: 30%">
                                                            Modify By </td><td class="rightColumnModule2DetailColon" style="width: 5%">
                                                            : </td><td class="rightColumnModule2DetailValue" style="width: 65%">
                                                            <asp:LinkButton ID="LinkButton2" runat="server"></asp:LinkButton>&nbsp;<asp:Label
                                                                ID="Label4" runat="server"></asp:Label></td></tr></table></td></tr></table>

                                                                </div>
                            </div>
                          </div>  <!-- end of rightColumnModule2Content -->

如何在我的默认 aspx 页面中调用来自 ContactPersonList.ascx 的添加按钮?请指导我一个解决方案。谢谢

【问题讨论】:

    标签: asp.net function button user-controls call


    【解决方案1】:

    你可以有两种方法:

    1. 使用 FindControl 属性:

       Button btn1 =UserControlID.FindControl("btn1") as Button;
       if(btn1 != null)
       {
         //your stuff with button
       }
      

    2. 可以直接调用UserControl按钮的点击事件:

      用户控制页面:

      <asp:Button ID="btnClick" runat="server" Text="Button1" onclick="btn_Click" />
      

      代码背后:

      public event EventHandler ButtonClick;
      protected void btn_Click(object sender, EventArgs e)
      {
        ButtonClick(sender, e);
      }
      

      aspx 页面:

      <UC1:UC1 ID="UserControl1" runat="server" />
      

      后面的代码:

      protected void Page_Load(object sender, EventArgs e)
      {
         UserControl1.ButtonClick += new EventHandler(UserControl1_ButtonClick);
      }
      
      protected void UserControl1_ButtonClick(object sender, EventArgs e)
      {
         Response.Write("this is usercontrol button event.");
      }
      

    【讨论】:

    • 嗨,我想了解第一种方法。我应该在哪里使用查找控制方法?在默认页面还是 wat?
    • 对于第二种方法。我无法使用 UserControl1.ButtonClick += new EventHandler(UserControl1_ButtonClick 。因为我使用占位符来加载用户控件
    • 第一种方法:在默认页面中。第二种方法:你必须找到用户控件ID。
    • 您好,感谢您的帮助。你能解释一下我为什么要使用公共事件 EventHandler ButtonClick; protected void btn_Click(object sender, EventArgs e) { ButtonClick(sender, e); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2020-09-07
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多