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

丶灬走出姿态 提交于 2019-12-23 04:42:15

问题


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 extra 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 =)


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/5688195/use-one-page-for-adding-and-editing-an-item-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!