【发布时间】:2016-05-27 09:14:57
【问题描述】:
我一直在尝试弄清楚如何执行此操作,因为我创建的图像按钮的 ID ('titanBtn') 在我的代码隐藏文件中无法识别。这将允许我简单地编写 'titanBtn.ImageUrl =" 但唉,后面的代码说它不存在。
我试图用这段代码做的只是从 Db 中读取一个字符串,它指定图像的文件名(适当的图像以每个对象的元素类型命名),并将其放在一个连接的指定 ImageUrl 的字符串。
<asp:Repeater ID="titanRptr" runat="server">
<ItemTemplate>
<table>
<tr class="tr1">
<td><%# Eval("TitanName") %></td>
<td>
<asp:ImageButton ID="titanBtn" runat="server" Width="100px" Height="100px" OnClick="titanBtn_Click" />
</td>
<td>Level: <%# Eval("char_lvl")%>
<br />
Step: <%# Eval("step")%>
<br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
后面的代码
using (SqlCommand cmd2 = new SqlCommand("getElement", conn))
{
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@uid", titanElement);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd2))
{
SqlDataReader reader;
conn.Open();
reader = cmd2.ExecuteReader();
while (reader.Read())
{
titanImage = Convert.ToString(reader["Element"]);
titanBtn.ImageUrl = "images/" + titanImage+ ".gif"; //"The name 'titanBtn' does not exist in the current context"
titanRptr
}
conn.Close();
}
}
【问题讨论】:
标签: c# asp.net code-behind