Link to home
Start Free TrialLog in
Avatar of egdigital
egdigital

asked on

Datagrid - OnItemCommand Question

Ok Experts here is another datagrid question..

Have a datagrid with 3 columns, 2 bound and 1 a button column.  When the user clicks the button column I want to grab the row data from the first 2 columns and perform an update to a db.  I have the following questions:

1. How do I get the data from the first 2 columns of the row where the button is pressed?
2. In order to trigger an OnItemCommand must the data grid be binded in the page_load?  If I bind the data in the page_load I can display a generic message to the screen when the button is pressed, however if I bind the data in the Page_PreRender the generic message is no longer displayed when the button is pressed.  (Just using the generic message for testing, eventually this is where the call to the db procedure would go...)  Reason why I had the dg bind in the prerender is because the datagrid values were dependent upon a value the user selected in a dropdownlist of the header which posts back to the current page.  If I keep the bind in the pageload the data is always 1 postback behind b/c as I understand it the page_load is executed prior to the dropdown_selectedIndexChange.

DataGrid:
<ASP:DataGrid id="MyDataGrid" runat="server"
    Width="855"
      OnItemCommand="dgrdItemCommand"
    BackColor="#cccccc"
    BorderColor="#FFFFFF"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana" Height="16px" BorderWidth="2"
    Font-Size="11px"
    HeaderStyle-BackColor="#999999" HeaderStyle-Height="16px" HeaderStyle-Font-Size="11px"
    EnableViewState="false"
      HorizontalAlign="Center"
      AutoGenerateColumns="false"
>

<columns>
<asp:BoundColumn
      HeaderText="First Name"
      DataField="FirstName"
      HeaderStyle-Font-Bold="true"
      HeaderStyle-HorizontalAlign="Center"
      ItemStyle-HorizontalAlign="Center" />

<asp:BoundColumn
      HeaderText="Last Name"
      DataField="LastName"
      HeaderStyle-Font-Bold="true"
      HeaderStyle-HorizontalAlign="Center" />


<asp:ButtonColumn CommandName="Delete" Text ="<img border=0 src=../images/icons/trash-24.png>">
      <ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>

</columns>
</asp:DataGrid>

Button:
Sub dgrdItemCommand( s As Object, e As DataGridCommandEventArgs )
  'where I would get the 2 bound values & call the procedure

                If e.CommandName="Delete" Then
            lblOutput.Text = "You clicked one of the delete buttons!"
      Else
            lblOutput.Text = "Error"
      End If
               End Sub
Avatar of Edward Diaz
Edward Diaz
Flag of United States of America image

This was a good question, and I wanted to find out the answer to this by myself as well.  What I found was:

use a disconnected recordset (set rs.activeconnection = nothing)

Posted by emoreau at:
https://www.experts-exchange.com/questions/20279184/Updating-more-than-one-row-in-datagrid.html?query=updating+more+than+one+row+in+a+datagrid&clearTAFilter=true

This following code was found at:
http://www.dotnet247.com/247reference/msgs/1/6342.aspx


VB.NET
----------

Dim objTable as Table
objTable = CType(dgCustomers.Controls(0),Table)
'Ignore Header Row and Footer Row
for i = 1 to objTables.Rows.Count - 2

'txtName declared as TextBox Server Control
'inside the Template Column within the DataGrid in the ASPX Page

txtName = (objTables.Rows(i).FindControl("txtName"),TextBox)
Write Update Code
--------------------
----------------------
Next

C#
-----

Table objTable;
objTable = (Table)(dgCustomers.Controls(0)
//Ignore Header Row and Footer Row
for(i=1;i<Count - 1;i++)
{
//txtName declared as TextBox Server Control inside
//the Template Column within the DataGrid in the ASPX Page
txtName = (TextBox)(objTables.Rows(i).FindControl("txtName");

Write Update Code
--------------------
----------------------
}


Hope this helps!
Kittrick
Avatar of egdigital
egdigital

ASKER

Thanks, Kittrick. Could you explain the disconnected recordset a little, in the code below (VB preferred) it looks as though I would be looping through all of the records that are in the table.  I don't understand how to use the disconnected recordset so I can identify the particular row where the button what pressed.
ASKER CERTIFIED SOLUTION
Avatar of dfaithen
dfaithen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way I added the init region so that you could see the wireup for the dropdownlist eventhandler. This is only if you wan the dropdown to trigger the event automatically. ( you also have to set AutoPostBack="True" in the control properties). If you want to handle this in a button click just refer to DropDownList1.SelectedValue to determine your data for binding.
Duane
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial