ItemCommand not getting the correct value in DataGrid

I am trying to get the value of a DataGrid and using an ItemCommand update the value in the DataTable behind it.

I have this working but it sends the OLD value of the txQTY textbox.

Can someone help?

DATAGRID
<asp:DataGrid id="dg" runat="server" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="1px" BorderColor="#3366CC" BackColor="White" CellPadding="4" OnDeleteCommand="dg_DeleteCommand" OnItemCommand="dg_ItemCommand">

                <FooterStyle forecolor="#003399" backcolor="#99CCCC"></FooterStyle>

                <HeaderStyle font-bold="True" forecolor="#CCCCFF" backcolor="#003399"></HeaderStyle>

                <PagerStyle horizontalalign="Left" forecolor="#003399" backcolor="#99CCCC" mode="NumericPages"></PagerStyle>

                <SelectedItemStyle font-bold="True" forecolor="#CCFF99" backcolor="#009999"></SelectedItemStyle>

                <ItemStyle forecolor="#003399" backcolor="White"></ItemStyle>

                <Columns>

                    <asp:TemplateColumn HeaderText="Product Name">
                        <ItemTemplate><asp:label id="Product_Name" text='<%# databinder.eval(container.dataitem, "Product_Name") %>'  runat="server"></asp:label></ItemTemplate>
                    </asp:TemplateColumn>

                    <asp:TemplateColumn HeaderText="Price">
                        <ItemTemplate>$<asp:label id="Product_Price" text='<%# databinder.eval(container.dataitem, "Product_Price") %>'  runat="server"></asp:label></ItemTemplate>
                    </asp:TemplateColumn>

                    <asp:TemplateColumn HeaderText="Size">
                        <ItemTemplate><asp:label id="Product_Size_Text" text='<%# databinder.eval(container.dataitem, "Product_Size_Text") %>'  runat="server">
                        </asp:label></ItemTemplate>
                    </asp:TemplateColumn>
                                       
                    <asp:TemplateColumn HeaderText="Colour">
                        <ItemTemplate><asp:label id="Product_Colour_Text" text='<%# databinder.eval(container.dataitem, "Product_Colour_Text") %>'  runat="server"></asp:label></ItemTemplate>
                    </asp:TemplateColumn>
                   
               

                    <asp:TemplateColumn HeaderText="QTY">

                        <ItemTemplate>
                   
                            <EditItemTemplate>

                                <asp:TextBox id="txtQTY" text='<%# databinder.eval(container.dataitem, "Quantity") %>' maxlength="2" width="30" runat="server"></asp:TextBox>

                            </EditItemTemplate>

                        </ItemTemplate>

                    </asp:TemplateColumn>

                    <asp:TemplateColumn HeaderText="Amount">
                    <ItemTemplate>
                   
                    <!--HIDDEN STUFF-->
                    <asp:label id="Gst_Excluded" text='<%# databinder.eval(container.dataitem, "Gst_Excluded") %>'  runat="server" Visible="false"></asp:label>
                    <asp:label id="Product_Size" text='<%# databinder.eval(container.dataitem, "Product_Size") %>'  runat="server" Visible="false"></asp:label>
                    <asp:label id="Product_Colour" text='<%# databinder.eval(container.dataitem, "Product_Colour") %>'  runat="server" Visible="false"></asp:label>
                    </ItemTemplate>
                   
                    </asp:TemplateColumn>

                    <asp:ButtonColumn Text="Remove" CommandName="Delete"></asp:ButtonColumn>
                    <asp:ButtonColumn Text="Re-Calculate" CommandName="Recalculate"></asp:ButtonColumn>

                </Columns>

            </asp:DataGrid>

ITEMCOMMAND
Protected Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Select Case e.CommandName
            Case "Recalculate"
                'Dim newQTY As Integer = CType(e.Item.FindControl("txtQTY"), TextBox).Text
                Dim newQTY As Integer = CType(e.Item.FindControl("txtQTY"), TextBox).Text
                Response.Write(newQTY)
               

                shopping_cart.AddToCart(Me.txtPRODID.Text, Me.txtPRODNAME.Text, Me.txtPRODPRICE.Text, Me.ddlSIZES.SelectedItem.Text, Me.ddlSIZES.SelectedValue, Me.ddlCOLOURS.SelectedItem.Text, Me.ddlCOLOURS.SelectedValue, newQTY, Me.txtGST_INCLUDED.Text)
                If Page.IsPostBack = True Then
                    dg.DataBind()
                End If
        End Select
       
    End Sub
lawsoAsked:
Who is Participating?
 
active_agentConnect With a Mentor Commented:
Replace this code

If Page.IsPostBack = True Then
                     
                    dg.DataBind()
                End If

With

' you don't have to check IsPostBack Itemcommand will always occur in the postback
dg.Datasource  = data ' Reassign the updated data source
dg.DataBind()

If there is still a confusion , please post the Page_Load code.
0
 
