Problem to div event

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?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSenior Software EngineerCommented:
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 resolverAuthor Commented:
Can I have more details to such way? Thanks.
PawełI Design & Develop SoftwareCommented:
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.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

HuaMin ChenProblem resolverAuthor Commented:
I expect to trigger that event, once I've clicked any place within the div.
Miguel OzSenior Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PawełI Design & Develop SoftwareCommented:
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 resolverAuthor 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 resolverAuthor Commented:
Miguel,
Can you please take a look, if possible? Thanks to all.
Miguel OzSenior Software EngineerCommented:
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 resolverAuthor 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 resolverAuthor Commented:
I am doing everything on Visual studio.
Miguel OzSenior Software EngineerCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.