服务器端的ASP.Net字符串
问题描述:
我在服务器端有一个字符串。我想将这个字符串绑定到文本框的文本propery或图像的imageurl属性。服务器端的ASP.Net字符串
服务器端
public string image_path = "@/tool_images/10000000s0.png";
我在客户端尝试这些
<asp:Image ID="t10000000" runat="server" ImageUrl='<%=image_path %>' />
<asp:TextBox ID="ASPTextBox1" runat="server" Text='<%= image_path %>' />
<asp:TextBox ID="TextBox1" runat="server" Text="<%# image_path %>" />
它不能正常工作。
为 '<%=%>' 输出为<%= IMAGE_PATH%>
为 '<%#%>' 输出什么
如果输出是真的,我想这IMAGEURL属性格式绑定
答
<%# %>
是结合数据控制,所以你需要调用每个控件的DataBind方法。
<asp:Image ID="t10000000" runat="server" ImageUrl='<%# image_path %>' />
<asp:TextBox ID="ASPTextBox1" runat="server" Text='<%# image_path %>' />
<asp:TextBox ID="TextBox1" runat="server" Text="<%# image_path %>" />
// Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
t10000000.DataBind();
ASPTextBox1.DataBind();
TextBox1.DataBind();
}
}
FYI:在ASP.Net Web窗体,我们通常不会使用这种方法将数据绑定到控件,除非这些控件是在像GridView控件,ListView控件或转发数据的控制。换句话说,我们只是在代码后面分配值,如t10000000.ImageUrl = image_path;
+0
谢谢你的工作。但我的主要目的是根据 id动态更改图片URL。 – BK52
答
使用三元运算符。 让我知道它是否适合你。
booleanExpression? trueValue:falseValue
– MMK
这些图像和文本框位于GridView或其他数据控件内部吗? – Win
@Win号正好在div – BK52