?
Solved

ItemCommand not getting the correct value in DataGrid

Posted on 2007-07-30
13
Medium Priority
?
686 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:lawso
  • 6
  • 4
  • 3
13 Comments
 
LVL 10

Expert Comment

by:SystemExpert
ID: 19591703
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
 
LVL 2

Expert Comment

by:active_agent
ID: 19591713
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
 
LVL 10

Expert Comment

by:SystemExpert
ID: 19591758
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:SystemExpert
ID: 19591791
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
 
LVL 2

Expert Comment

by:active_agent
ID: 19592505
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
 

Author Comment

by:lawso
ID: 19596135
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
 
LVL 2

Expert Comment

by:active_agent
ID: 19596279
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
 

Author Comment

by:lawso
ID: 19596717
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
 
LVL 2

Expert Comment

by:active_agent
ID: 19596893
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
 

Author Comment

by:lawso
ID: 19597865
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
 
LVL 2

Accepted Solution

by:
active_agent earned 1000 total points
ID: 19600311
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
 

Author Comment

by:lawso
ID: 19604754
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
 
LVL 2

Expert Comment

by:active_agent
ID: 19606310
Thanks ..It was a pleasure to help you!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question