Solved

How to get Javascript confirmation on buttonfield in Gridview?

Posted on 2009-05-07
20
5,601 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

11 Experts available now in Live!

Get 1:1 Help Now