c# - 如何在 ASP.NET 中将 DataFormatString 设置为 Textbox?

标签 c# asp.net

我正在使用Datagrid并且我已经绑定(bind)了列,如下所示

<asp:BoundColumn DataField="Salary" HeaderText="Salary" DataFormatString="{0:#,##0.00}">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>

我想将上面的数据格式字符串用于下面的TextBox

<asp:TextBox  DataFormatString="{0:#,##0.00}" ID="textbox1" runat="server" MaxLength="150"></asp:TextBox>

如果我尝试将 Dataformatstring 设置为 TextBox 它不起作用。如何将 dataformatstring 添加到 ASP.NET 或 Javascript 代码中的 TextBox 中以解决相同问题?

最佳答案

如果你在代码后面设置了TextBox.Text值,你可以这样做:

textBox1.Text = string.Format("{0:#,##0.00}", yourValue);

查看dotnetfiddle

如果你在aspx端设置,你可以使用Eval:

<asp:TextBox  Text='<%# Eval("myField", "{0:#,##0.00}") %>' ID="textbox1" runat="server" MaxLength="150"></asp:TextBox> 

关于c# - 如何在 ASP.NET 中将 DataFormatString 设置为 Textbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004595/

相关文章:

c# - 使用套接字与 MonoDroid 传输数据

c# - 将数据传递到现有表单

c# - 相当于 LINQ 的 Select 命令的 Powershell?

c# - 如何从 ssis 包脚本中的 dts 配置文件获取 sql 连接

asp.net - 如何(使用服务器端代码)检测 ASP.NET 页面是否即将被缓存(第一次请求时)?

c# - 在构造函数中手动添加 Application Start 事件处理程序

c# - 如何在不重新编译的情况下将键/值添加到语言 (*.resx) 文件?

c# - 数据表和 View 状态

asp.net - 如何使用 Membership ValidatePassword 事件处理程序?

asp.net - 为什么事件代码:4011显示为信息