?
Solved

How to get Javascript confirmation on buttonfield in Gridview?

Posted on 2009-05-07
20
Medium Priority
?
5,913 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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 

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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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