【问题标题】:Referencing a control as an incremented variable将控件引用为增量变量
【发布时间】:2016-04-25 06:57:18
【问题描述】:

我正在尝试用不确定数量的图像填充表格。我知道这个数字永远不会超过 6 个。

我所做的是这样的:

asp.net

<table>
    <tr>
        <td><img id="igImage1" src="Image1" alt=""/></td>
        <td><img id="igImage2" src="Image2" alt=""/></td>
        <td><img id="igImage3" src="Image3" alt=""/></td>    
        <td><img id="igImage4" src="Image4" alt=""/></td>
        <td><img id="igImage5" src="Image5" alt=""/></td>
        <td><img id="igImage6" src="Image6" alt=""/></td>    
    </tr>
</table>

c# 代码隐藏

public void ReadPhotoInfo()
{
    // Get the current logged in user's ID
    using (SqlConnection con2 = new SqlConnection(strCon1))
    using (SqlCommand cmd = new SqlCommand("SELECT uFileName, PhotoInfo FROM tmpUserPhotos WHERE UserName = '" + CurrUser + "'", con2))
    {
        con2.Open();
        using (SqlDataReader DT2 = cmd.ExecuteReader())
        {
            int X = 1;
            while (DT2.Read())
            {
                igImage[X].src = DT2["uFileName"].ToString();
                igImage[X].alt = DT2["PhotoInfo"].ToString();
                X = X + 1;
            }
        }
    }
}

这显然行不通,但想法是循环遍历 DT2 并将文件名和照片信息字段读取到每个适当的 td 中。

我得到的错误是:Error 92 The name 'igImage' does not exist in the current context

有人可以帮我做这个吗?

【问题讨论】:

  • 只需使用Repeater
  • 如果你想要列而不是行,你能做到吗?
  • 你可以重复任何事情;模板可以是任何你想要的,所以是的。 DataGrid 将要求您拥有一个表并将数据源中的每个项目映射到该表中的一行。这主要是Repeater 的一个特例。

标签: c# html asp.net html-table code-behind


【解决方案1】:

如果将runat="server" 属性添加到img 标记中,则igImage[X].Src = "~/img/pic.jpg"; 等语句将在后面的代码中正常工作。

【讨论】:

  • 还是不喜欢。 :(
猜你喜欢
  • 2012-12-15
  • 2023-03-09
  • 2023-04-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2023-01-09
  • 2012-03-19
相关资源
最近更新 更多