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_selectedIndexChan ge.
DataGrid:
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="855"
OnItemCommand="dgrdItemCom mand"
BackColor="#cccccc"
BorderColor="#FFFFFF"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana" Height="16px" BorderWidth="2"
Font-Size="11px"
HeaderStyle-BackColor="#99 9999" HeaderStyle-Height="16px" HeaderStyle-Font-Size="11p x"
EnableViewState="false"
HorizontalAlign="Center"
AutoGenerateColumns="false "
>
<columns>
<asp:BoundColumn
HeaderText="First Name"
DataField="FirstName"
HeaderStyle-Font-Bold="tru e"
HeaderStyle-HorizontalAlig n="Center"
ItemStyle-HorizontalAlign= "Center" />
<asp:BoundColumn
HeaderText="Last Name"
DataField="LastName"
HeaderStyle-Font-Bold="tru e"
HeaderStyle-HorizontalAlig n="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
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_selectedIndexChan
DataGrid:
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="855"
OnItemCommand="dgrdItemCom
BackColor="#cccccc"
BorderColor="#FFFFFF"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana" Height="16px" BorderWidth="2"
Font-Size="11px"
HeaderStyle-BackColor="#99
EnableViewState="false"
HorizontalAlign="Center"
AutoGenerateColumns="false
>
<columns>
<asp:BoundColumn
HeaderText="First Name"
DataField="FirstName"
HeaderStyle-Font-Bold="tru
HeaderStyle-HorizontalAlig
ItemStyle-HorizontalAlign=
<asp:BoundColumn
HeaderText="Last Name"
DataField="LastName"
HeaderStyle-Font-Bold="tru
HeaderStyle-HorizontalAlig
<asp:ButtonColumn CommandName="Delete" Text ="<img border=0 src=../images/icons/trash-
<ItemStyle HorizontalAlign="Center"><
</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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.SelectedValu e to determine your data for binding.
Duane
Duane
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
'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).FindCon
Write Update Code
--------------------
----------------------
Next
C#
-----
Table objTable;
objTable = (Table)(dgCustomers.Contro
//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
Write Update Code
--------------------
----------------------
}
Hope this helps!
Kittrick