我有一个表单视图,其中有一个asp:Image
控件:
<asp:FormView runat="server" ID="ListStories" DefaultMode="ReadOnly" >
<ItemTemplate>
<table>
<tr><td><%#Eval("Subject") %></td></tr>
<tr><td><%#Eval("Story") %></td></tr>
<tr><td><%#Eval("UserName")%></td></tr>
<tr><td><asp:Image ID="ImageAuthor" runat="server" /></td></tr>
</table>
</ItemTemplate>
</asp:FormView>
我正在尝试ImageUrl
从后面的代码中设置属性,如下所示:
Image imageAuthor = (Image)ListStories.FindControl("ImageAuthor") as Image;
conn = new SqlConnection(connString);
cmdStories = new SqlCommand("SELECT Stories.UserName, Stories.Subject, Stories.Story, UserProfile.Photo FROM Stories INNER JOIN UserProfile ON Stories.UserName=UserProfile.UserName", conn);
conn.Open();
reader = cmdStories.ExecuteReader();
while (reader.Read())
{
string userName = reader["UserName"].ToString();
imageAuthor.ImageUrl = "~/ImageHandler.ashx?name"+userName ;
}
ListStories.DataSource = reader;
ListStories.DataBind();
conn.Close();
但这给了我一个错误:
Object reference not set to an instance of an object.
我哪里错了?谢谢