Solved

How to get Javascript confirmation on buttonfield in Gridview?

Posted on 2009-05-07
20
5,660 Views
Last Modified: 2013-12-17
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
Comment
Question by:tangteng78
  • 10
  • 9
20 Comments
 
LVL 7

Expert Comment

by:thiyaguk
ID: 24323130
0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24323460
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24323465

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
 

Author Comment

by:tangteng78
ID: 24326874
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
 

Author Comment

by:tangteng78
ID: 24365931
Guys,
Any help is greatly appreciated.
0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24366677
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24366688
sorry change the message inside the confirm to "Do you want to remove"
0
 

Author Comment

by:tangteng78
ID: 24366752
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24372403

Something like this, after initiating btnEdit,
btnEdit.Attributes.Add("OnClientClick","return confirm('Do you really want to Add?'));"
0
 

Author Comment

by:tangteng78
ID: 24377054
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Expert Comment

by:Deathrace
ID: 24377208
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
 

Author Comment

by:tangteng78
ID: 24384718
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
 
LVL 9

Expert Comment

by:Deathrace
ID: 24393274
You cannot use OnClick here, try to add the ID to your button and use my code...they it will surely work...
0
 

Author Comment

by:tangteng78
ID: 24394726
There is no ID option for the buttonfield type :(
0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24395643
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
 

Author Comment

by:tangteng78
ID: 24397326
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
 
LVL 9

Accepted Solution

by:
Deathrace earned 500 total points
ID: 24397899
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
 

Author Comment

by:tangteng78
ID: 24397927
Yes. From my posting ealier.

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

Author Comment

by:tangteng78
ID: 24397932
btw, do you have a working code ? think we are going into rat-hole :)
0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24471494
yes you are right,,,,but let me try it...in differnet way
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now