Как сохранить текст в формате валюты вне текстового поля столбца цены при обновлении цены в веб-формах ASPX? - PullRequest
0 голосов
/ 26 августа 2018

В моем 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» 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 = decimal.TryParse (box1.Text, out Price); var PriceString = GridView1.Rows [index] .Cells [4] .Text.Replace ("AUD", ""); ] ... Я не могу обновить цену товара.

Было бы полезно, если указано рекомендуемое синтаксическое решение.

1 Ответ

0 голосов
/ 26 августа 2018

Мне наконец-то удалось это решить.Все, что мне нужно было сделать, это добавить ярлык.

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...