Search code examples
c#asp.netdopostback

Raise post back event on click of div tag just like Asp button click


I want to raise post back event on click of div tag just we are doing with Asp button.

currently i am trying like this.

 protected void Page_Load(object Sender, EventArgs E)
 { 
  MyDiv.Attributes["onclick"] = 
              ClientScript.GetPostBackEventReference(this, "MyDiv_Click");
}

public void RaisePostBackEvent(string eventArgument)
{
      if (!string.IsNullOrEmpty(eventArgument))
            {
                if (eventArgument == "MyDiv_Click")
                {
                    MyDiv_Click();
                }
            }
        }

protected void MyDiv_Click()
{
// My Implementation
}

how to do that?


Solution

  • You need to register GetPostBackEventReference in Page_PreRender and in Page_Load , check for Request["__EVENTARGUMENT"], if that is MyDiv_Click and page is posted back, then invoke MyDiv_Click.

    Full Code

    protected void Page_PreRender(object sender,EventArgs e)
    {
        MyDiv.Attributes["onclick"] =
            ClientScript.GetPostBackEventReference(this, "MyDiv_Click");
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack && Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "MyDiv_Click")
        {
            MyDiv_Click();
        }
    }
    

    Screenshot -

    enter image description here