c# - How do you write a server control in order for it to support partial postbacks in ASP.NET WebForms? -


i've got following issue: i've got webforms page contains column layout flickering when reloading custom server controls within placeholder ph1.

to avoid thinking improving our servercontrols support partial postbacks. don't quite know how accomplish this. when control triggers postback want udp2 update contents instead of udp1 reacting raised postback.

my page structure this:

<updatepanel id="udp1">  <tree>  </tree>  <updatepanel id="udp2">   <contenttemplate>    <div id="ph1" runat="server"></div> <!--- receives generated servercontrol's   </contenttemplate>  </updatepanel> </updatepanel> 

so far attempts of making work did not work out , udp1 reacted , reloaded entire content.

can give me useful tips on how correctly?

creation of servercontrols happens :

// following function called in onload/indexchanged on combobox/tabchanged on tabs of page

public void createcontrols() {  var dataitems = getdata();  foreach(var data in dataitems)  {   var control = createservercontrol(data); // typeof customservercontrol   control.id = "generated"+data.identifier.tostring();   ph1.controls.add(control);  } } 

is there interface of sort have implement on customservercontrol cause control updated when raising postback within next available updatepanel (but udp2, not upd1)

code used within customservercontrol initiate postback on client:

var options = new postbackoptions(this, "update");  // turns javascript:__dopostback('somerenderedid','update'); link.href = string.format("javascript:{0};", this.page.clientscript.getpostbackeventreference(options));  

edit 1:

as alexander pointed out had @ this, exactely problem. went ahead, created sample project servercontrol: , placed in page

[parsechildren(false)] public class testcontrol : webcontrol {     protected override htmltextwritertag tagkey     {         { return htmltextwritertag.a; }     }      protected override void onprerender(eventargs e)     {         var pbo = new postbackoptions(this, "update");         this.attributes.add("href", string.format("javascript:{0};", this.page.clientscript.getpostbackeventreference(pbo)));          base.onprerender(e);     } }  <%@ page language="c#" autoeventwireup="true" %> <%@ register tagprefix="a" namespace="nestedupdatepaneltest"             assembly="nestedupdatepaneltest" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server"> <title>untitled page</title> </head> <body> <form id="frmmain" runat="server"> <asp:scriptmanager runat="server" enablepartialrendering="true" id="thescriptmanager" /> <asp:updatepanel id="aupparent" runat="server" updatemode="conditional">     <contenttemplate>         <a:testcontrol runat="server">outer</a:testcontrol>         <asp:button id="btnparent" runat="server" text="update parent (and childs of course)" />         <%= "parent panel last updated at:" + datetime.now.tostring( ) %>         <asp:updatepanel id="aupchild" runat="server" updatemode="conditional">             <contenttemplate>                 <a:testcontrol runat="server">inner</a:testcontrol>                 <asp:button id="btnchild" runat="server" text="update child" />                 <%= "child panel last updated at:" + datetime.now.tostring( ) %>             </contenttemplate>         </asp:updatepanel>     </contenttemplate> </asp:updatepanel> </form> </body> </html> 

pressing asp:button elements exactely want. clicking links not. what's necessary changes testcontrol make work buttons?

this works:

however i'm not sure wether that's intended way go making servercontrol update closest updatepanel? corrections , feedback welcome. otherwise i'll mark answer.

(i wish past me knew sooner)

[parsechildren(false)] public class testcontrol : webcontrol, ipostbackeventhandler {     protected override htmltextwritertag tagkey     {         { return htmltextwritertag.a; }     }      protected override void onprerender(eventargs e)     {         var pbo = new postbackoptions(this, "update");         this.attributes.add("href", string.format("javascript:{0};", this.page.clientscript.getpostbackeventreference(pbo)));         scriptmanager.getcurrent(this.page).registerasyncpostbackcontrol(this);          base.onprerender(e);     }      // works when used in updatepanel.triggers property.     public event eventhandler someevent;      public void raisepostbackevent(string eventargument)     {         var h = someevent;         if(h != null)            h(this, eventargs.empty);          updatepanel p;         control parent = this.parent;         while (parent != null)         {             p = parent updatepanel;             if (p != null)             {                 p.update();                 return;             }              parent = parent.parent;         }     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -