【问题标题】:How to keep currency format text outside the textbox of a Price Column while updating the Price in ASPX Web Forms?如何在更新 ASPX Web 表单中的价格时将货币格式文本保留在价格列的文本框之外?
【发布时间】:2018-08-26 13:22:13
【问题描述】:

在我的 UpdateProducts.aspx 中,这就是我所做的:

<asp:TemplateField HeaderText="Price" >
         <ItemTemplate>
          <asp:TextBox ID ="TextBox1" runat="server" DataField="Product_Price" Text='<%#string.Format("{0:0.00} AUD",Eval("Product_Price"))%>'/>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numbers with decimals only" ControlToValidate="TextBox1" ValidationExpression="^\d{1,9}\.\d{1,2}$"></asp:RegularExpressionValidator>
          <asp:Button ID ="Button7" runat="server" OnClick="Price_Update_Click" CommandArgument="Button7" CommandName="Update"  Text="Update" />
          </ItemTemplate>
          </asp:TemplateField>

当我使用上面的代码运行 aspx 页面时,一切正常。唯一的小问题是“AUD”甚至包含在 Product_Price 列的文本框值中。换句话说,Price_Column 值在我不想要的文本框中显示为“65.55 AUD”。 但我想要的是 AUD 显示在文本框之外(但不在文本框内)。 换言之,Product_Price 列的文本框值应显示为“65.55”澳元

为了实现它,这是我在UpdateProducts.cs中尝试过的

protected void Price_Update_Click(object sender, EventArgs e)
        {
            GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
            int index = gvr.RowIndex;
            TextBox box1 = (TextBox)GridView1.Rows[index].Cells[4].FindControl("TextBox1");
            decimal Price;
            bool prc = decimal.TryParse(box1.Text, out Price);
            var PriceString = GridView1.Rows[index].Cells[4].Text.Replace(" AUD", "");

            Button btn = (Button)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            string ProductNo = row.Cells[0].Text;

if (Price > 00.00m)
            {
                string CS;
                CS = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
                SqlConnection con = new SqlConnection(CS);
                SqlCommand cmd = new SqlCommand("UpdateProductPrice", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                con.Open();
                cmd.Parameters.AddWithValue("@ProductPrice", Price);
                cmd.Parameters.AddWithValue("@ProductNo", ProductNo);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox("Price has been updated");
                DisplayProducts();
            }

            else if (Price == 00.00m || prc == false)
            {
                Label5.Text = "Please don't keep the price blank";
                DisplayProducts();
            }

        }

即使在使用 [ 十进制价格; bool prc = decimal.TryParse(box1.Text, out Price); var PriceString = GridView1.Rows[index].Cells[4].Text.Replace("AUD", ""); ] ...我无法更新产品价格。

如果提供推荐的语法解决方案将会很有帮助。

【问题讨论】:

  • 请注意,SQL Server 中 Product_Price 列的数据类型是 MONEY。

标签: c# aspxgridview


【解决方案1】:

我终于设法解决了。 我所要做的就是添加一个标签。

<asp:TemplateField HeaderText="Price" >
         <ItemTemplate>
          <asp:TextBox ID ="TextBox1" runat="server" DataField="Product_Price" Text='<%#string.Format("{0:0.00}",Eval("Product_Price"))%>'/>
          <asp:Label ID="Label6" Text="AUD" runat="server"></asp:Label>
          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numbers with decimals only" ControlToValidate="TextBox1" ValidationExpression="^\d{1,9}\.\d{1,2}$"></asp:RegularExpressionValidator>
          <asp:Button ID ="Button7" runat="server" OnClick="Price_Update_Click" CommandArgument="Button7" CommandName="Update"  Text="Update" />
          </ItemTemplate>
          </asp:TemplateField>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多