如何在更新ASPX Web窗体中的价格时将货币格式的文本保留在“价格”列的文本框之外?

时间:2018-08-26 13:22:14

标签: c# aspxgridview

在我的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页面时,一切运行正常。唯一的小问题是,Product_Price列的文本框中的值甚至包括“ AUD”。换句话说,Price_Column值在我不需要的文本框中显示为“ 65.55 AUD”。 但是我想要的是在文本框旁边(但不在文本框内)显示AUD。 换句话说,Product_Price列的文本框值应显示为“ 65.55” AUD

为了实现它,这就是我在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 =十进制.TryParse(box1.Text,out Price);             var PriceString = GridView1.Rows [index] .Cells [4] .Text.Replace(“ AUD”,“”); ] ...我无法更新产品价格。

如果提供了建议的语法解决方案,这将很有帮助。

1 个答案:

答案 0 :(得分:0)

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

<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>