Confirm Message in gridview

Discussion in 'ASP .Net Web Controls' started by John Wright, Oct 3, 2006.

  1. John Wright

    John Wright Guest

    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>
     
    John Wright, Oct 3, 2006
    #1
    1. Advertising

  2. 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,

    --
    Ameet Phadnis
    Sr. Technical Consultant
    e Tek Global Inc.
    ASP Alliance Author Page: http://aspalliance.com/author.aspx?uId=44260


    "John Wright" wrote:

    > 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>
    >
    >
    >
     
    Ameet Phadnis(e Tek Global Inc.), Oct 4, 2006
    #2
    1. Advertising

  3. John Wright

    John Wright Guest

    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.)"
    <> wrote in message
    news:...
    > 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,
    >
    > --
    > Ameet Phadnis
    > Sr. Technical Consultant
    > e Tek Global Inc.
    > ASP Alliance Author Page: http://aspalliance.com/author.aspx?uId=44260
    >
    >
    > "John Wright" wrote:
    >
    >> 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>
    >>
    >>
    >>
     
    John Wright, Oct 5, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Padam Jain
    Replies:
    1
    Views:
    1,300
    Anatoly
    Jul 14, 2003
  2. Grey
    Replies:
    7
    Views:
    13,659
    Greg Burns
    Sep 7, 2004
  3. Greg Smith

    Confirm Delete Message

    Greg Smith, Jun 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,687
    Steve C. Orr [MVP, MCSD]
    Jun 2, 2005
  4. hamsinii

    dropdown confirm message box

    hamsinii, Jul 6, 2006, in forum: .NET
    Replies:
    0
    Views:
    635
    hamsinii
    Jul 6, 2006
  5. Miguel Gómez
    Replies:
    3
    Views:
    1,313
    Miguel Gómez
    May 13, 2009
Loading...

Share This Page