asp.net数据更新问题 20
我用了listview控件绑定了sqldatasource,用label显现总价格.但每次更新后,label都没有正确显示.情况是这样的产品价格数量总价更新按钮1111更...
我用了listview控件绑定了sqldatasource,用label显现总价格.但每次更新后,label都没有正确显示.
情况是这样的
产品 价格 数量 总价
更新按钮 1 1 1 1
更新按钮 2 1 1 1
label显示:2
当把产品1的数量改为2,按了更新按钮,产品1的数量和总价都变为2,但label还是显示2,需要再按一次更新按钮,label才会变为3.要如何修改才能同步显示正确?
源代码:
aspx:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:oskbodykitConnectionString1 %>"
SelectCommand="SELECT orderdetail.quantity, orderdetail.price, product.productname, orderdetail.quantity * orderdetail.price AS total FROM orderdetail INNER JOIN product ON orderdetail.product_id = product.id WHERE (orderdetail.customer = @customer) AND (orderdetail.order_id = @order_id)"
UpdateCommand="UPDATE [orderdetail] SET [quantity] = @quantity WHERE [id] = @id">
<SelectParameters>
<asp:SessionParameter Name="customer" SessionField="username" />
<asp:Parameter DefaultValue="0" Name="order_id" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource2" >
<LayoutTemplate>
<table id="Table1" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1" >
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #666666;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
</td>
<td>
<td>
<asp:Label ID="productnameLabel" runat="server" Text='<%# Eval("productname") %>' />
</td>
<td>
<span style="color: #FF0000">$<asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' /></span>
</td>
<td>
<asp:textbox ID="quantityLabel" runat="server" Text='<%# Bind("quantity") %>' />
</td>
<td>
<span style="color: #FF0000">$<asp:Label ID="totalLabel" runat="server" Text='<%# Eval("total") %>' /></span>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
aspx.cs
public partial class test : System.Web.UI.Page
{
protected int sum;
protected void Page_Load(object sender, EventArgs e)
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
试了一,二楼的方法,还是不成功 展开
情况是这样的
产品 价格 数量 总价
更新按钮 1 1 1 1
更新按钮 2 1 1 1
label显示:2
当把产品1的数量改为2,按了更新按钮,产品1的数量和总价都变为2,但label还是显示2,需要再按一次更新按钮,label才会变为3.要如何修改才能同步显示正确?
源代码:
aspx:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:oskbodykitConnectionString1 %>"
SelectCommand="SELECT orderdetail.quantity, orderdetail.price, product.productname, orderdetail.quantity * orderdetail.price AS total FROM orderdetail INNER JOIN product ON orderdetail.product_id = product.id WHERE (orderdetail.customer = @customer) AND (orderdetail.order_id = @order_id)"
UpdateCommand="UPDATE [orderdetail] SET [quantity] = @quantity WHERE [id] = @id">
<SelectParameters>
<asp:SessionParameter Name="customer" SessionField="username" />
<asp:Parameter DefaultValue="0" Name="order_id" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource2" >
<LayoutTemplate>
<table id="Table1" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1" >
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #666666;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
</td>
<td>
<td>
<asp:Label ID="productnameLabel" runat="server" Text='<%# Eval("productname") %>' />
</td>
<td>
<span style="color: #FF0000">$<asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' /></span>
</td>
<td>
<asp:textbox ID="quantityLabel" runat="server" Text='<%# Bind("quantity") %>' />
</td>
<td>
<span style="color: #FF0000">$<asp:Label ID="totalLabel" runat="server" Text='<%# Eval("total") %>' /></span>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
aspx.cs
public partial class test : System.Web.UI.Page
{
protected int sum;
protected void Page_Load(object sender, EventArgs e)
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
试了一,二楼的方法,还是不成功 展开
5个回答
展开全部
很明显是绑定问题,你把查询和更新写成两个方法,在你的click事件中分别调用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
这样改改 试试
{
if(!IsPostBack)
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
这样改改 试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当你第一次点击更新按钮时,你要注意同步更新数据源里的数据,你可以再试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前台:
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
改为
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" onclick="Button1_Click" Text="Update" />
后台:
public partial class test : System.Web.UI.Page
{
protected int sum;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
public void bind()
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
bind();
}
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
改为
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" onclick="Button1_Click" Text="Update" />
后台:
public partial class test : System.Web.UI.Page
{
protected int sum;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
public void bind()
{
DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable dt = dv.ToTable();
sum = 0;
for (int i = 0; i < dt.Rows.Count;i++ )
{
string temp = dt.Rows[i]["total"].ToString();
sum = sum + int.Parse(temp);
}
Label1.Text = sum.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
bind();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询