在我完成这篇文章之前,我可能会回答我自己的问题,但如果我在这里失败了,我正在努力解决这个问题。
我需要从PhotoId
控件中获取Club:ImageThumbnail
值:
<asp:FormView ID="fvPhpto" runat="server" DataKeyNames="id"
Width="480px" AllowPaging="True"
PagerSettings-Visible="false">
<ItemTemplate>
<asp:Label Text='<%# Eval("title") %>' runat="server" ID="descriptionLabel" />
<Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("id") %>' />
</ItemTemplate>
...
这种事情int id = (int)fvPhpto.FindControl("thumb1").PhotoID;
不起作用,因为PhotoID不会出现在intellisence中。
这就是Club:ImageThumbnail
中的代码:
public object PhotoID
{
get
{
object o = ViewState["PhotoID"];
return (o != null) ? (int)o : 0;
}
set
{
ViewState["PhotoID"] = (value != null && value !=DBNull.Value) ? Convert.ToInt32(value) : 0;
}
}
public ImageSizes ImageSize
{
get
{
object o = ViewState["ImageSize"];
return (o != null) ? (ImageSizes)o : ImageSizes.Thumb;
}
set
{
ViewState["ImageSize"] = value;
}
}
public enum ImageSizes
{
Large = 0,
Thumb = 1,
FullSize = 2
}
public string NoPhotoImg
{
get
{
object o = ViewState["NoPhotoImg"];
return (o != null) ? (string)o : null;
}
set
{
ViewState["NoPhotoImg"] = value;
}
}
protected void Page_PreRender(object sender, System.EventArgs e)
{
if (Convert.ToInt32(PhotoID) == 0)
{
if (NoPhotoImg != null)
{
Image1.ImageUrl = NoPhotoImg;
}
else
{
Image1.Visible = false;
}
}
else
{
Image1.ImageUrl = "ImageFetch.ashx?Size=" + Convert.ToInt32(ImageSize) + "&ImageID=" + Convert.ToString(PhotoID);
}
}
<asp:Image ID="Image1" runat="server" CssClass="photo" BorderWidth="1" />
答案 0 :(得分:1)
您需要首先转换为用户控件类型,然后转换为int
以获取照片ID值,如下所示:
int id = (int)(fvPhpto.FindControl("thumb1") as ImageThumbnail).PhotoID;
如果将演员表拆分为两个步骤,首先是用户控件类型然后转换为int
,可能会更容易理解,如下所示:
ImageThumbnail theUserControl = fvPhpto.FindControl("thumb1") as ImageThumbnail;
// Since the C# as operator returns null for a failed cast, then we need to
// check that we actually have an object before we try to use it
if(theUserControl != null)
{
int id = (int)theUserControl.PhotoID;
}
注意:如果
ImageThumbnail
不是用户控件类的名称,请将ImageThumbnail
更改为用户控件类名的名称。