Rajnish D mishra Ответов: 1

как получить значение ячейки gridview с помощью jquery


привет,
у меня есть 3 столбца цена, количество и общая цена в gridview
у меня есть проблема, когда я сохраняю все значения GridView price и quantity сохраняются правильно, но для общей цены он сохраняет только последнее значение строки столбца total_price

ниже моего кода


в jQuery
<script type="text/javascript">
        $(function () {
            $("[id*=txtQuantity]").val("0");
        });
        $("[id*=txtQuantity]").live("change", function () {
            if (isNaN(parseInt($(this).val()))) {
                $(this).val('0');
            } else {
                $(this).val(parseInt($(this).val()).toString());
            }
        });
        $("[id*=txtQuantity]").live("keyup", function () {
            if (!jQuery.trim($(this).val()) == '') {
                if (!isNaN(parseFloat($(this).val()))) {

                  
                    var row = $(this).closest("tr");
                    $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
                    var row = $(this).closest("tr");
                    $("input:hidden[id*=MyHidden]").val(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
                
                
                }
                else {
                    var row = $(this).closest("tr");
                    $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * 0);
                }
            } else {
                var row = $(this).closest("tr");
                $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * 0);
                $(this).val('');
            }
            var grandTotal = 0;
            $("[id*=lblTotal]").each(function () {
                grandTotal = grandTotal + parseFloat($(this).html());
            });
            $("[id*=lblGrandTotal]").html(grandTotal.toString());
        });
    </script>



Asp.net

<asp:GridView ID="GridView1" class="sorting" role="columnheader" TabIndex="0" aria-controls="example2"

                                    aria-label=": activate to sort column ascending" CssClass="table table-striped dataTable"

                                    aria-describedby="example2_info" runat="server" AutoGenerateColumns="false">
                                    <Columns>


                                         <asp:TemplateField HeaderText="Product">
                                            <ItemTemplate>
                                                <asp:Label ID="lblProduct" runat="server" Text='<%# Eval("Product_Name") %>'>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                         <asp:TemplateField HeaderText="ProductID" Visible="false">
                                            <ItemTemplate>
                                                <asp:Label ID="lblProduct_Id" runat="server" Text='<%# Eval("Product_Id") %>'>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="Unit_Price" HeaderText="Price" ItemStyle-CssClass="price" />

                                        <asp:TemplateField HeaderText="Quantity">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("ProdQuantity") %>'>' ></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Total">
                                            <ItemTemplate>

                                             <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>




&ЛТ;как ASP:HiddenField идентификатор="MyHidden" атрибут runat="сервер" /&ГТ;

Код C#
foreach (GridViewRow row in GridView1.Rows)
      {


          if (row.RowType == DataControlRowType.DataRow)
          {



              Label Product = (Label)row.FindControl("lblProduct");
              Label ProductID = (Label)row.FindControl("lblProduct_Id");

              Label price = (Label)row.FindControl("Unit_Price");
              TextBox Quanty = (TextBox)row.FindControl("txtQuantity");


              string a = MyHidden.Value;

              SqlConnection con = new SqlConnection(StrConnection);
              con.Open();
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.CommandType = CommandType.Text;
              cmd.CommandText =
                  "insert into Order_Table(Product_Id,Quantity,Total_Price ) values(@Product,@Quanty,@Total_Price) ";

              cmd.CommandType = System.Data.CommandType.Text;
              cmd.Parameters.Add("@Product", SqlDbType.Char, 20, "Product_Id").Value = ProductID.Text;
              cmd.Parameters.AddWithValue("@Quanty", Quanty.Text);
              cmd.Parameters.AddWithValue("@Total_Price", a);


              cmd.ExecuteNonQuery();

          }
      }

1 Ответов

Рейтинг:
0

Anurag Gandhi

Я ясно вижу, что вы не принимаете значение Total_Price для этой строки.

Он должен использовать:

row.FindControl("YOUR_TOTAL_PRICE_FIELD");

Как и в других областях.


Rajnish D mishra

afeter u r guidance i принимает значение total_price в строке, но этого не может произойти