ASP.NET gridview user control - asp.net

Everybody use the grid view control in his project extremely, at least I do so.
I put a nice style to my grid and I erased the borders and cell spacing etc .
I also bind it from my database by a stored procedure. I write the paging method and the page index changing method etc .
Now I want to package it in a user control to use this grid in any project or page,
because it's to frustrating to make all of this at every page and every project.
But when I put it in a user control, I couldn't make a new columns and template field from the html.
uc1:WebGrid runat="server" ID="WebGrid" />
<!-- i want to be able to do this -->
<!--
<columns>
TODO
</columns>
-->
The answer:
this link is a very helpful except it's in a vb code :(

Putting the grid view into a user control will lock you off from making <Columns> definitions in your markup where you include your user control, because the grid view definition is not accessible via properties of the user control.
From the description of the changes you made that you want to reuse elsewhere, it seems to me that you are better off creating a CSS class that can be applied to all grid views in your application instead of a user control.
As for the paging logic, you can have that be in a utility class that all grid views in your application can call to do their paging, if the paging logic is generic enough.
The bottom line is you have locked the structure of your grid view by putting it into a user control, because there is no mechanism to alter the structure. You could try to build properties that allow for the alteration of the structure, but it will not be the familiar <Columns> syntax most people are used to.
UPDATE:
If you want to "extend" the grid view, then that is a whole different story and this is what companies like Telerik, etc. do so they can use the base functionality of the grid view and then provide value added.
Check out this previous StackOverflow question for details on extending the GridView and then exposing it as a control to be used elsewhere in your application - use class extending gridview as control.

Related

Web user control as masterpage with two child views

I'm building custom .NET web user control for Umbraco, which looks like single page, with a sidebar (which holds IDs of content items) on the left side of page, and two different content views(which have content by id displayed here) to the right of it.
The thing is, i don't know if this is possible to do something like masterpage/childs principle(in terms of this web user control), so i'll have something like "Master page" with this sidebar, and will render one of two child views to the right of it, depending on id selected from sidebar?
Or maybe there should be some workaround used here?
Will be much appreciated on any help here(like information about how that can be done, what information shall i seek for etc.), as i'm completely new in this kind of stuff.
From what I know, what you could possibly do is have your "master control" set up in your web user control, and then dynamically create one of two custom controls based on the values you choose from your master control. This is also known as a Master-Detail solution (provided a link).
Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView
If each of these controls was a user control in itself nested inside another user control, you could for example have three web user controls: Master, Child1 and Child2.
Using code-behind, you could easily create dynamic controls based on those you select from your Master control using Events to the "container page". Using these events, you could dynamically create your user controls/pass them variables, and so on.
Here are some articles about events, if you are interested:
Events in User Controls
Events in ASP.NET Server Controls
If you didn't want to dynamically create your controls, it wouldn't be difficult to bind them on demand dependent on the variable from your Master control, and hide them if they are not being used/bound.
You could also use the public properties of a control you define yourself. So say you have your Child1 user control, you could define a public property in the code behind. You can access this in design-time as well.
public int SpecialID { get; set; }

implementing mostly repeating content in ASP.NET WebForms' controls

