c# - 添加鼠标点击事件到面板

标签 c# asp.net panel mouseclick-event

我想将 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/

相关文章:

c# - System.Text.Json - DefaultValueHandling = DefaultValueHandling.忽略替代方案

c# - PDF 强制下载而不是在浏览器中打开

javascript - ID.Replace 不起作用

python - 如何在 MouseOver 上更改 wx.Panel 背景颜色?

multithreading - 由TPanel父级创建的TButton的TThread创建

c# - 为什么 float 在声明时显示精确表示

c# - 如果选中复选框,请执行其他操作取消选中

c# - 如何处理从 LINQ to SQL 中的存储过程返回的 NULLable Int 列

asp.net - 调试:当类型标记为可序列化时如何调试 "Type is not marked as serializable"异常

c# - 重绘时如何修复面板闪烁?