J
John Wright
I have a gridview control that has a buttonfield in it. I want to show a
messagebox to the user confirming their decision to reset the row data. I
have included the datagrid specs and the codebehind that will reset the
data. When I create a template field of the button and use the onclick
event to call a client side java script, I do not get an commandArguements
passed so I can take action on the row. The current method I tried below
(in the RowDataBound) will bind the button but it executes the RowCommand
without waiting for client confirmation. So, how can I create a button in a
datagrid, bind it to the confirm function in Javascript, then get the
Rowcommand event to fire passing in the CommandArgument (the row number) so
I can take action.
Thanks.
John
Code behind:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
If e.CommandName = "reset" Then
Try
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)
objParam.Clear()
objParam.CreateParameter("EventID", row.Cells(0).Text, DbType.String,
ParameterDirection.Input)
objParam.CreateParameter("EventGroup", intGroupID, DbType.Int32,
ParameterDirection.Input)
objDAL.ExecuteNonQuery("ResetEvent", objParam)
row.Cells(1).Text = 0
row.Cells(2).BackColor = Drawing.Color.Green
Catch ex As Exception
Label4.Text = "Error Resetting event. Please try again."
End Try
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
Dim l As LinkButton
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.Cells(2).Text = "1") Then
e.Row.Cells(2).BackColor = Drawing.Color.Green
e.Row.Cells(2).Text = ""
Else
e.Row.Cells(2).BackColor = Drawing.Color.Red
e.Row.Cells(2).Text = ""
End If
e.Row.Cells(3).Attributes.Add("onClick", "return ConfirmDeletion();")
End If
End Sub
Grid
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="Black"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="5"
CellSpacing="1"
ForeColor="Black" Width="368px" Height="136px">
<Columns>
<asp:BoundField DataField="EventType" HeaderText="Event Type" />
<asp:BoundField DataField="LastTripDays" HeaderText="Days Since Last Trip"
/>
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:ButtonField Text ="Reset" ButtonType="button" HeaderText = "Reset"
CommandName="reset" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" BorderStyle="Solid" BorderWidth="1px"
ForeColor="Black"
HorizontalAlign="Center" VerticalAlign="Middle" Width="10px" Wrap="True" />
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="White" />
</asp:GridView>
messagebox to the user confirming their decision to reset the row data. I
have included the datagrid specs and the codebehind that will reset the
data. When I create a template field of the button and use the onclick
event to call a client side java script, I do not get an commandArguements
passed so I can take action on the row. The current method I tried below
(in the RowDataBound) will bind the button but it executes the RowCommand
without waiting for client confirmation. So, how can I create a button in a
datagrid, bind it to the confirm function in Javascript, then get the
Rowcommand event to fire passing in the CommandArgument (the row number) so
I can take action.
Thanks.
John
Code behind:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand
If e.CommandName = "reset" Then
Try
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(index)
objParam.Clear()
objParam.CreateParameter("EventID", row.Cells(0).Text, DbType.String,
ParameterDirection.Input)
objParam.CreateParameter("EventGroup", intGroupID, DbType.Int32,
ParameterDirection.Input)
objDAL.ExecuteNonQuery("ResetEvent", objParam)
row.Cells(1).Text = 0
row.Cells(2).BackColor = Drawing.Color.Green
Catch ex As Exception
Label4.Text = "Error Resetting event. Please try again."
End Try
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
Dim l As LinkButton
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.Cells(2).Text = "1") Then
e.Row.Cells(2).BackColor = Drawing.Color.Green
e.Row.Cells(2).Text = ""
Else
e.Row.Cells(2).BackColor = Drawing.Color.Red
e.Row.Cells(2).Text = ""
End If
e.Row.Cells(3).Attributes.Add("onClick", "return ConfirmDeletion();")
End If
End Sub
Grid
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="Black"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="5"
CellSpacing="1"
ForeColor="Black" Width="368px" Height="136px">
<Columns>
<asp:BoundField DataField="EventType" HeaderText="Event Type" />
<asp:BoundField DataField="LastTripDays" HeaderText="Days Since Last Trip"
/>
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:ButtonField Text ="Reset" ButtonType="button" HeaderText = "Reset"
CommandName="reset" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" BorderStyle="Solid" BorderWidth="1px"
ForeColor="Black"
HorizontalAlign="Center" VerticalAlign="Middle" Width="10px" Wrap="True" />
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="White" />
</asp:GridView>