2

i would like to create OnClick event for my panel. So far now the most of the google results look more or less like this: adding onclick event to aspnet label. Is there any way, to call codebehind function from javascript or panel attributes? Because I would like to Redirect user to a new page and before that save some information in ViewSTate or Sessionstate. Any suggestions?

2
  • 1
    If you need to access server variables like ViewState or Session anyway, you should postback and then Response.Redirect to the other page. Commented Jun 5, 2012 at 8:12
  • Unless you need access to Viewstate I would disagree with this, it would require an extra request to the server which is very expensive. Session would be available in the method I have outlined below. Commented Jun 5, 2012 at 8:45

3 Answers 3

2

In your java script method raise a __dopostback call to a Server side method.

<script type="text/javascript">
     function YourFunction()
     {
         __doPostBack('btnTemp', '')
     }
</script>

Where btnTemp is a server side button, so write a onClick event of this button on server side, where you can do the processing and then redirect to other page.

You can have a good understanding of dopostback at DoPostBack Understanding

Sign up to request clarification or add additional context in comments.

9 Comments

Should the btnTemp_Click() event fire? Because at the moment the page is just doing postback for me.
yeh it must be fired, and it is firing over here with me. During postback, in Page_Load event, check what value is coming in the variable, Request.Params["__EVENTTARGET"]? In positive scenario it should have btnTemp in it.
In Request.Params["__EVENTTARGET" it shows the btnTenp, but thw button click event is not firing. If i set a breakpoint there, it only fires if a button is pressed. Could you check my code? <script type="text/javascript"> function CallMe() { __doPostBack('btnTemp', '') } </script> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <asp:Button ID="btnTemp" runat="server" Text="Button" onclick="btnTemp_Click" /> </div>
protected void Page_Load(object sender, EventArgs e) { Label2.Attributes.Add("onClick", "CallMe();"); Label1.Text= Request.Params["__EVENTTARGET"]; } protected void btnTemp_Click(object sender, EventArgs e) { Response.Redirect("test.aspx"); }
I used your above code and in Page_Load event I wrote this line, Label1.Attributes.Add("onclick", "CallMe();"); so that CallMe is called when Label1 is clicked and when I click Label1 then btnTemp_Click is fired.
|
2

My aspx page is like:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script type="text/javascript">
        function CallMe() { __doPostBack('btnTemp', '') }
    </script>
</head>
<body>
    <form id="form1" runat="server">
         <asp:Button ID="btnTemp" runat="server" Text="Test" onclick="btnTemp_Click" />
         <div> <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
         <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>

And my Server Side code is as:

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Attributes.Add("onClick", "CallMe();");
    }
protected void btnTemp_Click(object sender, EventArgs e)
    {

    }

Thats the code that I have written, I haven;t included the using statement, Page directive etc in above code.

Comments

0

There is a PostBackUrl property on a ASP.NET Button, you could render the button as normal then postback to a different page - this is where your OnClick method would need to be declared.

I would strongly recommend against posting back to the same page then doing a Response.Redirect(), consider the traffic. The browser requests the page, posts back then is sent a HttpRedirect and then navigates to the new page. With the method I have outlined above this is not required and the browser has to make one request less (meaning the message doesn't have to be sent or the page rebuilt on the server) and is a significant performance benefit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.