Saturday, March 24, 2012

Trigger wont cause a PostBack (Inside UpdatePanel)

When I click my LinkButton inside my GridView I want the page to postback and a pdf to be streamed to the browser.
It is streamed but the page dont Post so nothing is received.

What am I doing wrong here?

1Protected Sub GridView1_RowDataBound1(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.GridViewRowEventArgs)Handles GridView1.RowDataBound23If e.Row.RowType = DataControlRowType.DataRowThen45Dim ptAs New PostBackTrigger()67pt.ControlID = e.Row.FindControl("lbtnPrintInvoice").UniqueID8UP1.Triggers.Add(pt)910End If1112End Sub

Hi,

in docs:

http://asp.net/AJAX/Documentation/Live/mref/T_System_Web_UI_PostBackTrigger.aspx

Remarks

Use thePostBackTrigger control to enable controls inside anUpdatePanel to cause a postback instead of performing an asynchronous postback.

Use theRegisterPostBackControl(Control) method of theScriptManager control to programmatically register a postback control. You can then call theUpdate() method of theUpdatePanel control when the trigger control performs a postback.

note

Programmatically addingPostBackTrigger controls is not supported.

So basically you need to use ScriptManager's RegisterPostBackControl method to do it.


Hmm I can apreciate that but how do I call my lbtnPrintInvoice inside Gridview1 thats the question... The below code doesnt work for some reason ;)

1<Triggers>2<asp:PostBackTrigger ControlID="lbtnPrintInvoice" />3</Triggers>

Gives the error:

A control with ID 'lbtnPrintInvoice' could not be found for the trigger in UpdatePanel 'UP1'.


You really read the previous reply?Big Smile

Does this help:

Protected Sub GridView1_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

ScriptManager.GetCurrent(Me).RegisterPostBackControl(e.Row.FindControl("lbtnPrintInvoice"))

End If

End Sub

-- EDIT: Fixed this reference in the code


Jeeze! I wans't aware you could actually call the scriptmanager like that since mine is in the masterpage I never thought I could adress it directly

Thanks a bunch for the solution though for people that might want the complete solution I added the page reference in the below code as well.

1Protected Sub GridView1_RowDataBound1(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.GridViewRowEventArgs)Handles GridView1.RowDataBound23If e.Row.RowType = DataControlRowType.DataRowThen4ScriptManager.GetCurrent(Me).RegisterPostBackControl(e.Row.FindControl("lbtnPrintInvoice"))5End If67End Sub

Oh and I also tried to do it in RowCreated, never would have worked out.


ScriptManager has good utility methods. It's worth to know about them. I edited & corrected the code. Made a typo as I typed directly to the post.

No comments:

Post a Comment