如何使用jquery获取gridview的单元格值 [英] how to get the cell value of gridview using jquery
本文介绍了如何使用jquery获取gridview的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i有3列价格,数量和总价格在gridview
i有问题,当我保存所有价值的gridview价格和数量保存得当但总计价格它只保存total_price列的最后一行值
低于我的代码
jquery
< script type = text / javascript >
$(function(){
$( [id * = txtQuantity])。val( 0);
});
$( [id * = txtQuantity])。live( 更改,function(){
if (isNaN(parseInt($( this )。val()))){
$( this ).val(' 0');
} else {
$( this )。val(parseInt($(这个)。val())。toString());
}
});
$( [id * = txtQuantity])。live( keyup,function(){
if (!jQuery.trim($( this )。val())== ' '){
if (!isNaN(parseFloat)($( this )。val()))){
var row = $( this )。nearest( tr);
$( [id * = lblTotal],row).html(parseFloat($( 。price,row).html())* parseFloat($(此跨度>)VAL()))。
var row = $( this )。nearest( 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 跨度>);
}
} 其他 {
var row = $(< span class =code-keyword> this )。nearest( 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 = 排序 角色 = columnheader TabIndex = 0 aria-controls = example2
< span class =code-attribute> aria-label = :激活以对列升序进行排序 CssClass = table table-striped dataTable
< span class =code-attribute> < span class =code-attribute> aria-describedby = example2_info runat = < span class =code-keyword> server AutoGenerateColumns = false >
< 列 >
< asp:TemplateField HeaderText = 产品 >
< ItemTemplate >
< asp:标签 ID = lblProduct runat = server 文本 =' <% #Eval( Product_Name)%> ' > '> < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = ProductID 可见 = false >
< ItemTemplate >
< asp:标签 ID = lblProduct_Id runat = server 文本 =' <% #Eval( Product_Id)%> ' > '> < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< asp: BoundField DataField = Unit_Price HeaderText = 价格 ItemStyle-CssClass = price < span class =code-attribute> / >
< asp:TemplateField < span class =code-attribute> HeaderText = 数量 >
< ItemTemplate >
< asp:TextBox ID = txtQuantity runat = server 文本 =' <% #Eval( ProdQuantity)%> ' > '> < / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 总计 >
< ItemTemplate >
< asp:标签 ID = lblTotal runat = server 文字 < span class =code-keyword> = 0 > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >
< asp:HiddenField ID =MyHiddenrunat =server/>
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);
标签价格=(标签)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 =
插入Order_Table(Product_Id,Quantity,Total_Price)值(@产品,@ Quanty,@ Total_Price);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add( @ Product,SqlDbType.Char, 20 , Product_Id)。Value = ProductID。文本;
cmd.Parameters.AddWithValue( @ Quanty,Quanty.Text);
cmd.Parameters.AddWithValue( @ Total_Price,a);
cmd.ExecuteNonQuery();
}
}
解决方案
(function(){
( [id * = txtQuantity])。val(< span class =code-string> 0);
});
( [id * = txtQuantity])。live( 更改,function(){
if (isNaN(parseInt) (
hi,
i have 3 column Price,quantity,and totalPrice in gridview
i have issue when i save all value of gridview price and quantity are save properly but for total price it save only last row value of total_price column
below my code
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 ID="MyHidden" runat="server" />
C# code
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();
}
}
解决方案
(function () {
("[id*=txtQuantity]").val("0"); });
("[id*=txtQuantity]").live("change", function () { if (isNaN(parseInt(
这篇关于如何使用jquery获取gridview的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文