简体   繁体   English

如何将项目从一个数据列表传输到另一数据列表?

[英]How to transfer item from one datalist to other datalist?

I have a datalist 我有一个数据清单

 <asp:DataList ID="dlstImage" runat="server" RepeatDirection="Horizontal" RepeatColumns="5"
                                CellSpacing="8">
        <ItemTemplate>
           <asp:ImageButton ID="Image" runat="server" ImageUrl='<%#"~/Controls/ShowImage.ashx?FileName=" +DataBinder.Eval(Container.DataItem, "FilePath") %>'
                                        OnCommand="Select_Command" CommandArgument='<%# Eval("Id").ToString() +";"+Eval("FilePath")+";"+Eval("Index") %>' /><br />
           <asp:Label ID="lbl" runat="server" Text="Figure"></asp:Label><%# dlstImage.Items.Count + 1%>
        </ItemTemplate>
  </asp:DataList>

In which i am binding the image after uploading through uplodify upload, now i have one more datalist and two btn up and down, 在通过uplodify上传完成上传后,我在其中绑定图像,现在我又有一个数据列表,上下两个btn,

<asp:ImageButton ID="ibtnMoveUp" runat="server" ImageUrl="~/App_Themes/Default/Images/moveup.bmp"
                        Style="height: 16px" ToolTip="MoveUp The Item" />
<asp:ImageButton ID="ibtnMoveDown" runat="server" ImageUrl="~/App_Themes/Default/Images/movedown.bmp"
                        ToolTip="MoveDown The Item" /> 

<asp:DataList ID="dlstSelectedImages" runat="server" RepeatDirection="Horizontal"
                                RepeatColumns="5" CellSpacing="8">
    <ItemTemplate>
        <asp:ImageButton ID="Image" runat="server" /><br />
        <asp:Label ID="lbl" runat="server" Text="Figure"></asp:Label><%# dlstImage.Items.Count + 1%>
    </ItemTemplate>
 </asp:DataList>

My both datalist is in the same webuser control, datalist1 and datalist2 and I have 2 btn up and down, when i select one image from datalist1 and click on down btn then the selected image should move to datalist2. 我的两个数据列表都在同一个webuser控件中,即datalist1和datalist2,并且我上下有2个btn,当我从datalist1中选择一个图像并单击btn时,则所选图像应移至datalist2。 How to do that? 怎么做? someone please help me, 有人请帮助我,

You need to handle the ItemCommand event of one DataList in which you have to copy the selected data (image) into another dataSource of two DataList and remove that item from the datasource of one DataList . 您需要处理一个 DataList的ItemCommand事件,在该事件中,您必须将所选数据(图像)复制到两个 DataList另一个dataSource中,并从一个 DataList的数据源中删除该项目。

Markup: 标记:

<asp:DataList 
            ID="DataList1" 
            runat="server"
            OnItemCommand="PerformMove" 
            >
        <ItemTemplate>
        <br /><%#Eval("Text") %>
        <asp:Button ID="btn1" 
                runat="server" 
                Text="Move"
                CommandName="cmd"
                CommandArgument='<%#Eval("Text") %>'
                />

        </ItemTemplate>
</asp:DataList>
<asp:DataList ID="DataList2" runat="server">
            <ItemTemplate>
            <br /><%#Eval("Text") %>
            </ItemTemplate>
</asp:DataList>

Code-behind (.cs) 后台代码(.cs)

public class Data
    {
        public string Text { get; set; }
        public override int GetHashCode()
        {
            return Text.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            return GetHashCode() == obj.GetHashCode();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Data> list1 = new List<Data >()
            {
                 new Data() { Text="One"},
                 new Data() { Text="Two"},
                 new Data() { Text="Three"},
            };
            List<Data> list2 = new List<Data>();
            Session["list1"] = list1;
            Session["list2"] = list2;

            DataList1.DataSource = Session["list1"];
            DataList1.DataBind();

            DataList2.DataSource = Session["list2"];
            DataList2.DataBind();
        }
    }
    protected void PerformMove(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
            List<Data> list1 = Session["list1"] as List<Data>;
            List<Data> list2 = Session["list2"] as List<Data>;

            list1.Remove(new Data() { Text=e.CommandArgument.ToString() });
            list2.Add(new Data() { Text = e.CommandArgument.ToString() });
            DataList1.DataSource = Session["list1"];
            DataList1.DataBind();

            DataList2.DataSource = Session["list2"];
            DataList2.DataBind();
        }
    }

I am using this code and its working well for me. 我正在使用此代码,它对我来说效果很好。

    ArrayList ImgArry = new ArrayList();
    path = objGetBaseCase.GetImages(TotImgIds);
    ImgArry.Add(SelImgId);
    ImgArry.Add(SelImgpath);//image name
    ImgArry.Add(SelImgName);//image path
    //path.Remove(ImgArry);
    List<ArrayList> t = new List<ArrayList>();
    if (newpath.Count > 0)
        t = newpath;
    t.Add(ImgArry);
    newpath = t;
    for (int i = 0; i < newpath.Count; i++)
    {
        ArrayList alst = newpath[i];
        newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

    }
    dlstSelectedImages.DataSource = newtb;
    DataBind();

    path = objGetBaseCase.GetImages(TotImgIds);
    for (int i = 0; i < path.Count; i++)
    {
        ArrayList alst = path[i];
        tb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

    }
    dlstImage.DataSource = tb;
    DataBind();

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

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