PDA

View Full Version : Adding attributes to a Gridview


jleone
09-19-2007, 04:16 PM
OK, here's my situation. A user adds a name to a request by either searching for an existing name, selecting a name previously used (from a Gridview), or by creating all new info for a new name to be added.

If a user creates all new info for a new name (thus linking the new name to the request) and then decides to switch to an existing name by selecting from the Gridview, I want to display a Javascript confirm box. I do this right now for the "New" button in case they want to add another new name in place of the new name they already created for the request by doing the following code:

If Rqst.TempName IsNot Nothing Then
btnNew.Attributes.Add("onclick", "return confirm('This will permanently delete the new name information you have already entered for this request. Are you sure?');")
End If

How do I accomplish the same thing when something is selected from the Gridview on a selected index changed? The first parameter in the attributes.add is the key string and I have tried using "OnSelectedIndexChanged" and "OnSelectedIndexChanging" like the following:

GridView1.Attributes.Add("OnSelectedIndexChanged", "return confirm('This will permanently delete the new name information you have already entered for this request. Are you sure?');")


but even when I click cancel on the dialog box, the rest of my server code still executes, unlike the behavior when I click the "New" button which prevents the server code from executing.

Thanks for any help that can be offered.

jleone
09-19-2007, 06:07 PM
OK, I thought maybe I could try adding the attribute to each row in the Gridvew like so:

Dim r As GridViewRow
For Each r In Me.GridView1.Rows
r.Cells(0).Attributes.Add("onclick", "return confirm('This will permanently delete the new name information you have already entered for this request. Are you sure?');")
Next

However, because this column in the Gridview is a Command Field ("Select") it already has a JavaScript postback added to its onclick attribute thanks to the automatic ASP code. Is there any way I could add another hidden field to this Gridview and add the attribute to this hidden field and have it reference the onclick of the Command Field in order to display the dialog box?