简体   繁体   English

使用单个编辑按钮编辑转发器中的项目(超链接)

[英]Edit items (hyperlinks) in a repeater with a single edit button

I've got a repeater which is bound to a List<> generated from an XML file. 我有一个转发器,该转发器绑定到从XML文件生成的List <>。 The file consists of two nodes, ID and Item. 该文件包含两个节点,ID和Item。

<Items><ID>0</ID>
<Item><![CDATA[<a target='blank' href="http://www.cnn.com">CNN News</a> ]]></Item>

I need to provide Edit functionality to allow a user to edit the url and the text of each item in the repeater. 我需要提供“编辑”功能,以允许用户编辑转发器中每个项目的url和文本。 I don't want separate Edit/Save buttons for each row; 我不希望每行都有单独的“编辑/保存”按钮; too much clutter. 太混乱了。 I guess that means a single Edit/Save button, which would essentially result in the XML file being "created" anew on each Save. 我想这意味着只有一个“编辑/保存”按钮,这实际上将导致在每个“保存”上重新“创建” XML文件。 Or, might there be a better way to do this? 或者,也许有更好的方法可以做到这一点?

I guess I'm asking two things: 我想我要问两件事:

  1. A design suggestion on how to make a list of URLs editable (both url and text). 关于如何使URL列表(URL和文本)可编辑的设计建议。 Two separate text boxes I guess? 我猜两个单独的文本框?

  2. How to handle edits. 如何处理编辑。

Thanks. 谢谢。

  1. Yes, two separate fields is the way to go here 是的,这里有两个单独的字段
  2. As far as editing, you can have two panels for your ItemTemplate . 就编辑而言,您的ItemTemplate可以有两个面板。 One for View and one for Edit . 一种用于视图 ,一种用于编辑 The Edit panel's visibility is set to false by default. 默认情况下,“ 编辑”面板的可见性设置为false。 Each row has an "Edit" button with the item's ID as its CommandArgument , that when pressed, you set a Session variable with the ID of the item clicked. 每行都有一个“编辑”按钮,该项目的ID作为其CommandArgument ,在按下该按钮时,您可以设置一个带有单击的项目ID的Session变量。 Then when you rebind your repeater, you check each item's ID against the Session edit ID variable. 然后,当您重新绑定中继器时,请对照会话编辑ID变量检查每个项目的ID。 If they match, you set the View panel's visibility to false and the Edit panel's visibility to true. 如果它们匹配,则将“ 视图”面板的可见性设置为false,将“ 编辑”面板的可见性设置为true。 The Edit panel has two buttons, one for Save and one for Cancel. 编辑”面板有两个按钮,一个按钮用于保存,一个按钮用于取消。 Once the user clicks Save or Edit, you do any back-end processing, clear the Session variable and rebind the repeater. 用户单击“保存”或“编辑”后,您将执行任何后端处理,清除Session变量并重新绑定转发器。 Although there are built in controls that have edit functionality "built-in", I find the control I am alloted by using the Repeater is superior. 尽管有具有“内置”编辑功能的内置控件,但是我发现使用Repeater分配的控件是更好的。

I've used this method numerous times and it works great! 我已经多次使用这种方法,并且效果很好!

If you need any code to illustrate any of the point above, feel free to ask. 如果您需要任何代码来说明以上几点,请随时询问。

As a side not, your XML looks a little strange. 顺便说一句,您的XML看起来有些奇怪。 There doesn't seem to be a parent node to represent each URL/ID pair. 似乎没有一个父节点代表每个URL / ID对。 It this an oversight, a typo, or am I missing something here? 这是一个疏忽,还是一个错字,还是我在这里错过了一些东西?

EDIT: 编辑:

This is a good way to utilize ViewState in your scenario: 这是在您的方案中利用ViewState的好方法:

    private enum PageStates
    {
        None = 0,
        View = 1,
        Edit = 2
    }

    /// <summary>
    /// The current state of the page
    /// </summary>
    private PageStates PageState
    {
        get
        {
            if (ViewState["PageState"] == null)
                ViewState["PageState"] = PageStates.View; //default to view state

            return (PageStates)ViewState["PageState"];
        }
        set
        {
            ViewState["PageState"] = value;
        }
    }

By encapsulating ViewState access in a property, any changes to the method of storing the variable (Session, DB, etc) are abstracted away from any code that accesses it. 通过将ViewState访问封装在一个属性中,对存储该变量的方法(会话,数据库等)的任何更改都将从访问它的任何代码中抽象出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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