如何将datalist的imageurl绑定到insert命令上datalist外侧的图像控件

时间:2011-08-17 07:39:58

标签: c# asp.net datalist

我在datalist的一侧有一个图像coltrol,我想在插入命令上将图像url分配给这个图像控件。

数据清单:

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Vertical" onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound" CaptionAlign="Right" CellSpacing="6">
<ItemTemplate>
<asp:ImageButton ID="Image" runat="server" ImageUrl='<%#"~/Controls/ShowImage.ashx?FileName=" +DataBinder.Eval(Container.DataItem, "FilePath") %>'
 OnCommand="Insert_Command" CommandArgument='<%# Eval("Id").ToString() +";"+Eval("FilePath")%>' />
 <asp:Label ID="lblimagenumber" runat ="server" Text='<%# DataBinder.Eval(Container.DataItem, "FileName") %>' ></asp:Label>
</ItemTemplate>
</asp:DataList>

图像控制在数据列表外:

<div id="loadarea" class="imageViewArea">
        <asp:Image ID="imgthumb" runat="server" />
    </div>

插入命令:

protected void Insert_Command(object sender, CommandEventArgs e)
    {
        string[] str = e.CommandArgument.ToString().Split(';');
        hImgInsId.Value = str[0];
        ImgName = str[1];

        //Image imgThumb = (Image)this.FindControl("imgthumb");
        //imgThumb.ImageUrl=
        //how to assign datalist image url to image contol here...?
        // this is datalist imagebtn imageurl ImageUrl='<%#"~/Controls/ShowImage.ashx?FileName=" +DataBinder.Eval(Container.DataItem, "FilePath") %>'
    }

   public void BindImages()
    {
        List<int> ImgIds = new List<int>();
        List<string> imgFileName = new List<string>();
        txbCaption.Text = "";
        if (ImgUpLoad.ImgFleIds != null)
            ImgIds = ImgUpLoad.ImgFleIds;
        if (ViewState["imgIds"] != null)
        {
            List<int> oldids = (List<int>)ViewState["imgIds"];
            ImgIds.AddRange(oldids);
            ImgIds.Sort();
        }
        path = objGetBaseCase.GetImages(ImgIds);
        for (int i = 0; i < path.Count; i++)
        {
            ArrayList alst = path[i];
            if (i == 0)
                hImgInsId.Value = alst[0].ToString();

            tb.Rows.Add(Convert.ToInt32(alst[0]), "Figure " + (i + 1).ToString(), alst[2].ToString());
            imgIds.Add(Convert.ToInt32(alst[0]));
            imgFileName.Add(alst[2].ToString());
        }
        ViewState["imgIds"] = imgIds;
        ViewState["imgFileName"] = imgFileName;
        DataList1.DataSource = tb;
        DataBind();
        ImgIds.Clear();
        if (DataList1.Items.Count == 1)
        {

            Label lblimgname = (Label)DataList1.Items[0].FindControl("lblimagenumber");
            lblimgname.Style.Add(HtmlTextWriterStyle.Color, "Red");
            lblimgname.Font.Bold = true;
            if (imgIdCapHtbl != null && imgIdCapHtbl.ContainsKey(imgIds[0]))
                txbCaption.Text = imgIdCapHtbl[imgIds[0]].ToString();
        }

    }

1 个答案:

答案 0 :(得分:1)

imgThumb.ImageUrl="~/Controls/ShowImage.ashx?FileName=" +ImgName; 

或者我错过了你的问题?