开发者

2 onclick events on 1 Div ? (ASP.NET / HTML)

I'm using Div's, now I have to handle 2 Events with one Div-Click..

On the Button I would take, once onclick="" and OnClientClick.. but if I use a Div I can't use OnClientCli开发者_开发问答ck :(

Can you guys tell me, what I shall do, to get this? (there is one JavaScript method and one codebehind method, which I want to call) - till now I can call only one.

after wizards help:

got this:

<asp:Button ID="fakeButton" runat="server" Text="dummy" onclick="dummyButton_Click" style="display:none" /> and JS:

alert("div clicked"); document.getElementById("<%=fakeButton.ClientID%>").click();

So but he dont enters the Method:

protected void dummyButton_Click(object sender, EventArgs e) { } ..

i putted a breakpoint, he never entered, only showed the alert.. –


First, add dummy hidden button with the proper server side click event:

<asp:Button id="fakeButton" runat="server" OnClick="MyMethod" Text="dummy" style="display: none;" />

Second, have such code for the DIV:

<div onclick="DivClick();">text here...</div>

And finally such client side code:

function DivClick() {
    //your client side code here...
    //e.g.
    alert("div clicked");

    //now invoke server side click as well:
    document.getElementById("<%=fakeButton.ClientID%>").click();
}

This will first execute your client side code e.g. the alert, then "auto click" the button and by this causing post back and the server side code to execute.

If you don't want full reload, try putting it all inside UpdatePanel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