自定义用户控件

时间:2016-11-10 19:26:46

标签: c# asp.net

我相对较新的ASP.NET,并且在创建自定义用户控件时遇到问题。我正在尝试创建一个文本框和一个图像按钮。单击图像按钮时,会弹出一个GridView,其中包含各种选项供您选择。我还没有充实,但我遇到的问题是我的imageButton onClick事件没有发生。我在我的onClick命令上运行断点,但它们似乎从未触发过。

我查了很多论坛,但无法找到解决我问题的信息。我包括用户控件的代码以及运行它的Web表单。很抱歉问题有任何混乱。感谢您提供任何建议或指向信息。如果您认为有一个话题我应该谷歌不要害怕让我知道。对于丑陋的代码也很抱歉(我也会接受任何基本的编码建议)。顺便说一句,我已经为updatepanel / scriptmanager导入了AJAX Toolkit。提前谢谢。

- 自定义控制ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="CustomCntrl.WebUserControl1" ClassName="WebUserControl1" EnableViewState="true"%>

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" RenderMode="Inline" ID="UpdatePanel1">
    <ContentTemplate>
        <asp:TextBox ID="txtOutput" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:ImageButton ID="IBtnLkUp" CausesValidation="false" runat="server" ImageUrl="~/Images/Lookup.png" OnClick="IBtnLkUp_Click" OnCommand="IBtnLkUp_Command"  />

<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="IBtnLkUp" PopupControlID="Panel1" CancelControlID="btnCancel" DropShadow="true"></ajaxToolkit:ModalPopupExtender>

<asp:Panel ID="Panel1" runat="server">    
    <asp:GridView runat="server" ID="gvSrch" OnRowCancelingEdit="gvSrch_RowCancelingEdit" OnRowDeleting="gvSrch_RowDeleting" OnRowUpdating="gvSrch_RowUpdating" OnSelectedIndexChanging="gvSrch_SelectedIndexChanging"></asp:GridView>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />

</asp:Panel>

- 自定义控件ASCX.cs -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomCntrl
{    
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        public event EventHandler evLookupBtn;
        public event EventHandler evSelectBtn;
        public event EventHandler evDeleteBtn;
        public event EventHandler evEditBtn;
        public string sImgBtnUrl { get; set; }
        public DataTable dtLookup { get; set; }
        public bool bSelectBtn { get; set; }
        public bool bEditBtn { get; set; }
        public bool bDeleteBtn { get; set; }
        public bool bPaging { get; set; }
        public int pagesize { get; set; }

        protected void OnPreInit() {
            //IBtnLkUp.Click += IBtnLkUp_Click;
            //ImageButton ib = (ImageButton)IBtnLkUp;
            //ib.Click += new EventHandler(IBtnLkUp_Click(this.IBtnLkUp, ImageClickEventArgs.Empty ));
            Panel1.Controls.Add(IBtnLkUp);
        }
       /* public event ImageClickEventHandler IBtnLkUp_Click{
            add { IBtnLkUp.Click += value; }
            remove { IBtnLkUp.Click -= value; }
        }*/
        protected void Page_Load(object sender, EventArgs e)
        {
            //IBtnLkUp.Click += IBtnLkUp_Click;

            IBtnLkUp.ImageUrl = sImgBtnUrl;
        }

        protected void IBtnLkUp_Click(object sender, ImageClickEventArgs e)
        {               
            //Panel1.Visible = true;
            //UpdatePanel1.Visible = true;
            //gvSrch.Visible = true;
            gvSrch.Visible = true;
            //UpdatePanel1.Visible = true;
            Panel1.Visible = true;   

            gvSrch.AutoGenerateSelectButton = bSelectBtn;
            gvSrch.AutoGenerateEditButton = bEditBtn;
            gvSrch.AutoGenerateDeleteButton = bDeleteBtn;
            if (bPaging == true)
            {
                gvSrch.AllowPaging = bPaging;
                gvSrch.PageSize = pagesize;
            }
            else gvSrch.AllowPaging = false;

            gvSrch.DataSource = dtLookup;
            gvSrch.DataBind();
            if(evLookupBtn !=null)
            evLookupBtn(this, EventArgs.Empty);
            //Panel1.Visible = true;
            //UpdatePanel1.Visible = true;
            //gvSrch.Visible = true;
        }

        protected void gvSrch_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            evSelectBtn(this, EventArgs.Empty);
        }

        protected void gvSrch_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            evEditBtn(this, EventArgs.Empty);
        }

        protected void gvSrch_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            evDeleteBtn(this, EventArgs.Empty);
        }

        protected void gvSrch_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            gvSrch.EditIndex = -1;
            gvSrch.DataBind();//rebind data?
        }

      /*  protected void IBtnLkUp_Click1(object sender, ImageClickEventArgs e)
        {                
            //Panel1.Visible = true;
            //UpdatePanel1.Visible = true;
            //gvSrch.Visible = true;
            gvSrch.Visible = true;
            //UpdatePanel1.Visible = true;
            Panel1.Visible = true;    

            gvSrch.AutoGenerateSelectButton = bSelectBtn;
            gvSrch.AutoGenerateEditButton = bEditBtn;
            gvSrch.AutoGenerateDeleteButton = bDeleteBtn;
            if (bPaging == true)
            {
                gvSrch.AllowPaging = bPaging;
                gvSrch.PageSize = pagesize;
            }
            else gvSrch.AllowPaging = false;

            gvSrch.DataSource = dtLookup;
            gvSrch.DataBind();
            if (evLookupBtn != null)
                evLookupBtn(this, EventArgs.Empty);
            //Panel1.Visible = true;
            //UpdatePanel1.Visible = true;
            //gvSrch.Visible = true;
        }
   */
        protected void IBtnLkUp_Command(object sender, CommandEventArgs e)
        {

        }
    }
}

