我對ASP.NET相對來說比較陌生,並且在創建自定義用戶控件時遇到問題。我正在嘗試創建一個文本框和一個圖像按鈕。當點擊圖像按鈕時,GridView會彈出各種選擇選項。我沒有充實所有東西,但我不斷遇到的問題是我的imageButton onClick
事件不會觸發。我在我的onClick
命令上運行一個斷點,但他們似乎從未開火。事件不會在自定義用戶控件中觸發
我檢查了很多論壇,但找不到解決我的問題的信息。我包含用戶控件的代碼以及運行它的Web表單。對不起,在這個問題上有任何混淆。感謝您的任何建議或信息點。如果你認爲有一個話題,我應該谷歌不要害怕讓我知道。對於醜陋的代碼也很抱歉(我也會接受任何基本的編碼建議)。順便說一句,我已經爲updatepanel/scriptmanager導入了AJAX Toolkit。提前致謝。
--Custom控制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>
--Custom控制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>
--test 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;
}
}
}
非常感謝。我閱讀了一些關於代表的.Net文章,但無法完全理解它。似乎我不得不和他們一起玩弄一些想法。看起來Delegate存儲一個事件,將該事件發送到父頁面。父頁面然後對該命令作出反應。再次感謝,當我明天回去工作時,我將不得不嘗試這個。 – wsbobbitt
今天嘗試執行此操作。似乎這是正確的方式去做事情。我一直在閱讀代表並觀看一些視頻。仍然無法解決問題。看起來我需要在我的用戶控件中將事件處理程序指向委託。週一我會再討論這個問題。在我學習的所有編碼概念中,代表似乎是一個更抽象/更難學的概念。 – wsbobbitt
我終於完成了我的解決方案。我最終沒有將任何事件傳遞給我的父頁面。我剛剛重寫了解決方案以處理控件中的所有事件。代表證明太多,無法處理這一秒。如果有人向代表推薦任何優秀的文章,請告訴我。 – wsbobbitt