views:

937

answers:

2

I have a modal popup that has a targetId to a hidden button. I want the popup to occur when a button in a grid is clicked but that button is programmed behind the code and therefore the targetId would be invalid...

So I wanted to attempt to set the gridview's button's onclientclick event to be the onclientclickevent of that hidden button. Is this possible or should I be going about this another way.

here is how i created the grid button

 If Not IsPostBack Then
        Dim field As New TemplateField
        field.HeaderText = "Sub Departments"
        Dim col As DataControlField = field
        GridView1.Columns.Add(col)

        For i As Integer = 0 To GridView1.Rows.Count - 1
            Dim btnview As New ImageButton
            btnview.ImageUrl = "\images\icons\xp_ico_search_24x24.gif"
            GridView1.Rows(i).Cells(3).Controls.Add(btnview)
        Next

End If
+4  A: 

I am assuming you are using web forms. If so then yes, it it veru possible. Do the following.

  1. Create a javascript function on the page function openModal(btnId){ btn = document.getElementById(btnId); btn.click(); // this should fire the click even of the button }

  2. on the grid button add the onclientclick event: gridButton.OnClientClick = String.Format("openModal('{0}');", modalButton.ClientId))

This will set the client Id of the button that trigers the modal window into the javascript function. If you need to populate the modal window with other data, you should do it in this function as well.

Are you using the ASP.Net AJAX Control Toolkit? Or something else? This assumes the toolkit.

Also, you have set the visibility of the button to hidden, but do not the the Visible=False property on the server side, as this will not render the button. To hide it you will need to use the client side property style="display:none"

This link may help: http://forums.asp.net/t/1066506.aspx

aquillin
Wow. you nailed it!. Thank you!
Eric
What if i wanted to change a textbox's text on client click right before this function fires off? The reason I want to do that is because that modal displays a gridview with data that depends on data from a textbox. That textbox will get it's data onclient click from the gridview's row data. any idea how to do this all on the same onclientclick?
Eric
On when you set the onclientclick property, you can nest multiple javascript statement as long as they are seperated by a ";". As:gridButton.OnClientClick = String.Format("getElementById('txtBoxId').text='new text'; openModal('{0}');", modalButton.ClientId))
aquillin
+1  A: 

It is possible doing that, it requires you to fire off the buttons "DoPostback" function.

Try looking into the Page.ClientScript.GetPostBackClientHyperlink method. http://msdn.microsoft.com/en-us/library/ms223396(VS.80).aspx

Alternatively you can use javascript and the .click() event, but I think that limits you to IE as a browser, not sure on that though.

It would look like this javascript:document.getElementById('clientSideID').click();

thmsn