Search code examples
asp.netgridviewuser-controls

ASP.NET gridview user control


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 :(


Solution

  • 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.