Asp.net 在aspx中有条件地设置HTML5占位符属性
我有一个带有服务器控制文本框的Asp.net 在aspx中有条件地设置HTML5占位符属性,asp.net,Asp.net,我有一个带有服务器控制文本框的aspx <asp:TextBox placeholder="myPlaceholder" ID="myID" runat="server" /> 我想有条件地设置HTML5属性占位符 我尝试了这个,但没有成功,因为服务器控件不能有 受WPF知识的启发,我在黑暗中拍摄了这张照片 <asp:TextBox ID="sttxBillToVatNumber" MaxLength="10" runat="server"> <%
aspx
<asp:TextBox placeholder="myPlaceholder" ID="myID" runat="server" />
我想有条件地设置HTML5属性占位符
我尝试了这个,但没有成功,因为服务器控件不能有
受WPF知识的启发,我在黑暗中拍摄了这张照片
<asp:TextBox ID="sttxBillToVatNumber" MaxLength="10" runat="server">
<% if (this.ShowVatNumberCodePlaceholder)
{
%>
placeholder="myPlaceholder"
<%
}
%>
</asp:TextBox>
占位符=“我的占位符”
从我的阅读资料来看,我想我只能在服务器端做到这一点——我做到了,而且效果很好
但我宁愿在aspx中使用此逻辑,以避免将表示逻辑分散到两个文件中
在
aspx
本身中有什么方法可以实现这一点吗?尝试使用JQuery关联占位符。比如:
$(function () {
<% if (this.ShowVatNumberCodePlaceholder)
{
$("#<%sttxBillToVatNumber.ClientID"%>").attr("placeholder","myPlaceholder");
}
%>
});
$(函数(){
属性(“占位符”、“我的占位符”);
}
%>
});
您可以通过三元运算符和数据绑定调用的组合来实现这一点,如下所示
加价
<asp:TextBox ID="sttxBillToVatNumber" runat="server"
MaxLength="10"
placeholder='<%# this.ShowVatNumberCodePlaceholder ? "my_placeholder" : "my_other_placeholder" %>'>
</asp:TextBox>
尝试使用JQuery“).attr(“占位符”、“我的占位符”);}%>
<asp:TextBox ID="sttxBillToVatNumber" runat="server"
MaxLength="10"
placeholder='<%# this.ShowVatNumberCodePlaceholder ? "my_placeholder" : "my_other_placeholder" %>'>
</asp:TextBox>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sttxBillToVatNumber.DataBind();
}
}