ListView,图像和SQL问题

本文关键字:SQL 问题 图像 ListView | 更新日期: 2023-09-27 18:07:52

我有下一个代码:

    <asp:ListView ID="moreI" 
    runat="server" 
    DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">

    <ItemTemplate>
            <asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
 // and so on till the </ItemTemplate> and </asp:ListView>

我有下一个方法:

    protected void checkTheImage() 
{
    foreach (ListViewItem item in moreI.Items)
    {
        ((Image)item.FindControl("Image1")).ImageUrl = "noImage.jpg";
    }
}

和sql

    protected Boolean ImageCheck()
{
    SqlConnection connection = new SqlConnection(@"Data Source=.'SQLEXPRESS;AttachDbFilename=|DataDirectory|'***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    string CommandText2 = "SELECT noImage FROM Machreta WHERE noImage = 1";
    SqlCommand command2 = new SqlCommand(CommandText2, connection);
    connection.Open();
    Boolean check = (Boolean)command2.ExecuteScalar();
    connection.Close();
    //here i stoped without the return.

我有一个包含noImage (bit)列的表Machreta。

  • noImage = true,我想显示noImage.jpg来自checkTheImage()
  • noImage = false中,我想显示'<%# Eval("ImageUrl") %>'
我的逻辑有问题,可能是工作时间太长了…你有什么建议吗?

ListView,图像和SQL问题

I think you need to update the datasource that bind to the listview to contain the boolean variable that specify if to use the ImageUrl or the noimage.jpeg
<asp:ListView ID="moreI" 
    runat="server" 
    DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">

    <ItemTemplate>
            <asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# checkImage(Eval("ImageUrl"), Eval("noImage")) %>' />
 // and so on till the </ItemTemplate> and </asp:ListView>
  protected string checkTheImage(object ImageUrl, object noImage) 
{
    if((bool)noImage)
    {
        return "noImage.jpg";
    }
else
{
return ImageUrl.ToString();
}
}