UserControl DirectMethod Not firing in Ext.net - asp.net

HI, I am Loading one UserControl(Which has the GridPanel in it ) into
tabPanel.
But when i click on Gridpanel Edit Command Button the Event at Server
side method not working and showing the Exception like
Exception Details: System.Web.HttpException: The control with ID
'id8b177c82adb2e925' not found.
My Code here is
For the userControl : the .ascx code is
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="GridPanelUserControl.ascx.cs" Inherits="Ext_PracticeExamples.GridPanelUserControl" %>
<ext:GridPanel ID="gpEmployeeList" runat="server" StripeRows="true" Title="Employee List" Layout="FitLayout"
Width="620" Height="400" AutoExpandColumn="FirstName">
<Store>
.....
</Store>
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:ImageCommandColumn ID="imgSettings" runat="server" Text="Settings" Resizable="false">
<Commands>
<ext:ImageCommand Icon="BasketEdit" Style="text-align: center" CommandName="Settings">
</ext:ImageCommand>
</Commands>
<Listeners>
<Command Handler=" #{DirectMethods}.fnDisplaySettings(record.data.Company,record.data.Price,record.data.Change);" />
</Listeners>
</ext:ImageCommandColumn>
</Columns>
</ColumnModel>
and the Corresponding .cs Class file method which is not firing.
[DirectMethod]
public void fnDisplaySettings(string name, double X, double Y)
{
// ..some Operation....
Response.Redirect("~/_Default.aspx");
}
while Observing the Source page in webBrowser i find that the Controls have
different Id so that Corresponding function not displaying.If i give
"ClientIdMode=static" it gives me the Unique Id problem when i load
the same control in the page. If i Use this code in Single page
without UserControl it's working fine.
What should i do Invoke the server Side Method.
Thank you

I guess you render a user control on the fly during one request and doesn't recreate it during another request, therefore Ext.NET can't find its DirectMethod, because there is no user control instance on server.
Possible solutions are:
Recreate a user control during each request. Here is a related discussion on the Ext.NET forums.
Setting up a DirectMethod's handler in, for example, a WebService (asmx) or an HTTP handler (ashx), and call it via URL. Such a call won't require a user control instance. Here are examples.
http://examples.ext.net/#/Events/DirectMethods/WebService/
http://examples.ext.net/#/Events/DirectEvents/WebService/
The disadvantage of this approach is the fact that you won't be able to access a user control's controls in a WebService or an HTTP handler. All required things should be submitted from a page as extra parameters of a request.
Placing a DirectMethod to the page.

Related

Accessing text boxes in a web control on a master page

I need to access multiple textboxes on a master page. The textboxes are declared in a separate web control and it won't let me call the names of the textboxes so I can populate them with data. I have multiple textboxes like I said, but for example, when I try to write to txtName it will not let me even though when I click on the Design View it says it is there.
Can anybody help me out?
Expose the text boxes inside the web control as properties.
In webcontrol.ascx
<asp:TextBox runat="server" id="txtName" />
In webcontrol.ascx.cs
public virtual TextBox TxtName { get {return txtName;} //note capitalization
Then do the same thing in the master page to expose the web control.
In masterpage.master
<uc1:MyWebControl runat="server" id="MyWebControl1" />
In mastermage.master.cs
public virtual MyWebControl myWebControl{get {return myWebControl1;}}
Then make your master page strongly typed from the content page by adding a MasterType directive.
In default.aspx
<%# MasterType TypeName="MyMasterPageClass" />
Then you can access it from your content page code behind. In default.aspx.cs
Master.myWebControl.TxtName.Text="Hello, world!";
The reason it's necessary to do this is that controls declared on .aspx, .ascx, and .master pages are protected instead of public and there's no way (as of right now) to change them that I'm aware of. So we can use properties to expose these controls as public.

Parent Page Load Event fires every time a User Control Event Fires

I have an .aspx page that dynamically creates multiple user controls, each of which have their own code behind which fires based on several events at the user control level. The problem is, before the events fire in a particular user control, the parent page Load method fires as well. Because each of the controls are added dynamically, this isn't a trivial amount of processing since each control must be re-added.
I'd like to be able to fire UserControl events without forcing an update on the page that hosts it. I've tried putting the entire user control in an UpdatePanel, but postbacks within the user control still force a page load for the parent.
Here's some test code with certain parts omitted and the control added declaratively, instead of programmatically.
.ASPX Parent Page
<%# Page Language="vb" CodeBehind="MyParent.aspx.vb" %>
<%# Register TagPrefix="uc" TagName="Control" Src="MyControl.ascx" %>
<form id="form1" runat="server">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<uc:Control ID="Control1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
.ASCX User Control
<%# Control Language="vb" CodeBehind="MyControl.ascx.vb" %>
<p>Control Added</p>
<asp:Button runat="server" Text="ForcePostBack"/>
This is how the ASP.NET WebForms page life-cycle works. Controls that postback will cause the host (read: parent) page to go through its Page_Load. If you do not like this model, then you should consider ASP.NET MVC or a client-side solution where you can make AJAX calls to acquire data and then do client-side rendering via templating logic.
Can you avoid it by having a Session variable like 'IsFirstLoad', then render the UCs only when IsFirstLoad is null (or true)?
Also, I've found that items rendered in Page_Init dynamically will persist through PostBack, so you wouldn't have to re-render. Don't know if that applies to UCs or not, though.

Using AjaxControlToolkit Accordion control in User control that dynamically added to page in asp.net

Hi I am trying to use AjaxControlToolkit's Accordion control in user control. That user control is added to page dynamically from code behind. My code for user control is as follows:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs"
Inherits="Project.UC.Control" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp" %>
<asp:Accordion ID="Accordion1" runat="server" HeaderSelectedCssClass="Sel"
HeaderCssClass="unSel" SelectedIndex="0">
<Panes>
<asp:AccordionPane runat="server" ID="AccordionPane1">
<Header>Red Orchid</Header>
<Content>Some content here.</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>
To add user control dynamically I used following code in my page's code behind
Control mycontrol = this.LoadControl("~/myUserControl.ascx");
this.Controls.Add(mycontrol);
On my user control consumer page I have form tag with runat="server" also added ScriptManager control for ajax functionality. When I run my code I am getting following error
Control 'ctl02_Accordion1_AccordionExtender' of type
'AccordionExtender' must be placed inside a form tag with runat=server.
I already used form with runat="server" tag on my consumer page why this problem is coming. To solve this problem I moved my consumer page's form tag to user control this solves my problem but by doing this I can't take advantage of postback on my consumer page. If I put form tag with runat="server" in both user control and consumer page it show error that you can use only one form tag on page. How to solve this problem.
Thanks in advance..
Add a PlaceHolder control onto a form and instead of this.Controls.Add(mycontrol); use PlaceHolder1.Controls.Add(mycontrol);
Or you need to add controls to form's Controls collection. In assumption that form has aspnetForm id you should write: aspnetForm.Controls.Add(mycontrol);
The reason is that Page class has own Controls property thus this.Controls.Add(mycontrol); line add control to Page.Controls collection. But all controls those submit any data to server must be placed in form element otherwise theirs values shouldn't be accessible on server side. This follows from html forms mature: HTML Forms and Input

Usercontrol errors in asp.net

Is there a way to universally force a page to error when a usercontrol raises an error? For example, if you write code that will create an error in a usercontrol, like this:
<asp:Label ID="lbl" runat="server" Text='<%# string.Format(("") %>' />
The page will still load like everything is working, but the portion of the page that the usercontrol occupies will contain the error "... ) expected." I want the page to actually error like it normally would if that bad code were on a normal .aspx page. Thanks in advance for any advice.
See How do I catch an exception raised on my ASCX control (not the code-behind)? for some details on this. One suggested solution involves creating an event in your user control that is fired if an exception is caught during the control's RenderChildren method. Then, your parent aspx page can handle the event raised in the ascx.

ASP.NET: CollapsiblePanelExtender doesn't work with UserControl

Iam trying to use a UserControl named CheckBoxControl as value for the ExpandControlID/CollapseControlID attributes of the CollapsiblePanelExtender. It works fine when I use a normal CheckBox instead.
<uc:CheckBoxControl ID="ucHaftpflicht" runat="server" OnCheckedChanged="UpdateStatus" ControlLabel="Haftpflicht" />
<asp:Panel ID="HaftpflichtPanel" runat="server">
...
</asp:Panel>
<asp:CollapsiblePanelExtender ID="CollapsiblePanelExtender" runat="server" CollapseControlID="ucHaftpflicht.CheckBox" ExpandControlID="ucHaftpflicht.CheckBox" TargetControlID="HaftpflichtPanel" />
When I run the site, most ajax controls on my site disappear and I get following errors in firefox script error console:
Fehler: Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method.
Fehler: Sys.ArgumentException: Failed to find element "ucHaftpflicht.CheckBox"
Parameter name: CollapseControlID
Expose the checkbox through a getter in your UserControl, and in the code-behind of the page assign the CollapsiblePanelExtender collapse/expand control IDs to ucHaftpflicht.MyCheckBox.ClientID

Resources