Confirm Message in gridview

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>
 
A

Ameet Phadnis(e Tek Global Inc.)

If you are trying to set it for commandbutton you need to get reference to
the command button. You need to do something like this -

dim testcomm as commandbutton =
ctype(e.Row.Cells(3).FindControl("<comandname>"), CommandButton)

testcomm.attributes.Add("onclick", "return ConfirmDeletion();")

Please check the syntax. I have given the solution based on my previous
experience.

Thanks,
 
J

John Wright

I found a solution. The problem was not wiring the button up to the
javascript, but when the RowCommand was running there was nothing in the
CommandArgument, so I added this line to the RowDatabound:
l = CType(e.Row.Cells(0).FindControl("Reset"), LinkButton)
l.Attributes.Add("onclick", "return getconfirm();")
l.CommandArgument = e.Row.RowIndex '-----> Add this row

Once I added the row index as the command arugment, then the RowCommand
event fired as normal.

John

"Ameet Phadnis(e Tek Global Inc.)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top