当值源自查询字符串时格式化 asp.net 标签

发布于 2024-09-26 22:35:49 字数 310 浏览 2 评论 0原文

下午都。

今天厚厚的里奇为您提供了一个非常简单的方法。

我有一个标签,我想以可爱的数字格式显示,即 {0:N0}

现在,此标签文本相当于查询字符串值。

如何根据查询字符串值一次性格式化标签文本?

我尝试过这个

lblTotalPurchQS.Text = String.Format("{0:N0}",Request.QueryString["totalpurchasequantity"].ToString());

但收效甚微。

有什么想法或指示吗?

Afternoon all.

A very simple one for you today from thicky Rich.

I have a label I want to display as a lovely number format i.e. {0:N0}

Now, this label text equates to a query string value.

How do I go about formatting a label's text from a query string value in one fell swoop?

I have tried this

lblTotalPurchQS.Text = String.Format("{0:N0}",Request.QueryString["totalpurchasequantity"].ToString());

but with little success.

Any ideas or pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-10-03 22:35:49

不要在传入的查询字符串参数上使用 ToString,而是先将其转换为 int

lblTotalPurchQS.Text = String.Format("{0:N0}", int.Parse(Request.QueryString["totalpurchasequantity"]));

注意:

以上不是安全代码。首先,转换可能会因转换异常而失败。您还应该对输出进行 HTML 转义,以防出现 XSS。

这是更好的:

int totalPurchaseQuantity;

if(int.TryParse(Request.QueryString["totalpurchasequantity"], out totalPurchaseQuantity))
{
    lblTotalPurchQS.Text = Server.HtmlEncode(String.Format("{0:N0}", totalPurchaseQuantity);
}

Don't use ToString on the incoming query string parameter, but convert it to an int first:

lblTotalPurchQS.Text = String.Format("{0:N0}", int.Parse(Request.QueryString["totalpurchasequantity"]));

Note:

The above is not safe code. First, the conversion may fail with a conversion exception. You should also be HTML escaping the output, in case of XSS.

This is better:

int totalPurchaseQuantity;

if(int.TryParse(Request.QueryString["totalpurchasequantity"], out totalPurchaseQuantity))
{
    lblTotalPurchQS.Text = Server.HtmlEncode(String.Format("{0:N0}", totalPurchaseQuantity);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文