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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid OnItemCommand, OnSortCommand, and OnEditCommand conflicts

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi DataGrid gurus,

Got a query for you. Let me try and explain what's going on: I've got a DataGrid control on a page with editing and sorting enabled (paging will come later, too), and I added a custom method that allows a record to be added. To do this, I bound a series of Textbox controls in <FooterTemplate>s nested within <asp:TemplateColumn>s.

To fire the event, I:
(1) use a Button control in the <FooterTemplate> - <asp:Button
id=&quot;btnAddRecord&quot; CommandName=&quot;Insert&quot; Text=&quot;Add&quot; runat=&quot;server&quot;/>
(2) wire the OnItemCommand event of the DataGrid to the custom method -
<asp:DataGrid id=&quot;dgContacts&quot; AllowSorting=&quot;True&quot; ShowFooter=&quot;False&quot;
OnSortCommand=&quot;SortGrid&quot; OnItemCommand=&quot;Add_New_Record&quot;
AutoGenerateColumns=&quot;False&quot; OnEditCommand=&quot;Edit_Contacts&quot;
OnUpdateCommand=&quot;Update_Contacts&quot; OnCancelCommand=&quot;Cancel_Edit&quot; .......>

However, I can now add record, but now the sorting and editing links don't
work, at which point they return &quot;System.NullReferenceException: Object
reference not set to an instance of an object.&quot; Do you know what object
isn't referenced? Or is there some other argument I can pass to the
DataGrid that differentiates between the built-in sorting and editing
abilities, and my custom adding?

To debug it, I took out the OnItemCommand property in my DataGrid control (which I'm setting declaratively BTW, not in code), and sorting and editing
works like it should (except the ability to add records).



Thanks!

Jason
 
Item command denotes a command on a datagrid item, whereas you're after a command for your page (or at least your datagrid as a whole), which is why I suggest the following method:

Try wiring the button to a page command event:

<asp:Button id=&quot;btnAddRecord&quot; CommandName=&quot;Insert&quot; Text=&quot;Add&quot; runat=&quot;server&quot; onCommand=&quot;handleCommand&quot;/>

Then declare your handler in code-behind:

protected void handleCommand(Object o, System.EventArgs e){
if(e.commandName.ToLower() == &quot;insert&quot;)
//call insert method here
}

Hope that helps -
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top