按下时在 ascx 控件上创建单击事件

发布于 2024-09-12 11:05:57 字数 743 浏览 14 评论 0原文

我有一个关于在 ascx 自定义 Web 控件中创建和管理事件的问题。

我创建了一个非常愚蠢的控件,其中包含一个包含 asp:Label 控件的 div,它是一个非常简单的结构:

<div id="mydiv" runat="server">
     <asp:Label id="mylabel" text="Text"... />
</div>

也就是说,非常简单。 我想添加一个事件:点击。我想让用户在页面上添加此控件并将处理程序附加到此事件,以便当单击此控件时可以执行某些操作。这可能看起来是一个奇怪的解决方案,就像我试图再次发明按钮控件,即:我的自定义按钮。 为了触发该事件,我想在我的 div 中添加一个 javascript 并调用一个 js 函数,该函数使用 ajax 机制调用服务器端函数。 那么如何从这里调用服务器端函数。我发布了一个关于如何从客户端调用服务器端函数的问题,并得到了一些答案(其中许多人告诉我使用 PageMethods),看来 pagemethod 不起作用,它可以编译,但在运行并单击我的控制并执行js(在PageMethods.mymethod()行中)这里我有一个错误--> Java 脚本异常:无法识别的方法。好像没有找到PageMethod。

好吧,考虑到我的目标,我该怎么办? 啊,像这样的解决方案:在标签中使用单击事件不是我想要的,因为当我在 div 中单击时必须触发单击事件,考虑我可能会设置一个大的填充,以便大的空白空间可以提供大的可点击区域。

提前致谢。

I have a question about creating and managing events inside an ascx custom web control.

I have created a very stupid control consisting in a div containing a asp:Label control, it is a very simple structure:

<div id="mydiv" runat="server">
     <asp:Label id="mylabel" text="Text"... />
</div>

That is, very simple.
I would like to add an event: clicked. I want to let the user add this control on the page and attach handlers to this event so that when this control is clicked it is possible to do something. It might seem a strange solution, it's like i'm trying to invent again the button control, that is: my custom button.
Well to fire the event I would like to add a javascript in my div and call a js function that calls, using ajax mechanism, a server side function.
Well how to call a server side function from here. I posted a question about how to call a server side function from a client side one and got some answers (many of them told me to use PageMethods), well it seems that pagemethod does not work, it compiles but when running and clicking on my control and executing the js (in the line PageMethods.mymethod()) here I have an error --> Java script exception: unrecognized method. It seems not finding the PageMethod.

Well, considering my objective, how can I do?
Ah, a solution like: use the click event in the label is not what I want because the click event must fire when I click in the div, consider that I might set a large padding so that a large empty space can provide a large clickable area.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-09-19 11:05:58

PageMethods 在页面类中使用 Web 服务;我假设您想要回发到服务器并处理页面中的这次点击,对吧?

所有回发的控件都使用 __doPostBack('clientid', '') 方法回发到服务器。您的标签也需要这样做。在控件内,控件需要实现 IPostBackEventHandler 来处理这些事件,并引发单击事件。

查看此示例: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx

PageMethods uses a web service within the page class; I assume you want a postback to the server and process this click in the page, right?

All controls that postback use a __doPostBack('clientid', '') method to postback to the server. Your label would need to do the same. Within the control, the control needs to implement IPostBackEventHandler to process these events, and raise the click event.

Check out this example: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx

清音悠歌 2024-09-19 11:05:58

我可能误解了您的要求,但是您不能将 div 包装在 asp:linkbutton 中吗?

<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
 <asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>

然后在服务器端:

protected void lnk_something_Click(object sender, EventArgs e)
{

}

正如 Yellowfrog 在评论中提到的,您可以通过进一步将其包装在更新面板中以进行异步回发来扩展它。

I may have misinterpreted your requirements, but can't you just wrap the div in an asp:linkbutton?

<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
 <asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>

and then on the server side:

protected void lnk_something_Click(object sender, EventArgs e)
{

}

As yellowfrog mentioned in the comments, you could then extend this by further wrapping it in an update panel for an async postback.

柳若烟 2024-09-19 11:05:57

在用户控件中创建一个事件,

例如: -

public event EventHandler<CustomEventArgs> Click;

和一个处理程序

public void OnClick()
{
if(this.Click !=null)
{
    //what ever else you do
    this.Click(new CustomEventArgs(...));
}
}

还处理用户控件上的鼠标单击 (mouseleftbuttonup)(并从那里触发事件)。即

protected void OnMouseDown(...){ this.OnClick(); }

从 ASP.NET 页面,您可以注册用户控件事件并处理它:

MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)

protected myctr_click(object sender, CustomEventArgs e){
//do anything
}

Create a Event in your User Control

E.g: -

public event EventHandler<CustomEventArgs> Click;

and a handler

public void OnClick()
{
if(this.Click !=null)
{
    //what ever else you do
    this.Click(new CustomEventArgs(...));
}
}

Also handle the mouse click (mouseleftbuttonup) on the User Control (and fire your event from there). i.e.

protected void OnMouseDown(...){ this.OnClick(); }

From the ASP.NET page, you can register the user control event and handle it:

MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)

protected myctr_click(object sender, CustomEventArgs e){
//do anything
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文