日期:2014-05-18  浏览次数:20373 次

为何变量的值SmlImgUrl永远是datalist绑定的最后一个label的值?
<asp:DataList   ID= "ShowOtherPicDL "   runat= "server ">
          <ItemTemplate   >
<img   src= '../ <%#Eval( "OtherImgUrlSml ")   %> '/> <asp:LinkButton   ID= "DelectPic "   runat= "server "   OnClick= "DelectPic_Click "> 删除图片 </asp:LinkButton> <asp:Label   ID= "SmlImgUrlLB "   runat= "server "   Text= ' <%#   Eval( "OtherImgUrlSml ")   %> '   Visible= "true "> </asp:Label>        
          </ItemTemplate>
</asp:DataList>

+++++++++++++++++++++++++++后台++++++++++++++++++++++++++++++++++
Public   SmlImgUrl   As   String

        Protected   Sub   ShowOtherPicDL_ItemDataBound(ByVal   sender   As   Object,   ByVal   e   As   System.Web.UI.WebControls.DataListItemEventArgs)   Handles   ShowOtherPicDL.ItemDataBound
                SmlImgUrl   =   CType(e.Item.FindControl( "SmlImgUrlLB "),   Label).Text.ToString
        End   Sub

        Protected   Sub   DelectPic_Click(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)
                System.IO.File.Delete(Server.MapPath( "../ ")   +   SmlImgUrl)
                BindData()
        End   Sub

+++++++++++++++++++++++++++++++++++++++++++++
比如有10张图片,无论我点哪一张图片的LinkButton,他永远是删除第10张图片,如果我再点任何一张图片,他就删除第9张图片,如此类推,总是从后面一张一张倒数删除。也就是得到的变量SmlImgUrl永远是datalist绑定的最后一个label的值。label显示的图片路径没有错啊。。是不是要写什么循环之类的?

------解决方案--------------------
还是没弄明白吗?

好好想想 DelectPic_Click 里面使用的 SmlImgUrl 怎么得到的

在下面这句打个断点,跟踪一下,看看执行了多少次,最后一次值是什么
SmlImgUrl = CType(e.Item.FindControl( "SmlImgUrlLB "), Label).Text.ToString

------------------------------------------
DataListItem item = btn.NamingContainer;

> >

DataListItem item = btn.NamingContainer as DataListItem;

今天代码写晕了,总丢三落四~



------解决方案--------------------
Ctype关键字
DirectCast关键字

http://blog.csdn.net/lzmtw/archive/2004/11/21/189758.aspx