开发者

Use one page for adding and editing an item in ASP.NET

I don't think the question is clear here.

Simply I have 19 types of items in my system. and I have 19 pages each one allow me to add a new item of specific type.

The "Add New Item" page and the "Edit an existing Item" page, are very similar from each other .. all what I need is to hide/show a couple of controls.

so I thought I'd use QueryString to define how we'll be using the page, if new then everything will remain the same and if it's used for "editing" then I'll change the Text properties for a couple of labels and show some e开发者_开发问答xtra TextBoxes and DropDownLists.

I could do this in a several ways but it will be a mess. I was hoping that someone could propose a way to do this keeping in my a good design and architecture.

Thanks for your time =)


An alternative to have one page that does both add/edit depending on parameters is to have two distinct pages that share a UserControl that provides the common UI. For example, if you have CustomerAdd.aspx and CustomerEdit.aspx thjey could share a CustomerProperties.ascx control that has textboxes for Name, Address, etc.


You could have two panels on your page, one for the add controls and one for the edit controls with both set visible=false.

Then you can either do page.aspx?do=add or page.aspx?do=edit and then use:

        If Request.QueryString("do") = "add" Then
            pnlAdd.Visible = true
        ElseIf Request.QueryString("do") = "edit" Then
            pnlEdit.Visible = true
        Else
           'Do some error handling.
        End If


1- Declare an InstanceState property for the page or the type. Make it read and write its value to a ViewState variable if it is for the page.

2- Use an enumuration to declare the possible values of this property. You can declare numerous of values to this property like (New, OnEdit, OnRead ).

3- Declare some other boolean properties to help you in customising your layout easily like (InstanceIsNew, InstanceIsOnRead, InstanceIsOnEdit ..). Those properties depend on the InstanceState property to return their values.

4- Bind your layout items to those properties to show, enable and what ever else you need to do on your layout items according to their values.

5- Change the value of the InstanceState on the appropriate events to change your layout.


NOTE: You might find it a bit complicated when you work on it for the first page. But once you understand the logic of it you can apply it easy and fast on the others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