鼠标点击事件添加到面板 [英] add mouseclick event to panel
本文介绍了鼠标点击事件添加到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要分配鼠标点击
事件 asp.net面板
:
protected void Page_Load(object sender, EventArgs e)
{
Panel p = new Panel();
p.Click += new EventHandler(b_Click);//but, this doesn't compiles correctly
}
protected void b_Click(object sender, EventArgs e)
{
//C#code
}
有什么办法click事件添加到面板?
Is there any way to add click event to panel?
推荐答案
下面是你可以做什么,使你的面板点击,并在服务器端处理该事件。
Here is what you can do to make your panel clickable and handle the event at server side.
在您的网页表单将面板
<asp:Panel runat="server" ClientIDMode="Static" ID="clickMe">
Click here
</asp:Panel>
添加jQuery脚本库到您的网页。
Add jQuery script library to your page.
<script src="http://code.jquery.com/jquery.min.js" language="javascript"
type="text/javascript"></script>
定义以下客户端事件处理程序
Define the following client side event handler
$(document).ready(function() {
$("#clickMe").click(function () {
__doPostBack('clickMe', '');
});
});
在服务器端处理该事件。
Handle the event at server side.
protected void Page_PreRender(object sender, EventArgs e)
{
this.Page.ClientScript.GetPostBackEventReference(clickMe, "");
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] == "clickMe")
{
ClickMeOnClick();
}
}
在preRender事件处理code是asp.net框架,在双方cilent __doPostBack渲染功能。如果您的网页包含导致自动回传,你不需要这个code控制。
The code in PreRender event handler is for asp.net framework to render __doPostBack function at cilent sides. If your page includes a control that causes an auto postback you don't need this code.
这篇关于鼠标点击事件添加到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文