本文介绍了我的VB代码更新会话购物车时遇到问题。有人能用我的代码帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已将购物车绑定到GridView,并希望更新特定行的数量列。我似乎遇到了更新文本框的问题。我在文本框中添加了一个新值,然后按更新链接,它不会更新。我的订单页面的工作方式是将项目放入会话购物车。这是我遇到问题的购物车页面。 购物车页面GV: < asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataKeyNames =ID GridLines =Horizo​​ntalBorderColor =BlackBorderWidth =2pxBorderStyle =Solid> < columns> < asp:BoundField DataField =IDHeaderText =IDSortExpression =IDReadOnly =trueVisible =false> < HeaderStyle ForeColor =WhiteBackColor =Black/> < asp:BoundField DataField =itemNoHeaderText =itemNoReadOnly =trueSortExpression =itemNoVisible =True> < HeaderStyle ForeColor =WhiteBackColor =Black/> < asp:BoundField DataField =itemHeaderText =itemSortExpression =itemItemStyle-Wrap =trueReadOnly =trueItemStyle-ForeColor =Black> < HeaderStyle Width =300pxBackColor =BlackForeColor =White/> < ItemStyle Wrap =TrueForeColor =Black> < asp:TemplateField HeaderText =qtySortExpression =quantity> < edititemtemplate> < asp:TextBox ID =txtQtyrunat =serverText ='<%#Bind(quantity)%>'> < itemtemplate> < asp:Label ID =lblQtyrunat =serverText ='<%#Bind(quantity)%>'> < HeaderStyle BackColor =BlackForeColor =White/> < asp:BoundField DataField =priceHeaderText =priceSortExpression =priceReadOnly =trueItemStyle-ForeColor =Black> < HeaderStyle BackColor =BlackForeColor =White/> < ItemStyle ForeColor =Black> < asp:CommandField ShowEditButton =True/> 我很感激任何帮助让我的购物车页面按照我的意图工作。提前谢谢。 我的尝试: Dim dt As DataTable Dim dr As DataRow 受保护 Sub Page_Load(发件人作为 对象,e 作为 EventArgs)句柄 我 .Load 如果 不 IsPostBack 那么 dt =会话( 购物车) GridView1.DataSource = dt GridView1.DataBind() Else dt =会话( 购物车) GridView1.DataSource = dt GridView1.DataBind() 如果 GridView1.Rows.Count = 0 然后 lblCart.Visible = True lblCart.Text = 您的购物车是空的,但不一定是。 ' lblTotal.Text =$& GetItemTotal() 结束 如果 结束 如果 结束 Sub 受保护的 Sub GridView1_RowUpdating( ByVal sender 作为 对象, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim dt As DataTable = TryCast (会话( 购物车),DataTable) ' Dim dt As DataTable = Session(cart) ' dt =会话(购物车) Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) ' Dim row As GridViewRow = GridView1.SelectedRow Dim index As 整数 = Convert.ToInt32(e.RowIndex) ' Dim qtb As TextBox = TryCast(arow.FindControl(txtQty),TextBox)[模板字段] Dim qtb 作为 字符串 = TryCast (arow.Cells( 3 )。控件( 0 ),TextBox).Text ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(3).Controls(0) Dim strQty 作为 字符串 = e.NewValues( 3 )。ToString() Dim newQty As Integer = Convert.ToInt32(strQty) ' dt.Rows( index)(quantity)= 10'它在我点击的行中将其更改为10编辑链接05/18/18 ' 如何在文本框中获取新值并将其放在10的位置? ' 如果我那么p一个值在文本框中,它返回到原始值 dt.Rows(index)( 数量)= newQty ' Dim dt As DataTable = TryCast(会话(购物车),DataTable) ' dt。行(索引)(数量)= Integer.Parse(qtb.Text) dt.AcceptChanges()会话( 购物车)= dt GridView1.EditIndex = -1 GridView1.DataSource = dt GridView1.DataBind() Label2.Text = qtb lblCart.Visible = True lblCart.Text = index ' 显示行# ' 在左侧编辑: ' Dim arow As GridViewRow = GridView1.Rows( e.RowIndex) ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex) .Cells(4).Controls(0) ' Dim newQty As Integer = Integer。解析(qtb.Text) ' dt.Rows(e.RowIndex)(数量)= newQty ' dt.AcceptChanges() ' 会话(购物车)= dt ' GridView1.EditIndex = -1 ' GridView1.DataSource = Session(cart) ' GridView1.DataBind() 结束 Sub 受保护的 Sub GridView1_RowEditing(发件人作为 对象,e As GridViewEditEventArgs)句柄 GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataSource = dt GridView1.DataBind() 结束 Sub 受保护的 Sub GridView1_RowCancelingEdit(sender As 对象,e 作为 GridViewCancelEditEvent Args)句柄 GridView1.RowCancelingEdit GridView1.EditIndex = -1 GridView1.DataSource = Session( 购物车) GridView1.DataBind() 结束 Sub 解决方案 & GetItemTotal() 结束 如果 结束 如果 结束 Sub 受保护的 Sub GridView1_RowUpdating( ByVal sender 作为 对象, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim dt As DataTable = TryCast (会话( 购物车),DataTable) ' Dim dt As DataTable = Session(cart) ' dt =会话(购物车) Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) ' Dim row As GridViewRow = GridView1.SelectedRow Dim index As 整数 = Convert.ToInt32(e.RowIndex) ' Dim qtb As TextBox = TryCast(arow.FindControl(txtQty),TextBox)[模板字段] Dim qtb 作为 字符串 = TryCast (arow.Cells( 3 )。控件( 0 ),TextBox).Text ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(3).Controls(0) Dim strQty 作为 字符串 = e.NewValues( 3 )。ToString() Dim newQty As Integer = Convert.ToInt32(strQty) ' dt.Rows( index)(quantity)= 10'它在我点击的行中将其更改为10编辑链接05/18/18 ' 如何在文本框中获取新值并将其放在10的位置? ' 如果我那么p一个值在文本框中,它返回到原始值 dt.Rows(index)( 数量)= newQty ' Dim dt As DataTable = TryCast(会话(购物车),DataTable) ' dt。行(索引)(数量)= Integer.Parse(qtb.Text) dt.AcceptChanges()会话( 购物车)= dt GridView1.EditIndex = -1 GridView1.DataSource = dt GridView1.DataBind() Label2.Text = qtb lblCart.Visible = True lblCart.Text = index ' 显示行# ' 在左侧编辑: ' Dim arow As GridViewRow = GridView1.Rows( e.RowIndex) ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex) .Cells(4).Controls(0) ' Dim newQty As Integer = Integer。解析(qtb.Text) ' dt.Rows(e.RowIndex)(数量)= newQty ' dt.AcceptChanges() ' 会话(购物车)= dt ' GridView1.EditIndex = -1 ' GridView1.DataSource = Session(cart) ' GridView1.DataBind() 结束 Sub 受保护的 Sub GridView1_RowEditing(发件人作为 对象,e As GridViewEditEventArgs)句柄 GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataSource = dt GridView1.DataBind() 结束 Sub 受保护的 Sub GridView1_RowCancelingEdit(sender As 对象,e 作为 GridViewCancelEditEvent Args)句柄 GridView1.RowCancelingEdit GridView1.EditIndex = -1 GridView1.DataSource = Session( 购物车) GridView1.DataBind() 结束 Sub 引用: 如果不是IsPostBack则 dt =会话(购物车) GridView1.DataSource = dt GridView1.DataBind() Else dt =会话(购物车) GridView1.DataSource = dt GridView1.DataBind() ... 每次页面加载时你都会重新绑定网格,而不检查它是否是回发版。 这将覆盖您使用原始值更改的任何值。 仅在 IsPostBack r eturns False 。 I have bound the cart to a GridView and want to update the quantity column of a particular row. I seem to be having a problem with update textbox. I put a new value in the textbox and press update link and it doesn't update. My order pages work in that an item is put in the session cart. It's the cart page I am having problems with. Cart page GV: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" GridLines="Horizontal" BorderColor="Black" BorderWidth="2px" BorderStyle="Solid"> <columns> <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" Visible="false"> <HeaderStyle ForeColor="White" BackColor="Black" /> <asp:BoundField DataField="itemNo" HeaderText="itemNo" ReadOnly="true" SortExpression="itemNo" Visible="True"> <HeaderStyle ForeColor="White" BackColor="Black" /> <asp:BoundField DataField="item" HeaderText="item" SortExpression="item" ItemStyle-Wrap="true" ReadOnly="true" ItemStyle-ForeColor="Black" > <HeaderStyle Width="300px" BackColor="Black" ForeColor="White" /> <ItemStyle Wrap="True" ForeColor="Black"> <asp:TemplateField HeaderText="qty" SortExpression="quantity"> <edititemtemplate> <asp:TextBox ID="txtQty" runat="server" Text='<%# Bind("quantity") %>'> <itemtemplate> <asp:Label ID="lblQty" runat="server" Text='<%# Bind("quantity") %>'> <HeaderStyle BackColor="Black" ForeColor="White" /> <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" ReadOnly="true" ItemStyle-ForeColor="Black" > <HeaderStyle BackColor="Black" ForeColor="White" /><ItemStyle ForeColor="Black"> <asp:CommandField ShowEditButton="True" />I'd appreciate any help in getting my cart page to work as I have intended. Thank you in advance.What I have tried:Dim dt As DataTable Dim dr As DataRow Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not IsPostBack Then dt = Session("Cart") GridView1.DataSource = dt GridView1.DataBind() Else dt = Session("Cart") GridView1.DataSource = dt GridView1.DataBind() If GridView1.Rows.Count = 0 Then lblCart.Visible = True lblCart.Text = "Your cart is empty, but it doesn't have to be." ' lblTotal.Text = "$" & GetItemTotal() End If End If End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim dt As DataTable = TryCast(Session("Cart"), DataTable) ' Dim dt As DataTable = Session("cart") ' dt = Session("Cart") Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) ' Dim row As GridViewRow = GridView1.SelectedRow Dim index As Integer = Convert.ToInt32(e.RowIndex) 'Dim qtb As TextBox = TryCast(arow.FindControl("txtQty"), TextBox) [template field] Dim qtb As String = TryCast(arow.Cells(3).Controls(0), TextBox).Text ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(3).Controls(0) Dim strQty As String = e.NewValues(3).ToString() Dim newQty As Integer = Convert.ToInt32(strQty) ' dt.Rows(index)("quantity") = 10 ' it changes it to "10" in the row I clicked edit link 05/18/18 ' how do I get the new value in the textbox and put it where the "10" is? 'If I Then put a value In the textbox, it goes back To original value dt.Rows(index)("quantity") = newQty ' Dim dt As DataTable = TryCast(Session("Cart"), DataTable) ' dt.Rows(index)("quantity") = Integer.Parse(qtb.Text) dt.AcceptChanges() Session("Cart") = dt GridView1.EditIndex = -1 GridView1.DataSource = dt GridView1.DataBind() Label2.Text = qtb lblCart.Visible = True lblCart.Text = index ' shows row # ' edit on left side: ' Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) 'Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(4).Controls(0) 'Dim newQty As Integer = Integer.Parse(qtb.Text) 'dt.Rows(e.RowIndex)("quantity") = newQty 'dt.AcceptChanges() 'Session("cart") = dt 'GridView1.EditIndex = -1 'GridView1.DataSource = Session("cart") 'GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataSource = dt GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 GridView1.DataSource = Session("Cart") GridView1.DataBind() End Sub 解决方案 " & GetItemTotal() End If End If End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim dt As DataTable = TryCast(Session("Cart"), DataTable) ' Dim dt As DataTable = Session("cart") ' dt = Session("Cart") Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) ' Dim row As GridViewRow = GridView1.SelectedRow Dim index As Integer = Convert.ToInt32(e.RowIndex) 'Dim qtb As TextBox = TryCast(arow.FindControl("txtQty"), TextBox) [template field] Dim qtb As String = TryCast(arow.Cells(3).Controls(0), TextBox).Text ' Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(3).Controls(0) Dim strQty As String = e.NewValues(3).ToString() Dim newQty As Integer = Convert.ToInt32(strQty) ' dt.Rows(index)("quantity") = 10 ' it changes it to "10" in the row I clicked edit link 05/18/18 ' how do I get the new value in the textbox and put it where the "10" is? 'If I Then put a value In the textbox, it goes back To original value dt.Rows(index)("quantity") = newQty ' Dim dt As DataTable = TryCast(Session("Cart"), DataTable) ' dt.Rows(index)("quantity") = Integer.Parse(qtb.Text) dt.AcceptChanges() Session("Cart") = dt GridView1.EditIndex = -1 GridView1.DataSource = dt GridView1.DataBind() Label2.Text = qtb lblCart.Visible = True lblCart.Text = index ' shows row # ' edit on left side: ' Dim arow As GridViewRow = GridView1.Rows(e.RowIndex) 'Dim qtb As TextBox = GridView1.Rows(e.RowIndex).Cells(4).Controls(0) 'Dim newQty As Integer = Integer.Parse(qtb.Text) 'dt.Rows(e.RowIndex)("quantity") = newQty 'dt.AcceptChanges() 'Session("cart") = dt 'GridView1.EditIndex = -1 'GridView1.DataSource = Session("cart") 'GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex GridView1.DataSource = dt GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 GridView1.DataSource = Session("Cart") GridView1.DataBind() End SubQuote: If Not IsPostBack Then dt = Session("Cart") GridView1.DataSource = dt GridView1.DataBind()Else dt = Session("Cart") GridView1.DataSource = dt GridView1.DataBind() ...You are re-binding the grid every time the page loads, without checking whether or not it's a post-back.That will overwrite any values you have changed with the original values.Only bind the grid if IsPostBack returns False. 这篇关于我的VB代码更新会话购物车时遇到问题。有人能用我的代码帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 23:53