Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

e.Item is null in DataGrid OnUpdateCommand

Status
Not open for further replies.

RobHudson

Programmer
Joined
Apr 30, 2001
Messages
172
Location
GB
Hi

I am using the EditCommandColumn column in a DataGrid and e.Item is null in the OnUpdateCommand event code.

Any ideas why? No doubt I am doing something worng...

Page side is:
Code:
    <asp:DataGrid ID="dgUsers" runat="server" AutoGenerateColumns="False" HeaderStyle-CssClass="thCopy" DataKeyField="ID"
                  OnItemCreated="dgUsers_ItemCreated"
                  OnItemDataBound="dgUsers_ItemDataBound"
                  OnEditCommand="dgUsers_OnEditCommand"
                  OnCancelCommand="dgUsers_OnCancelCommand"
                  OnDeleteCommand="dgUsers_OnDeleteCommand"
                  OnUpdateCommand="dgUsers_OnUpdateCommand">
        <Columns>
            <asp:TemplateColumn HeaderText="Username">
                <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Username")%></ItemTemplate>
                <EditItemTemplate><asp:TextBox id="txtUsername" runat="server" CssClass="textInput" Text='<%# DataBinder.Eval(Container.DataItem, "Username")%>'></asp:TextBox></EditItemTemplate>
                <FooterTemplate><asp:TextBox id="txtUsername" runat="server" CssClass="textInput" Text=""></asp:TextBox></FooterTemplate>
            </asp:TemplateColumn>
            
            <asp:EditCommandColumn CancelText="[cancel]" EditText="[edit]" UpdateText="[save]" HeaderText="Action"></asp:EditCommandColumn>
            <asp:ButtonColumn Visible="false" CommandName="Delete" Text="[delete]"></asp:ButtonColumn>
        </Columns>    
    </asp:DataGrid>

and server side is
Code:
        protected void dgUsers_OnUpdateCommand(object sender, DataGridCommandEventArgs e)
        {
            //Save
            SaveUser(e.Item);

            //Reset and reload
            lbCancelNew_Click(sender, e);
            LoadUsers();
        }

Thanks for your help :)
Rob
 
Well a follow up...

e.Item (after a reboot) is now not null! Unfortunately the e.Item.DataItem that I ultimately needed to use is null.

Needless to say - rebooting hasn't fixed this one! ;)

I have done a workaround by reloading the original data for that row instead.

Used this to get the data's ID:
Code:
dgUsers.DataKeys[e.Item.ItemIndex]

I'm guessing this is has something to do with ViewState - but I'm not sure...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top