简体   繁体   中英

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,

<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. 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 .

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)

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();

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