Hi,
I have a aspx form with a Master Page. The Master Page has a RadScriptManager. The form has an UpdatePanel with a RadDockLayout and RadDockZone control. The logic will dynamically load RadDock controls. The RadDock controls each have an embedded usercontrol. The user control has navigation buttons. I cannot get the click events of the buttons in user control to fire.
I figured the solution was to register an AsyncPostBackTrigger event for each button. I tried to do this in three different ways with no success:
1)Pass the update panel to the user control. The user control registers the trigger and adds it to the update panel. I tried setting the controlid parameter to ID, CLientId and UniqueId. I get an error stating the the Update panel cannot find the "Control Name" .
2) Get a reference to the radscriptmanager and use RegisterAsyncPostBackControl to register each button control. I get no errors, but no click events either.
3) Provide properties for the client id in the user control. Register the user control in the form using method #1. I get the same error as #1 as well.
Thanks,
Al Rosner
Source for ASPX
Code Behind for ASPX
Source for control
Code behind for control
I have a aspx form with a Master Page. The Master Page has a RadScriptManager. The form has an UpdatePanel with a RadDockLayout and RadDockZone control. The logic will dynamically load RadDock controls. The RadDock controls each have an embedded usercontrol. The user control has navigation buttons. I cannot get the click events of the buttons in user control to fire.
I figured the solution was to register an AsyncPostBackTrigger event for each button. I tried to do this in three different ways with no success:
1)Pass the update panel to the user control. The user control registers the trigger and adds it to the update panel. I tried setting the controlid parameter to ID, CLientId and UniqueId. I get an error stating the the Update panel cannot find the "Control Name" .
2) Get a reference to the radscriptmanager and use RegisterAsyncPostBackControl to register each button control. I get no errors, but no click events either.
3) Provide properties for the client id in the user control. Register the user control in the form using method #1. I get the same error as #1 as well.
Thanks,
Al Rosner
Source for ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" |
MasterPageFile="~/loggedIn.master" %> |
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
<%@ Register Src="~/UserControls/Gadget.ascx" TagName="Gadgets" TagPrefix="uc" %> |
<asp:Content ID="ContentArea" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> |
<asp:UpdatePanel runat="server" ID="UpdatePanel1"> |
<ContentTemplate> |
<telerik:RadDockLayout runat="server" ID="rdlGadgets" OnSaveDockLayout="rdlGadgets_SaveDockLayout" |
OnLoadDockLayout="rdlGadgets_LoadDockLayout"> |
<telerik:RadDockZone runat="server" ID="rdzGadgets" Orientation="Horizontal" Width="100%" |
MinHeight="200" Style="float: left; margin-right: 20px;"> |
</telerik:RadDockZone> |
</telerik:RadDockLayout> |
</ContentTemplate> |
</asp:UpdatePanel> |
<br /> |
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /> |
</asp:Content> |
Code Behind for ASPX
using System; |
using System.Data; |
using System.Configuration; |
using System.Collections; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Web.UI.HtmlControls; |
using Telerik.Web.UI; |
using System.Collections.Generic; |
public partial class Home : System.Web.UI.Page |
{ |
private const string _createPage = "CreateGadget.aspx?Id="; |
private List<DockState> CurrentDockStates |
{ |
get |
{ |
//Store the info about the added docks in the session. For real life |
// applications we recommend using database or other storage medium |
// for persisting this information. |
//get data |
DataLayer.toReadGadgetsDataTable gadgets = DataAccess.GetGadgets(Users.GetUserInfo.UserId); |
List<DockState> currentDockStates = new List<DockState>(); |
foreach (DataRow dr in gadgets.Rows) |
{ |
DataLayer.toReadGadgetsRow row = dr as DataLayer.toReadGadgetsRow; |
currentDockStates.Add(CreateDockState(row)); |
} |
return currentDockStates; |
} |
set |
{ |
List<DockState> currentDockStates = value; |
foreach (DockState dockState in currentDockStates) |
{ |
DockStateTag tag = new DockStateTag(dockState.Tag); |
//update gadget in db |
DataAccess.UpdateGadget( |
int.Parse(dockState.UniqueName), |
tag.SearchCriteria, |
dockState.Title, |
dockState.Index, |
tag.DisplayRows |
); |
} |
} |
} |
private void CreateSaveStateTrigger(RadDock dock) |
{ |
//Ensure that the RadDock control will initiate postback |
// when its position changes on the client or any of the commands is clicked. |
//Using the trigger we will "ajaxify" that postback. |
dock.AutoPostBack = true; |
dock.CommandsAutoPostBack = true; |
AsyncPostBackTrigger saveStateTrigger = new AsyncPostBackTrigger(); |
saveStateTrigger.ControlID = dock.ID; |
saveStateTrigger.EventName = "DockPositionChanged"; |
UpdatePanel1.Triggers.Add(saveStateTrigger); |
saveStateTrigger = new AsyncPostBackTrigger(); |
saveStateTrigger.ControlID = dock.ID; |
saveStateTrigger.EventName = "Command"; |
UpdatePanel1.Triggers.Add(saveStateTrigger); |
} |
private RadDock CreateRadDockFromState(DockState state) |
{ |
RadDock dock = new RadDock(); |
dock.DockMode = DockMode.Docked; |
dock.ID = state.UniqueName; |
dock.ApplyState(state); |
dock.Commands.Add(new DockCloseCommand()); |
dock.Commands.Add(new DockExpandCollapseCommand()); |
DockCommand dc = new DockCommand(); |
dc.AutoPostBack = true; |
dc.Name = "Edit"; |
dock.Commands.Add(dc); |
dock.Command += new DockCommandEventHandler(dock_Command); |
return dock; |
} |
private DockState CreateDockState(DataLayer.toReadGadgetsRow row) |
{ |
DockState dockState = new DockState(); |
dockState.UniqueName = row.GadgetId.ToString(); |
dockState.Title = row.Title; |
dockState.Width = Unit.Pixel(300); |
DockStateTag tag = new DockStateTag(row.SearchCriteria, row.DisplayRows); |
dockState.Tag = tag.Tag; |
dockState.Index = row.Rank; |
return dockState; |
} |
private void Initialize() |
{ |
List<DockState> dockStates = CurrentDockStates; |
int count = dockStates.Count; |
for (int i = 0; i < count; i++) |
{ |
RadDock dock = CreateRadDockFromState(dockStates[i]); |
//We will just add the RadDock control to the RadDockLayout. |
// You could use any other control for that purpose, just ensure |
// that it is inside the RadDockLayout control. |
// The RadDockLayout control will automatically move the RadDock |
// controls to their corresponding zone in the LoadDockLayout |
// event (see below). |
//load gadget control into dock |
UserControls_Gadget c = (UserControls_Gadget)LoadControl("~/UserControls/Gadget.ascx"); |
c.ID = "Gadget" + i.ToString(); |
DockStateTag tag = new DockStateTag(dock.Tag); |
c.DisplayRows = tag.DisplayRows; |
c.SearchCriteria = tag.SearchCriteria; |
dock.ContentContainer.Controls.Add(c); |
rdzGadgets.Controls.Add(dock); |
c.SetTriggers(this.UpdatePanel1); |
//We want to save the dock state every time a dock is moved. |
CreateSaveStateTrigger(dock); |
} |
} |
protected void Page_Load(object sender, EventArgs e) |
{ |
//on init, load all docks in this event so display will occur in sequence |
if (!IsPostBack) |
{ |
Initialize(); |
} |
} |
protected void Page_Init(object sender, EventArgs e) |
{ |
//on postback, initialize dock controls so they will display |
if (IsPostBack) |
{ |
Initialize(); |
} |
} |
void dock_Command(object sender, DockCommandEventArgs e) |
{ |
if (sender is RadDock) |
{ |
RadDock radDock = sender as RadDock; |
switch (e.Command.Name) |
{ |
case "Edit": |
Response.Redirect(_createPage + radDock.UniqueName); |
break; |
case "Close": |
DataAccess.DeleteGadget(int.Parse(radDock.UniqueName)); |
break; |
} |
} |
} |
protected void rdlGadgets_LoadDockLayout(object sender, DockLayoutEventArgs e) |
{ |
//Populate the event args with the state information. The RadDockLayout control |
// will automatically move the docks according that information. |
foreach (DockState state in CurrentDockStates) |
{ |
e.Positions[state.UniqueName] = state.DockZoneID; |
e.Indices[state.UniqueName] = state.Index; |
} |
} |
protected void rdlGadgets_SaveDockLayout(object sender, DockLayoutEventArgs e) |
{ |
//Save the dock state. This will enable us |
// to recreate the dock in the next Page_Init. |
CurrentDockStates = rdlGadgets.GetRegisteredDocksState(); |
} |
protected void btnAdd_Click(object sender, EventArgs e) |
{ |
Response.Redirect(_createPage + "0"); |
} |
} |
Source for control
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Gadget.ascx.cs" Inherits="UserControls_Gadget" %> |
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
<telerik:RadGrid ID="rgGadget" runat="server" Skin="Telerik" ShowHeader="true" AllowPaging="true" |
ShowStatusBar="false" ShowFooter="False" AllowMultiRowSelection="False" GridLines="none" |
AutoGenerateColumns="false"> |
<MasterTableView NoMasterRecordsText="No Records Found."> |
<Columns> |
<telerik:GridBoundColumn HeaderText="Date Created" DataField="DateCreated" UniqueName="DateCreated" |
DataType="System.DateTime" /> |
<telerik:GridBoundColumn HeaderText="Doc Title" DataField="DocTitle" UniqueName="DocTitle" |
DataType="System.String" /> |
<telerik:GridBoundColumn HeaderText="Doc Name" DataField="DocNme" UniqueName="DocNme" |
DataType="System.String" /> |
</Columns> |
</MasterTableView> |
<HeaderStyle Width="250px" /> |
<ItemStyle Wrap="true" Width="100px" /> |
<AlternatingItemStyle BackColor="#F0F4F7" /> |
<ItemStyle BackColor="white" /> |
</telerik:RadGrid> |
<asp:Panel ID="pnlNavigation" runat="server"> |
<table width="100%"> |
<tr> |
<td> |
<asp:Button ID="btnFirst" runat="server" Text="<<" OnClick="btnFirst_Click" /></td> |
<td> |
<asp:Button ID="btnPrevious" runat="server" Text="<" OnClick="btnPrevious_Click" /></td> |
<td> |
<asp:Button ID="btnNext" runat="server" Text=">" OnClick="btnNext_Click" /></td> |
<td> |
<asp:Button ID="btnLast" runat="server" Text=">>" OnClick="btnLast_Click" /></td> |
</tr> |
</table> |
</asp:Panel> |
Code behind for control
using System; |
using System.Data; |
using System.Configuration; |
using System.Collections; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Web.UI.HtmlControls; |
using Telerik.Web.UI; |
public partial class UserControls_Gadget : System.Web.UI.UserControl |
{ |
protected void Page_Load(object sender, EventArgs e) |
{ |
} |
public int DisplayRows |
{ |
get { return (int)ViewState["DisplayRows"]; } |
set { ViewState["DisplayRows"] = value; } |
} |
public string SearchCriteria |
{ |
set |
{ |
ViewState["SearchCriteria"] = value; |
ViewState["PageNumber"] = 0; |
Bind(); |
} |
} |
public void SetTriggers(UpdatePanel updatePanel) |
{ |
RadScriptManager c = Page.Master.FindControl("ScriptManager1") as RadScriptManager; |
c.RegisterAsyncPostBackControl(this.btnFirst); |
c.RegisterAsyncPostBackControl(this.btnPrevious); |
c.RegisterAsyncPostBackControl(this.btnNext); |
c.RegisterAsyncPostBackControl(this.btnLast); |
//set triggers for all buttons so they can postback |
//AddButtonClickEventTrigger(updatePanel, this.btnFirst.UniqueID); |
//AddButtonClickEventTrigger(updatePanel, this.btnPrevious.UniqueID); |
//AddButtonClickEventTrigger(updatePanel, this.btnNext.UniqueID); |
//AddButtonClickEventTrigger(updatePanel, this.btnLast.UniqueID); |
} |
private void Bind() |
{ |
SearchEngineOutput searchEngine = SearchEngineFactory.GetSearchEngineData(ViewState["SearchCriteria"].ToString()); |
rgGadget.DataSource = searchEngine.Doc; |
rgGadget.DataBind(); |
if (searchEngine.Summary.Rows.Count > 0) |
{ |
SearchEngineOutput.SummaryRow row = (SearchEngineOutput.SummaryRow)searchEngine.Summary.Rows[0]; |
ViewState["NumFound"] = row.NumFound; |
if ((int)ViewState["NumFound"] <= (int)ViewState["DisplayRows"]) //no paging necessary |
{ |
pnlNavigation.Enabled = false; |
} |
else if ((int)ViewState["PageNumber"] == 0) //on first page |
{ |
this.btnFirst.Enabled = false; |
this.btnPrevious.Enabled = false; |
this.btnNext.Enabled = true; |
this.btnLast.Enabled = true; |
} |
else if (((int)ViewState["PageNumber"] + 1) * (int)ViewState["DisplayRows"] >= (int)ViewState["NumFound"]) //last page |
{ |
this.btnFirst.Enabled = true; |
this.btnPrevious.Enabled = true; |
this.btnNext.Enabled = false; |
this.btnLast.Enabled = false; |
} |
else |
{ |
//in middle |
this.btnFirst.Enabled = true; |
this.btnPrevious.Enabled = true; |
this.btnNext.Enabled = true; |
this.btnLast.Enabled = true; |
} |
} |
else |
{ |
pnlNavigation.Enabled = false; |
} |
} |
protected void btnFirst_Click(object sender, EventArgs e) |
{ |
ViewState["PageNumber"] = 0; |
Bind(); |
} |
protected void btnPrevious_Click(object sender, EventArgs e) |
{ |
ViewState["PageNumber"] = (int)ViewState["PageNumber"] - 1; |
Bind(); |
} |
protected void btnNext_Click(object sender, EventArgs e) |
{ |
ViewState["PageNumber"] = (int)ViewState["PageNumber"] + 1; |
Bind(); |
} |
protected void btnLast_Click(object sender, EventArgs e) |
{ |
if ((int)ViewState["DisplayRows"] > 0) |
{ |
int lastPageNumber = (int)ViewState["NumFound"] / (int)ViewState["DisplayRows"]; |
int pageNumberRemainder = (int)ViewState["NumFound"] % (int)ViewState["DisplayRows"]; |
if (pageNumberRemainder > 0) |
lastPageNumber++; |
ViewState["PageNumber"] = lastPageNumber - 1; |
Bind(); |
} |
} |
//public string FirstClientId |
//{ |
// get { return this.btnFirst.ClientID; } |
//} |
//public string PreviousClientId |
//{ |
// get { return this.btnPrevious.ClientID; } |
//} |
//public string NextClientId |
//{ |
// get { return this.btnNext.ClientID; } |
//} |
//public string LastClientId |
//{ |
// get { return this.btnLast.ClientID; } |
//} |
private void AddButtonClickEventTrigger(UpdatePanel updatePanel, string clientId) |
{ |
AsyncPostBackTrigger clickTrigger = new AsyncPostBackTrigger(); |
clickTrigger.ControlID = clientId; |
clickTrigger.EventName = "Click"; |
updatePanel.Triggers.Add(clickTrigger); |
} |
} |