- Test.aspx文件 -

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="CustomCntrl.WebForm1"enableviewstate="true" %>

<%@ Register Src="~/UserControls/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>

<!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title runat="server"></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <uc1:WebUserControl1 runat="server" ID="WebUserControl1" sImgBtnUrl="~/Images/Lookup.png" bDeleteBtn="false" bEditBtn="false" bSelectBtn="true" bPaging="false"/>
        </div>
            <asp:Label ID="LabelTest" runat="server" Text=""></asp:Label>
        </form>
    </body>
    </html>

- 测试ASPX.CS -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomCntrl
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void WebUserControl1_evSelectBtn(object sender,EventArgs e) {
            LabelTest.Text = "Success";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
                DataTable dt = new DataTable();
                dt.Clear();
                dt.Columns.Add("Name");
                dt.Columns.Add("Marks");
                DataRow dr = dt.NewRow();
                dr[0] = "Smith";
                dr[1] = "1";
                dt.Rows.Add(dr);              

                WebUserControl1.dtLookup = dt;                
        }    
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您想要从UserControl向父页面发送命令。如果是这样,您必须使用Delegate而不是EventHandler

将其添加到用户控件

//declare the delegates
private Delegate _sendCommandToParentControl;
public Delegate sendCommandToParentControl
{
    set { _sendCommandToParentControl = value; }
}

//just a button click event handler
protected void sendCommandToParentControl_Click(object sender, EventArgs e)
{
    //send the textbox value to the parent by invoking the delegated command
    _sendCommandToParentControl.DynamicInvoke(TextBox1.Text);
}

然后在父页面

delegate void commandFromChildControlDelegate(string value);

protected void Page_Load(object sender, EventArgs e)
{
    //add the command to the usercontrol
    commandFromChildControlDelegate command = new commandFromChildControlDelegate(processCommandFromChildControl);
    WebUserControl1.sendCommandToParentControl = command;
}

//the command invoked from the child control
private void processCommandFromChildControl(string value)
{
    Label1.Text = value;
}