The original problem is how to make the most maintainable code given that I need to have a menu bar that is very similar but not identical in most WebForms.
At first glance I thought I wanted to make a placeholder in a custom user control, which was placed in a master page, then access the placeholder, putting in the unique content within the WebForm and all is happy and maintainable. After reading through the forms and through trial and error, I realize there are some problems with every solution I have tried:
Putting a placeholder control in a user control makes the placeholder and each of its contents difficult to access in the web form (relying on nested instances of FindControl("ID")), events are difficult to bubble up (I assume I have to do this when the control is created in the code behind where it is referenced) or bind (not successful yet despite referencing: http://msdn.microsoft.com/en-us/library/t4z863dh(vs.71).aspx), and the code is difficult to maintain if I want to move/rename the control.
Nesting controls gives the same problem because I want to use one control - making small modifications on many pages. If I nest then I still have to have cross-control knowledge of each other which is difficult in ASP.NET. Regardless, if I nested user controls then I would also need a unique user control per WebForm right?
Copying and pasting the code to each WebForm while simple is also repetitive and error prone.
Apparently, I shouldn't even try to inherit user controls to make unique children: How (if at all) can you make an ASP.NET UserControl inherit from another UserControl?.
I'm new to ASP.NET. Is there some general strategy I should use to make good maintainable code in this scenario?
You need to use a Master Page, and put your menu in there...
http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx
Then, for a data-driven menu, try to use one of the template controls like the Repeater to bind your data.
For example, I have a query that returns Page Name and Link - I bind this to a Repeater control to produce my Menu.
<asp:Repeater runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate><li><a href='<% Eval("Link") %>'><%# Eval("PageName") %></a></li></ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Dynamically creating many instances of ASP.NET usercontrols: How do I create it, and where will my conflicts be?

I haven't seen this implemented before in ASP.NET, but am thinking about a UI that would look and act like this:
Conceptual Overview
A TabControl is loaded and the first tab contains a grid
When a row is double-clicked, a new tab is created with the record detail
The content of the tab/record detail is created by a usercontrol
Many tabs could be created, and therefore many instances of the usercontrol will be created
I know ASP.NET will rename my (runat="server") ID's for me, and that I can use jQuery or ASP.NET server-side code to work with the ID's... My concerns are:
How can I ask ASP.NET to generate a unique ID for each Nth instance of my usercontrol (to be rendered in a placeholder)
How do I actually create that extra instance of the control?
What else do I need to keep in mind?
Since I don't want postbacks I'm considering basing my implementation off of ComponentArt's Callback Control, and using ASP.net usercontrols to achieve this effect. This will allow me to do most things that require a postback, but won't refresh all the elements on a page... just the section that contains the user control. That being said, I'm not tied to a particular implementation.
You should look into the Page.LoadControl method. It works nicely and as far as I remember you put placeholders on your page and load the controls into the PlaceHolders, that's how you control the ids.
One thing that doesn't work out so well with this approach is when your control raises events that your Page object has to handle. If your control is selfcontained however you shouldn't have a problem.
This might help you get started:
http://www.codeproject.com/KB/aspnet/LoadingUSerControl.aspx

single template server control

Is it possible to have a custom server control with a single template (meaning the user can put any text they want) without having to require the an "ItemTemplate" like in a FormView control?
I would want the control in Source View to look like this
<foo:mycontrol runat="server" id="controlid">
User puts whatever html content they want here
</foo:mycontrol>
INSTEAD OF THIS
<foo:mycontrol runat="server" id="controlid">
<ItemTemplate>
User puts whatever html content they want here
</ItemTemplate>
</foo:mycontrol>
My custom server control needs to add 2 asp.net panel controls and the ajax collapsiblepanel control. one panel will be the expand/collapse panel and the other panel is what I would want to put the user text into and then have the collapsible panel collapse and hide the panel.
I know how to do this (at least I think I do) creating a composite server control and using ITemplate but that requires the child <ItemTemplate> tag in source view.
Any ideas?
I haven't tested this but I would think you could do this by inheriting from the literal or label control and then reading/writing to the Text property.
p.s. next time when you post a question check the preview to see if it's readable and format code with 4 spaces in-front so it's actually shown and syntax highlighted.
Hmm why don't you use an approach where you just specify the controls to be collapsed. For instance your declaration could look like
<foo:mycontrol runat="server" id="controlid" TargetControlId="pnlToCollapse" />
Internally, your mycontrol gets an instance of the specified TargetControlId by using the FindControl method (here's a recursive version). The same could be done for the 2nd panel you need.
Your server control does therefore just take configuration info and doesn't render anything, but controls the rendering of the other panels in this case. It is a much more flexible solution in my eyes.
Juri,
I should have clarified that this control might be used by non-developers or developers who we do not want to require too much knowledge of setting properties so I'm trying to develop a control where they would drag it on like a panel and just enter text and maybe set one property which would be the title. I was able to create a compositecontrol which I created a title panel, the collapsible panel and utilized the asp.net ajax collapsiblepanel control. I had to add an Template though which I would prefer not to do.
Darrell

ASP.NET: Bind Repeater using jQuery?

I have a Repeater control that I bind server-side. It repeats a series of divs, and does so with no problem. I have some buttons that I use to sort the repeater (newest, highest ranked, random) and this works the way it should.
I would like to improve my user experience by making the buttons sort the divs using Ajax/jQuery somehow so that there is no page postback and the user does not lose his/her spot on the page.
Is there a way to use jQuery to access server-side code like this, or use Ajax to re-bind a server-side control?
Thanks... if I need to list more details, please let me know!
EDIT I'm aware of UpdatePanels, but I would prefer not to use them if I don't have to.
Have you considered moving the Repeater's functionality to the client-side?
Doing it that way, functionality like paging and sorting is not very difficult to add. In fact, you can lean on the framework pretty heavily by using ADO.NET data services as the service layer.
It's relatively easy.
Move your repeater to a separate custom control, let's say MyControl. Now repeater in your page becomes uc1:MyControl.
Wrap MyControl into a div:
<div id="mydiv">
<uc1:MyControl ID="MyControl1" runat="server" />
</div>
Create a new page, pgMyControl.aspx, that contains MyControl only.
On your main page, add jQuery handlers to your sort links. Use load method to dynamically replace div contents:
$('#link_sort_random').click(function()
{
$("#mydiv").load("pgMyControl.aspx&sort=random");
}
Use QueryStringParameter in datasource inside MyControl to change order. Or use Request.QueryString in code-behind file.
Using an updatePanel or a jquery Ajax postback are the same thing essentially. Both will ask your code to fetch the new query, then make your control render itself, and then feed the HTML back to the client as a partial page render, and then insert the content in place of the old content in the same DOM location.
It is considerably harder to make JQuery and ASP.NET talk to each other this way due to the nature of web controls and their lifecycle that determines when they render. An updatePanel knows how to call all this, maintain proper viewstate and return the result to the correct location.
In this case, don't make things any harder on yourself, use the updatePanel unless you have some very specific reason not to.
EDIT: If you're having JQuery issues with update panels it is probably due to the fact that new DOM nodes being created. JQuery has the live event to handle this. It will notice when new DOM elements are created and match them against your selector even after the document ready.
Maybe it's an OT, but you can consider to change the way you bind even the client and the server control, using XSLT transformation instead od the classics server controls.
You can find an example here (sorry, it's in italian...).

Resources