[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to get Javascript confirmation on buttonfield in Gridview?

Posted on 2009-05-07
20
Medium Priority
?
5,983 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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