带表达式值的ASP.NET自定义表达式 [英] ASP.NET Custom Expression with expression values
本文介绍了带表达式值的ASP.NET自定义表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何动态地将值添加到$ expression?
例如,我想获取用户在两个不同文本框中输入的2个值,并将它们添加为RandomNumber的值(我自己创建的$ Expression)
< asp:Literal ID =" Literal1" RUNAT = QUOT;服务器" Text ="<%$ RandomNumber:<%= Num1%>,<%= Num2%>%>" />
public string Num1
{
get
{
return TextBox1.Text;
}
}
公共字符串Num2
{
get
{
return TextBox2.Text;
}
}
编辑:我也尝试将属性设置为int但仍然不会进行替换...
public class RandomNumberExpressionBuilder:System.Web .Compilation.ExpressionBuilder
{
public static string GetRandomNumber(int lowerLimit,int higherLimit)
{
Random random = new Random();
int no = random.Next(lowerLimit,higherLimit);
返回no.ToString();
}
公共覆盖CodeExpression GetCodeExpression(BoundPropertyEntry条目,对象parsedData,ExpressionBuilderContext上下文)
{
if(!entry.Expression.Contains(","))
抛出新的ArgumentException("必须通过昏迷分隔");
else
{
string [] entryData = entry.Expression.Split(',');
if(entryData.Length!= 2)
{
抛出新的ArgumentException(" mast provide 2 argument");
}
else
{
int lowerLimit,higherLimit;
if(Int32.TryParse(entryData [0],out lowerLimit)&& Int32.TryParse(entryData [1],out higherLimit))
{
CodeTypeReferenceExpression typeRef = new CodeTypeReferenceExpression( this.GetType());
CodeExpression [] parameters = new CodeExpression [2];
parameters [0] = new CodePrimitiveExpression(lowerLimit);
parameters [1] = new CodePrimitiveExpression(higherLimit);
返回新的CodeMethodInvokeExpression(typeRef," GetRandomNumber",参数);
}
else
{
抛出新的ArgumentException(" use valid integers");
}
}
}
}
}
< form id =" form1" RUNAT = QUOT;服务器">
< div>
< asp:Literal ID =" Literal1" RUNAT = QUOT;服务器" Text ="<%$ RandomNumber:<%= Num1%>,<%= Num2%>%>" />
< asp:Button ID =" Button1" RUNAT = QUOT;服务器"文本= QUOT;按钮和QUOT; />
< br />
来自< asp:TextBox ID =" TextBox1" RUNAT = QUOT;服务器"宽度= QUOT; 53px">< / ASP:文本框>
到< asp:TextBox ID =" TextBox2" RUNAT = QUOT;服务器"宽度= QUOT; 70像素">< / ASP:文本框>
< / div>
< p>
& nbsp;< / p>
< / form>
解决方案
< blockquote>如果要添加值,它们必须是数字,而不是字符串。您可以使用
int.Parse 或
int.TryParse 从字符串中获取数字。
How can I dynamically add values to a $ expression??
For example I want to take 2 values that a user entered in two different text boxes and add them as values for the RandomNumber (a $Expression that i have created myself)
<asp:Literal ID="Literal1" runat="server" Text="<%$ RandomNumber: <%= Num1%>, <%= Num2%>%>" />
public string Num1 { get { return TextBox1.Text; } } public string Num2 { get { return TextBox2.Text; } }
EDIT: I have also tried to set the property as int but still won't make the substitution...
public class RandomNumberExpressionBuilder : System.Web.Compilation.ExpressionBuilder { public static string GetRandomNumber(int lowerLimit, int higherLimit) { Random random = new Random(); int no = random.Next(lowerLimit, higherLimit); return no.ToString(); } public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { if (!entry.Expression.Contains(",")) throw new ArgumentException("must be separated through coma"); else { string[] entryData = entry.Expression.Split(','); if (entryData.Length != 2) { throw new ArgumentException("mast provide 2 argument"); } else { int lowerLimit, higherLimit; if (Int32.TryParse(entryData[0], out lowerLimit) && Int32.TryParse(entryData[1], out higherLimit)) { CodeTypeReferenceExpression typeRef = new CodeTypeReferenceExpression(this.GetType()); CodeExpression[] parameters = new CodeExpression[2]; parameters[0] = new CodePrimitiveExpression(lowerLimit); parameters[1] = new CodePrimitiveExpression(higherLimit); return new CodeMethodInvokeExpression(typeRef, "GetRandomNumber", parameters); } else { throw new ArgumentException("use valid integers"); } } } } }
<form id="form1" runat="server"> <div> <asp:Literal ID="Literal1" runat="server" Text="<%$ RandomNumber: <%= Num1%>, <%= Num2%>%>" /> <asp:Button ID="Button1" runat="server" Text="Button" /> <br /> from<asp:TextBox ID="TextBox1" runat="server" Width="53px"></asp:TextBox> to<asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox> </div> <p> </p> </form>
解决方案
If you want to add values, they must be numeric, not string. You can use int.Parse or int.TryParse to get a number from a string.
这篇关于带表达式值的ASP.NET自定义表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文