我很想知道是否可以将String.Format
与以下内联标记一起使用:
<asp:RequiredFieldValidator ControlToValidate="tbTest" Text="<%$ Resources:ErrorMsg, E3032 %>" runat="server" />
像这样
<asp:RequiredFieldValidator ControlToValidate="tbTest" Text='<%= String.Format(<%$ Resources:ErrorMsg, E3032 %>, "Other Text to insert")' runat="server" />
我知道你不能那样做,但这只是为了拍照。
TX
答案 0 :(得分:1)
资源绑定语法仅限于接受两个参数:资源容器的名称和资源键。
您可以使用数据绑定
<asp:RequiredFieldValidator
ControlToValidate="tbTest"
Text='<%# String.Format(
Convert.ToString(GetGlobalResourceObject("ErrorMsg", "E3032"),
System.Globalization.CultureInfo.CurrentCulture),
"Other Text to insert") %>'
/>
如果控件不是数据绑定控件的子代,则可能需要调用tbTest.DataBind()。
答案 1 :(得分:1)
我最终为此目的创建了自己的表达式:
web.config:
<expressionBuilders>
<add expressionPrefix="CreerMsgErreur" type="MsgErreurs, App_Code" />
</expressionBuilders>
.aspx:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="tbdDateFin"
CssClass="test" Text="<%$ CreerMsgErreur:E3033, insert, date %>"
ErrorMessage="<%$ CreerMsgErreur:E3033, insert, date %>"
runat="server" EnableClientScript="true" SetFocusOnError="true" />
.vb:
Imports System
Imports System.CodeDom
Imports System.Linq
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.Compilation
Imports System.Web.UI
<ExpressionPrefix("Reflect")> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.LinkDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class MsgErreurs
Inherits ExpressionBuilder
Public Overrides Function GetCodeExpression(ByVal entry As System.Web.UI.BoundPropertyEntry, ByVal parsedData As Object, ByVal context As System.Web.Compilation.ExpressionBuilderContext) As System.CodeDom.CodeExpression
If parsedData IsNot Nothing Then
Return New CodePrimitiveExpression(CType(parsedData, MsgErreursParse).ToString())
End If
Return New CodePrimitiveExpression("Erreur de l'expression")
End Function
Public Overrides Function ParseExpression(ByVal expression As String, ByVal propertyType As System.Type, ByVal context As System.Web.Compilation.ExpressionBuilderContext) As Object
Dim leMsgErreur As MsgErreursParse = Nothing
If Not String.IsNullOrEmpty(expression) Then
leMsgErreur = New MsgErreursParse(expression)
Else
Throw New HttpException(String.Format("Message de l'expression MsgErreur vous devez entrer une expression."))
End If
Return leMsgErreur
End Function
Private Class MsgErreursParse
Private mNoErreur As String
Private mParametres As String
Public Property NoErreur() As String
Get
Return mNoErreur
End Get
Set(ByVal value As String)
mNoErreur = value
End Set
End Property
Public Property Parametres() As String
Get
Return mParametres
End Get
Set(ByVal value As String)
mParametres = value
End Set
End Property
Public Sub New(ByVal parametres As String)
Try
mNoErreur = parametres.Substring(0, parametres.IndexOf(",")).Trim()
mParametres = parametres.Substring(parametres.IndexOf(",") + 1).Trim()
Catch ex As ArgumentOutOfRangeException
mNoErreur = parametres
Catch ex As Exception
End Try
If String.IsNullOrEmpty(mNoErreur) Then
Throw New HttpException(String.Format("Message de l'expression MsgErreur : vous devez spécifier un code d'erreur ou les paramètres ne sont pas dans le bon format : {0}", parametres))
End If
End Sub
Public Sub New(ByVal noErreur As String, ByVal parametres As String)
mNoErreur = noErreur
mParametres = parametres
End Sub
Public Overrides Function ToString() As String
Try
If String.IsNullOrEmpty(Parametres) Then
Return Convert.ToString(HttpContext.GetGlobalResourceObject("MsgErreurs", NoErreur), System.Globalization.CultureInfo.CurrentCulture)
Else
Return String.Format(Convert.ToString(HttpContext.GetGlobalResourceObject("MsgErreurs", NoErreur), System.Globalization.CultureInfo.CurrentCulture), Parametres.Split(",").Select(Function(s) s.Trim()).ToArray())
End If
Catch ex As Exception
Throw New HttpException(String.Format("Message de l'expression MsgErreur : impossible de créer le message d'erreur : NoErreur : {0}, Parametres : {1}", NoErreur, Parametres))
End Try
End Function
End Class
End Class