SystemExpertCommented:
Hi,

 If Page.IsPostBack = True Then
       dg.DataSource = ds  // where ds is your datasource so fill it again here
       dg.DataBind()
End If
Thanks & Regards
0
 
active_agentCommented:
Why have you added txtQTY under <EditTemplate> ? Should'nt it be in <ItemTemplate> itself.

Secondly, I assume that when you call shopping_cart.AddToCart it changes the underlying datasource so you need to reassign new data (or datasource) to datagrid before databind().

and

' If Page.IsPostBack = True Then 'You don't have to check IsPostback ItemCommand will only call during IsPostBack
                     dg.DataSource = dt
                    dg.DataBind()
'End If
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
SystemExpertCommented:
This is correct what lawso has did

I think txtQTY will have the value when datagrid is for EDIT mode
so Edited value in txtQTY will assign as new value
0
 
SystemExpertCommented:
The ItemTemplate simply displays the value of the DataSource field this column is to display,
while the EditItemTemplate creates a TextBox Web control for the user to edit the data.
0
 
active_agentCommented:
Reply to SystemExpert:
 i know what EditItemTemplate is for, only reason i aasked this is because i don't see any handler where the EditItemIndex is being set.


Question to Origianal Poster.
<EditItemTemplate> will only show TextBox when a EditItemIndex of the datagrid will be set to datagridrowindex which needs to be edited. And you can edit only one row at a time. what i would like to know is where are you setting the EditItemIndex for the datagrid.

This is what is my understanding.

You Datagrid is actually used to display ShoppingCart and every row has QTY text box. Which user changes and Recalculates based on new quantity in the Itemcommand handler..
0
 
lawsoAuthor Commented:
I was assuming that the DataGriid handled all of the creation of unique ID's etc.
I was also assuming that the Re-Calculate would recalculate for that DataRow only.
Am I wrong?

When I click Re-Calculate it goes to the ItemCommand function no problems, it just can't find txtQTY in the DataRow.

How would this normally be handled?
How do I set the EditItemIndex?

your help is appreciated.
0
 
active_agentCommented:
If you will put the textbox in <ItemTemplate> it will work. (I assume that you want to display textbox for all the rows (shopping cart item)  in the datagrid.

In case you are interested in Inline Editing with Datagrid.

Inline Editing generally works as per row basis. You have to add following tag
<asp:EditCommandColumn runat="server" HeaderText="Edit" EditText="Edit" UpdateText="OK" CancelText="Cancel"></asp:EditCommandColumn>

Also handle follwing events:
OnEditCommand, OnUpdateCommand, OnCancelCommand.

In the OnEditCommand Handler
grid.EditItemIndex = e.Item.ItemIndex;
//Binddata again
After this the text box will be displayed with Update and cancel link.

Then accordingly you write code for Update and Cancel handler.




0
 
lawsoAuthor Commented:
Maybe I am missing something here because I can get the code working, but it is picking up the old value of txtQTY. Regardless of whether I am refreshing the DG correctly

This seems to be the root of my problem and I can't see why.

If I have:
- Two rows wach with a different txtQTYvalue
- I change the txtQTY value on one row
- I click on re-Calculate

It finds the right txtQTY but it sets the value to the OLD value that I have overtyped.

What am I missing in this process.
I can find txtQTY but it has the old version

I now have the txtQTY insid eonly the <ItemTemplate> only and it is doing the same thing
0
 
active_agentCommented:
Is it updating the correct value and then displaying the old value.

did you change the databind code.

dg.datasource = <datasource> //It should refresh with the new data
 dg.DataBind()


0
 
lawsoAuthor Commented:
This is my code ... from what I can see I am not doing anything to the DataSource.

 Protected Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Select Case e.CommandName
            Case "Recalculate"
                Dim newQTY As Integer = CType(e.Item.FindControl("txtQTY"), TextBox).Text
                'Dim newQTY As Integer = e.Item.FindControl("txtQTY").text
                Response.Write(newQTY)
               

                shopping_cart.AddToCart(Me.txtPRODID.Text, Me.txtPRODNAME.Text, Me.txtPRODPRICE.Text, Me.ddlSIZES.SelectedItem.Text, Me.ddlSIZES.SelectedValue, Me.ddlCOLOURS.SelectedItem.Text, Me.ddlCOLOURS.SelectedValue, newQTY, Me.txtGST_INCLUDED.Text)
                If Page.IsPostBack = True Then
                     
                    dg.DataBind()
                End If
        End Select
       
    End Sub
0
 
lawsoAuthor Commented:
Wow I am an amateur....
I was reseting the DataTable from a Session variable on page load.

A simple IF NOT PAGE.ISPOSTBACK THEN has done the trick.

Thanks to everyone for participating... I have given active agent the points for perserving with me and points me to the Page Load
0
 
active_agentCommented:
Thanks ..It was a pleasure to help you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.