Problem to div event

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
I have
                <div align="center" onclick="Area0_Clicked" runat="server" enableviewstate="true" style="width: 600px; height: 120px;background-color:#3d7c94">
				...
        protected void Area0_Clicked(object sender, EventArgs e)
        {
            ...

Open in new window

but the event is not fired, when I click the div, on the page. Why?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Because the div is not a valid ASP.NET control Only valid controls are linked to the respective event handler by ASP.NET.
If you are trying to execute this server event you need to generate a postback (_doPostBack and handle the post back call yourself in the server logic.
HuaMin ChenProblem resolver

Author

Commented:
Can I have more details to such way? Thanks.
PawełI Design & Develop Software

Commented:
what are you trying to accomplish?
why do you want to add a click event to the div tag?

if you tell us this I would think that we can come up with a better solution to your problem.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

HuaMin ChenProblem resolver

Author

Commented:
I expect to trigger that event, once I've clicked any place within the div.
Senior Software Engineer
Top Expert 2009
Commented:
Add ID="clickMe" to div:
<div ID="clickMe" align="center" onclick="Area0_Clicked" runat="server" enableviewstate="true" style="width: 600px; height: 120px;background-color:#3d7c94">

Open in new window


Assuming you use Jquery, then add the following client side event handler in your aspx file: (could be in the start up section too)
$(document).ready(function() {
    $("#clickMe").click(function () {
        __doPostBack('clickMe', '');//this function creates the callback
    });
});

Open in new window


Handle the event at server side.
protected void Page_PreRender(object sender, EventArgs e)
{
    this.Page.ClientScript.GetPostBackEventReference(clickMe, "");//it renders __doPostBack function at cilent side
}

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form["__EVENTTARGET"] == "clickMe")
    {
        Area0_Clicked();
    }
}

protected void Area0_Clicked()
{
    ...
}

Open in new window

PawełI Design & Develop Software

Commented:
yes, but why? are you trying to use the div as a button? because you could just use an asp:button and then use css to style it.

if you must use a div, what i would do is handle the click event on the client side in JavaScript and then make an ajax call or post the page from JavaScript.
HuaMin ChenProblem resolver

Author

Commented:
I put your codes but I get this error
Error	5	The name 'clickMe' does not exist in the current context	C:\app\Start 150705\Start\Default.aspx.cs	130	62	Start

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Miguel,
Can you please take a look, if possible? Thanks to all.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
UHmm. please post the following info:
- VS/ASP.NET version.
- Page markup (aspx) file. You may not need to do this call at all if there are other asp.net controls generating postback in the page.
- Whether this page is master/child page.
 Post how the html is generated in browser. Want to check if the id is bein changed in the browser.
HuaMin ChenProblem resolver

Author

Commented:
I am running this using VS 2013 on Win 2008 R2 server.
This is not master/child page.
HTML is not generated.
HuaMin ChenProblem resolver

Author

Commented:
I am doing everything on Visual studio.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
I am confused by HTML you mean the markup you add to the aspx page?
Please post your aspx page contents as well. Keep in mind that aspx page is used to generate the hmtl.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial