extend asp.net repeater, writing c# code - c#

Im trying to extend repeater control to add pagination. I started with creating control which derives from Repeater but there is a problem:
public partial class controls_pagination : Repeater
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
That should I do to make it work ?

You can paging repeater using this code. please go through the link :
http://www.codeproject.com/KB/custom-controls/CollectionPager.aspx
http://www.codeproject.com/KB/webforms/SQLPager_For_Everything.aspx

Related

How to place Async="true" on masterpage

In my master page I will load some data from the database. I have place it into an asynchronous method. For normal pages I place Async="true" on the top but if I do it on the master page, I have the following error:
myproject.master does not contain a definition for AsyncMode and blablabla...
I've also search on the internet but nothing found for an asynchronous master page. Language I use on background is C#.
Can anyone help me?
The sample uses the new async and await keywords (available in .NET 4.5 and Visual Studio 2012) to let the compiler be responsible for maintaining the complicated transformations necessary for asynchronous programming. The compiler lets you write code using the C#'s synchronous control flow constructs and the compiler automatically applies the transformations necessary to use callbacks in order to avoid blocking threads.
ASP.NET asynchronous pages must include the Page directive with the Async attribute set to "true".
Master File contain master directive.
Master Page inherit MasterPage class of System.Web.UI which does not contain AsyncMode property..So you can't use it at master page.
Normal Page inherit Page class of System.Web.UI which contain AsyncMode.
You can set it in the master page like this. Found solution here:
public abstract class MyBasePage : System.Web.UI.Page
{
public MyBasePage()
{
this.AsyncMode = true;
}
}
Then change the inheritance in the aspx.cs file to something like this:
public partial class WebForm1 : MyBasePage
It can break the system when you set the AsyncMode property in anything else then the constructor.

Abstract ASP.NET User Control without recreating markup

I have the following control layout replicated several times in an ASP.NET application and would like to encapsulate the functionality in a user control (or server or composite control if necessary).
PSEUDO MARKUP
LISTBOX1 (Available Objects) > (Add Button) LISTBOX2 (Selected Objects)
Basically 2 listboxes with available objects in one listbox and selected objects in the other listbox (the type of these objects will change depending on use e.g. a collection of available and selected products)
Example usage: Add a listbox item from listbox1 to listbox2 via the add button (there are also buttons for adding all and going the other way from selected to available - these aren't shown for clarity) and the item is removed from listbox1 and placed in listbox2. Fairly straightforward - I'll call the control DualListbox.
I would like to do this:
DualListbox.ascx contains markup like this:
<asp:ListBox id="AvailableListBox" runat="server"/>
<asp:Button id="AddItemtoSelected" runat="server"/>
<asp:ListBox id="SelectedListBox" runat="server"/>
and DualListBox.ascx.cs contains a series of abstract and non abstract functions, properties etc.
e.g
--We don't actually use CollectionBase but a class derived from it
protected abstract CollectionBase AvailableItems {get;set;}
protected abstract CollectionBase SelectedItems {get;set;}
protected abstract void SaveContentsofAvailableListBox ();
private void FillAvailableListBox ()
{
.....
AvailableListBox.DataSource = AvailableItems;
AvailableListBox.DataBind()
}
private void MoveItemfromAvailabletoSelectedListBox()
{
..Some code that takes item from available and puts it in selected.
}
Then I'd inherit from DualListbox.ascx e.g.
ConcreteDualListBox : DualListBox
{
public override WidgetCollection AvailableItems {get;set;}
public override void SaveContentsofAvailableListBox(){}; etc.etc.
}
The problem is that you can't inherit markup and so the markup in DualListBox is unavailable to the concrete class. I can define the markup in the concrete class but then to use functions like FillAvailableListBox in the base class I would have to pass the AvailableListBox control (and all the other controls) into the base class from the concrete class.
Also for every concrete class the markup would have to be repeated (I could embed the common markup in each concrete class from another .ascx file I guess).
I would appreciate any suggestions on the correct way to go about defining such a control.
Apologies for the pseudocode - I'm doing this as a proof of concept at the moment.
Thanks,
Rich.
I don't think you can do this directly with a user control. What you can do is break this down into 2 controls, a user control and a regular server control. Keep the markup in the user control and have the server control override the OnInit method and load the user control and add it as a child. All the logic with abstract methods can be embedded in the server control which can be inherited.
To avoid layout issues you can create Templated ASP.NET User control. Here is an example from MSDN
I would suggest creating a function to generate the markup in the base class that child classes can call passing them the type of data they want to render/a template (this could even be a path to an ascx file)/a function that renders (child) data in the template (again in the child class).

Can't access master page from user control

My master page code looks something like this:
namespace Recipes
{
public partial class MasterPage : System.Web.UI.MasterPage
{
...
public void UpdateUserLogin()
{
NicknameLbl.Text = ((Recipes.BasePage)Page).CurrentUser.Nickname;
}
...
}
}
I want to call the UpdateUserLogin() method from a user control, something like this:
((Recipes.MasterPage)this.Page.Master).UpdateUserLogin();
But for some reason the compiler doesn't know Recipes.MasterPage (are you missing an assembly blablabla).
Sorry I can't show the exact error message, it's in French.
Maybe the problem is that I added the Recipes namespace around MasterPage manually, it wasn't added by VS.
By the way I'm using VS Web Developer Express 2008.
Do you have any idea how I can make this call work?
Both the MasterPage and the UserControl are child controls of the page they are used by. Your UserControl could potentially be used in a page that doesn't use your MasterPage, and so calling UpdateUserLogin() would not be valid.
You can check it like this, however, and make your call conditionally:
if (Page.Master is MasterPage)
{
((MasterPage)Page.Master).UpdateUserLogin();
}
UPDATE
It seems you were already aware of that, sorry. Your question is about the reference not working. What is the namespace of your UserControl?
I would recommend data binding the NicknameLbl to the CurrentUser.Nickname property. Then the NicknameLbl text will get updated automatically if the property changes.
include a MasterType directive at the top of the Content page ASPX file:
'<%# MasterType virtualpath="~/DetailsMaster.master" %>'
include a public method in the Master page
public void UpdateUserLogin(string value)
{
NicknameLbl.Text = value;
}
access the method from the Content page using the Master syntax:
Master.UpdateUserLogin(Some Text");
http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx
If your project is a Web Site Project (instead of a Web Application Project), then you do not have a project namespace. All code that is referenced from aspx.cs or master.cs files needs to be stored inside the App_Code directory, as the ASP.Net compiler will create several assemblies instead of just 1, and its not predictable which assembly will contain which aspx code.
Update after 1st comment:
The .ascx.cs and .aspx.cs stay where VS puts them. But it you want to reference classes etc, this needs to be placed inside App_Code, e.g. your Recipes.MasterPage or Recipes.BasePage objects.

How to access a user control's fields and methods without regitering the control?

I have a ASP.Net project that is setup in such a way that it can be dropped into any site and "just work." All the paths are relative to the current file, not relative to the "~". The paths are determined by ThePath = this.TemplateSourceDirectory;
This is working for everything expect registering a custom control that is created and added to one of the pages. I can add the control just fine with the Page.LoadControl but I cant cast it as the correct type to access anything.
How can I add a reference to the class from within the code itself?
If you don't know the control's specific type ahead of time, this isn't possible. The class must derive from UserControl, so you can cast it to a UserControl and you'll have access to all the methods and properties on that class. If there's some special information or functionality you need to require all controls to have, and you need to be able to assume those are always present, then you will have to write your own class that derives from UserControl, and require all custom controls to derive from that instead:
//all custom controls must inherit from this
public abstract class SpecialControlBase : UserControl
{
public abstract void DoSomethingSpecial();
}
Then you could cast all controls at load-time to this SpecialControlBase, and have access to the DoSomethingSpecial method.
But as far as the most-specific members of a class loaded at runtime, think about it - if I write my own control called RexsUserControl and drop it into your application, there's no way you could know what methods and fields I've put on my control, so you can't write any code that references those members specifically.
Rex M, If you register the control on the ASPX page, you can cast the control on the code behind.
In the ASPX:
<%# Register TagPrefix="Mine" TagName="Ctrl" Src="~/Test/User/Controls/UserCtrl.ascx" %>
Then, in the ASPX.CS:
User_Controls_UserCtrl myUserCtrl = LoadControl("~/Test/User/Controls/UserCtrl.ascx");
however, if you don't register the control in the ASPX first, you get this error:
CS0246: The type or namespace name 'User_Controls_UserCtrl' could not be found (are you missing a using directive or an assembly reference?)

Partial User Control

In Asp.net Web forms why must a user control be partial? I tried to remove the partial keyword and it says:
Missing partial modifier on
declaration of type 'test'; another
partial declaration of this type
exists
Where is the other declaration?
I am trying to pass a generic type with the user control how can I do that? I can't unless I change the other declaration too. I couldn't find it so I removed the partial keyword.
Note:
you do have 3 files if your making WebApplication but if your making website you only get 2 files !
UserControl.ascx
UserControl.ascx.cs
so in website where is the other declaration ?
the reason i want generic is because im making a Grid User Control so i want to pass the type the datasource is going to have.
ASP.NET uses partial classes for the codehind because it has to generate all the server side controls you have declared in your ASPX file to a class and merge all the other data files that come along with your codebind. It allow the classes ASP.NET uses to be distributed across multiple files.
From MSDN:
The code file contains a partial
class—that is, a class declaration
with the keyword partial (Partial in
Visual Basic) indicating that it
contains only some of the total code
that makes up the full class for the
page. In the partial class, you add
the code that your application
requires for the page.
This is the key part:
When the page is compiled, ASP.NET
generates a partial class based on the
.aspx file; this class is a partial
class of the code-behind class file.
The generated partial class file
contains declarations for the page's
controls. This partial class enables
your code-behind file to be used as
part of a complete class without
requiring you to declare the controls
explicitly.
What are you trying to do by adding a generic type to a user control? Can you accomplish this by adding a Type property to the UserControl and then using that?
They have to be partial because the designer autogenerates the stuff you do in the Design window. Usually that code is in the foo.designer.cs (or whatever the extension for an asp.net form is). If you want to change the class, you have to generate the UI manually without the designer or use the designer and copy the code over to your proper class.
There are actually three files for each user control.
UserControl.ascx
UserControl.cs
UserControl.designer.cs
The designer file has a partial implementation because of that your code behind also has to be partial.
They don't have to be partial, indeed I've never used a partial class for one. Some of the code-generation tools will use partial so that they can more readily alter bits based on GUI-work you do without you and it getting in each others way, but that's for the tools rather than for the code.

Categories

Resources