简体   繁体   中英

User Control Properties?

Can I build a control that basically acts "like a MasterPage"?

What I mean to do is, say I have a grid like this in a number of pages:

<asp:UpdatePanel ID="AnnouncementsPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:GridView ID="AnnoucementsGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" >
            <Columns>
                <asp:BoundField DataField="Title" HeaderText="Title" />
                <asp:BoundField DataField="Created" HeaderText="Date" />
                <asp:BoundField DataField="Modified" HeaderText="Last Modified" />
                <asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

I'd like to build a control that handles most of the codebehind, but I need to declare the columns for the grid declaratively in each case.


Is there a way to create a control like this?

<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Created" HeaderText="Date" />
        <asp:BoundField DataField="Modified" HeaderText="Last Modified" />
        <asp:ButtonField ButtonType="Button" Text="Process" CommandName="Process" />
    </Columns>
</uc:CrudGrid>

or event better:

<uc:CrudGrid ID="AnnouncementsCrud" runat="server">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:BoundField DataField="Created" HeaderText="Date" />
        <asp:BoundField DataField="Modified" HeaderText="Last Modified" />
    </Columns>
</uc:CrudGrid>

Maybe having to name the tag "Fields", but being able to drop the button, so it can be used in the UC's code-behind?

A lot of love to whoever has a positive answer on this <3

Found the exact solution I was looking for:

    [DefaultValue((string)null)]
    [Editor(typeof(System.Web.UI.Design.WebControls.DataControlFieldTypeEditor), typeof(UITypeEditor))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public DataControlFieldCollection Columns
    {
        get { return Grid.Columns; }
    }

this way I can expose the Columns from the grid in my own user control and edit the fields from the markup, keeping general functionalities within my UC

Depends on how positive you want it to be :-) You can always build your own control to do this. That is one option.

Another option is to create a helper object that attaches to the grid, and keep each grid separate.

Third option is to create a user control with the common code, and programmably add columns to the grid through the user control.

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM