Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6038
  • Last Modified:

How to get Javascript confirmation on buttonfield in Gridview?

Hi,
I'm able to get a Javascript confirmation popup on normal button, but how do i do the same on the buttonfield in Gridview?

Below is my code, i'm able to get the popup, but clicking on "OK" doesn't do anything (it suppose to process something on the code).

Please advise.
Protected Sub gvwAllItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwAllItem.RowDataBound
 
        'Process the each data row in the Gridview.
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim btnRemove As Button = e.Row.Cells(1).Controls(0)
            btnRemove.Attributes.Add("onclick", "javascript:return " + "confirm('" + "delete?" + "')")
   
           End If
 
    End Sub

Open in new window

0
tangteng78
Asked:
tangteng78
  • 10
  • 9
1 Solution
 
DeathraceCommented:
hey try this....


<script>
function AlertMessage()
{
 
alert("hey this is popup");
 
}
 
 
 
// Grid View Row_dataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            DirectCast(e.Row.FindControl("Buttonname"), Button).Attributes.Add("onclick", "javascript:AlertMessage('" & DirectCast(e.Row.FindControl("ddl"), Button).ClientID & "')")
        End If

Open in new window

0
 
DeathraceCommented:

Some chage in Row_dataBound

// Grid View Row_dataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            DirectCast(e.Row.FindControl("Buttonname"), Button).Attributes.Add("onclick", "javascript:AlertMessage();")
        End If
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
tangteng78Author Commented:
I can't use the the findcontrol as the buttonfield can't be assign an ID.

This is  how i define the buttonfield for the gridview in the page_load.

            Dim btnRemove As ButtonField = New ButtonField()
            btnRemove.ButtonType = ButtonType.Button
            btnRemove.Text = "Remove"
            btnRemove.CommandName = "Remove"

And btw, i'm looking for confirmation popup, NOT alert popup :)
0
 
tangteng78Author Commented:
Guys,
Any help is greatly appreciated.
0
 
DeathraceCommented:
See the below,, code it works.

ClientSide:
<asp:GridView1
 ....
....
  <asp:Button ID="ButtonRemove" runat="server" CommandName="Remove" Text="Remove"     OnClientClick="return confirm('Do you really want to Add?');" />
</asp:GridView>


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       if (e.CommandName.Equals("Remove"))
       {
         // perform logic to remove from Db or table or Grid          
       }
    }
0
 
DeathraceCommented:
sorry change the message inside the confirm to "Do you want to remove"
0
 
tangteng78Author Commented:
How do i do this programatically?

PS: I add the button field programatically in my code. See below.

            Dim btnEdit As ButtonField = New ButtonField()
            btnEdit.ButtonType = ButtonType.Button
            btnEdit.Text = "Edit"
            btnEdit.CommandName = "Edit"
            gvwAllItem.Columns.Add(btnEdit)
0
 
DeathraceCommented:

Something like this, after initiating btnEdit,
btnEdit.Attributes.Add("OnClientClick","return confirm('Do you really want to Add?'));"
0
 
tangteng78Author Commented:
The editor will underline the error when trying to do that.
Meaning, there is NO .Attributes for buttonfield (unlike button which this works fine).
0
 
DeathraceCommented:
Ok, here btnEdit will be of multiple buttons on the GridView, so to add attributes to it . in rowDataBound event. try this sure luck ..

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As Button = CType(e.Row.FindControl("btnEdit"), Button)
            btn.Attributes.Add("OnClientClick","return confirm('Do you really want to Add?'));"
        End If
    End Sub
0
 
tangteng78Author Commented:
Deathrace,
I modified your code to below (since the buttonfield has no ID, i can't use the findcontrol).

            Dim btn As Button = CType(e.Row.Cells(1).Controls(0), Button)
            btn.Attributes.Add("OnClientClick", "return confirm('Do you really want to Add?'));")

Well, nothing happens, no popup confirmation box, it just bring me straight to the onclick event.

Again, i changed it to below, there is popup window, but clicking on the "ok" button doesn't bring me to the onclick event.


            Dim btn As Button = e.Row.Cells(1).Controls(0)
            btn.Attributes.Add("OnClick", "return confirm('Do you really want to Add?'));")

Anymore ideas?
0
 
DeathraceCommented:
You cannot use OnClick here, try to add the ID to your button and use my code...they it will surely work...
0
 
tangteng78Author Commented:
There is no ID option for the buttonfield type :(
0
 
DeathraceCommented:
how about placing a button control on ItemTemplate of your grid, which has id as well...why don't you try this...any specific problems with Button over ButtonField
0
 
tangteng78Author Commented:
well, i will get the error of conversion from Webcontrols.Button cannot be converted to WebControls.DataControlField.

See the attached printscreen.

conversion-error.jpg
0
 
DeathraceCommented:
hey,,
in this 24384718 post, you said you tried something like this

 Dim btn As Button = e.Row.Cells(1).Controls(0)
  btn.Attributes.Add("OnClick", "return confirm('Do you really want to Add?'));")

Did u try with OnClientClient event for the same..instead of OnClick.. ?

0
 
tangteng78Author Commented:
Yes. From my posting ealier.

"Well, nothing happens, no popup confirmation box, it just bring me straight to the onclick event."
0
 
tangteng78Author Commented:
btw, do you have a working code ? think we are going into rat-hole :)
0
 
DeathraceCommented:
yes you are right,,,,but let me try it...in differnet way
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now