我想将 mouseclick
事件分配给 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
}
有没有办法给面板添加点击事件?
最佳答案
您可以通过以下方式使面板可点击并在服务器端处理事件。
在您的网络表单中放置面板
<asp:Panel runat="server" ClientIDMode="Static" ID="clickMe">
Click here
</asp:Panel>
将 jQuery 脚本库添加到您的页面。
<script src="http://code.jquery.com/jquery.min.js" language="javascript"
type="text/javascript"></script>
定义以下客户端事件处理程序
$(document).ready(function() {
$("#clickMe").click(function () {
__doPostBack('clickMe', '');
});
});
在服务器端处理事件。
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 事件处理程序中的代码供asp.net 框架在客户端渲染__doPostBack 函数。如果您的页面包含导致自动回发的控件,则您不需要此代码。
关于c# - 添加鼠标点击事件到面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739979/