Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何使用数据绑定动态分配图像的ImageUrl属性?_Asp.net_Visual Studio 2010_Data Binding - Fatal编程技术网

Asp.net 如何使用数据绑定动态分配图像的ImageUrl属性?

Asp.net 如何使用数据绑定动态分配图像的ImageUrl属性?,asp.net,visual-studio-2010,data-binding,Asp.net,Visual Studio 2010,Data Binding,我有一个SQL Server数据库,其中有一个表,其中列出了图像的文件名。我打算根据表中的数据在页面上分配图像控件的ImageUrl。我在页面上放置了一个SQLDataSource控件,然后尝试在其中放置一个FormView控件和一个Image控件。但我不知道如何通过数据绑定将值分配给ImageUrl属性。在任何类型的控件中,您都可以使用该控件返回数据,例如 <asp:imagebutton id="btnId" runat="server" ImageUrl='<%# Bind("

我有一个SQL Server数据库,其中有一个表,其中列出了图像的文件名。我打算根据表中的数据在页面上分配图像控件的ImageUrl。我在页面上放置了一个SQLDataSource控件,然后尝试在其中放置一个FormView控件和一个Image控件。但我不知道如何通过数据绑定将值分配给ImageUrl属性。

在任何类型的控件中,您都可以使用该控件返回数据,例如

<asp:imagebutton id="btnId" runat="server" ImageUrl='<%# Bind("ImgUrl") %>' />

玩一下,试试看。。。我做的大部分工作都是在DataGridView或Repeater中。。。但这并不重要,重要的是约束或评估

给你多一点信息

  <asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      DataSourceMode="DataReader"
      ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
      SelectCommand="SELECT LastName FROM Employees">
  </asp:SqlDataSource>

  <asp:SqlDataSource
      id="SqlDataSource2"
      runat="server"
      DataSourceMode="DataReader"
      ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
      SelectCommand="SELECT FirstName FROM Employees">
  </asp:SqlDataSource>


  <asp:ListBox
      id="ListBox1"
      runat="server"
      DataTextField="LastName"
      DataSourceID="SqlDataSource1">
  </asp:ListBox>
  <asp:ListBox
      id="ListBox2"
      runat="server"
      DataTextField="FirstName"
      DataSourceID="SqlDataSource2">
  </asp:ListBox>


让我知道这是否有帮助

进行此类绑定的更干净的方法,至少对我来说更干净,就是在ItemDataBound事件中处理绑定

所以你可以这样做:

Image imageToBind = e.Item.FindControl("imgTest") as Image;
image.ImageUrl = (string)DataBinder.Eval(e.Item.DataItem, "ColumnName");
我只是觉得这比在实际的标记中进行更优雅。

.aspx.cs

public string GetImage(string status)
    {
        if (status=="Active")

            return "~/images/green_acti.png";

        else

            return "~/images/red_acti.png";

    }
.aspx



如果仅用于显示,则应使用Eval而不是Bind。我倾向于同意。。。我不知道他最终打算做什么,所以我想从绑定开始。这看起来会奏效,但我还有一个后续问题。我在页面上有2个SqlDataSource控件。Bind()方法如何知道使用哪一个?@Rod。在绑定的控件中,将有一个DataSourceID。。。如果有多个数据源,则每个数据源都需要有不同的id。然后,控件根据数据源idI知道要从哪个数据源绑定。我认为添加EventHandler和添加两行代码比较干净,这两行代码将控件的id视为字符串,并在标记中的单个属性足够时使用强制转换代码隐藏。这个解决方案:势在必行。Patrick的解决方案:我想更多的声明性主观性。每当我看到一个到处都是Eval/Bind的中继器时,我就很恼火。我认为在代码背后处理它更干净,更容易查看/理解。
 <asp:TemplateField HeaderText="|| Status ||">
          <ItemTemplate>
                <asp:Image ID="imgGreenAct" ImageUrl='<%# GetImage(Convert.ToString(DataBinder.Eval(Container.DataItem, "truck_status")))%>' AlternateText='<%# Bind("truck_status") %>' runat="server" />                            
          </ItemTemplate>
 </asp:TemplateField>