Gridview SelectedIndexChanged Event Not Firing Off Javascript

I have a page with two frames.  Frame 1 has a bunch of labels and text boxes and frame 2 has a gridview.  I would like to populate the labels and text boxes with values from the gridview when I click on the select buttons in the gridview.  I have researched this problem modeled the code at http://www.15seconds.com/issue/030528.htm and have been able to transfer values between frames from one textbox to another, but am stuck at trying to move values from the gridview on the select event.

At first I tried to load my script on the pageload event by doing this:


Gridview1.Attributes.Add("SelectedIndexChanged", "javascript:tranRight(txtTransferRight.value)")

That didn't work, and it seems I'm reading that others have tried and failed as well.  I read somewhere else I should try to register the script from the SelectedIndexChanged event handler so I tried this below, which puts a value in the text box of "Undefined" instead of the value in the gridview row.  Can anyone tell me what I'm doing wrong?

Thanks!

========================

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

Dim rows As GridViewRow = GridView1.SelectedRow

Dim Str As String

Str = rows.Cells(1).Text

GridView1.Attributes.Add(
"SelectedIndexChanged", "javascript:tranRight(Str)")

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "callFunctionsStartupScript", "tranRight();", True)

End Sub

0
NelsonSun
4/28/2009 3:43:12 AM
asp.net.client-side 24353 articles. 1 followers. Follow

6 Replies
2887 Views

Similar Articles

[PageSpeed] 22

  

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    Dim Str As String = GridView1.SelectedRow.Cells(1).Text
    Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "callFunctionsStartupScript", "<script>tranRight('" & Str & "');</script>")
End Sub
 
When you ask a question, remember to click "Mark As Answer" when you get a reply which answers your question.
0
Kulrom
5/4/2009 11:21:37 AM

what's the need of RegisterClientScriptBlock?

is it a async call ?

i think we can direct get the values from the gridview and populate the labels and textbox


Ajay Pathak
MVP
All About ASP.net
Microsoft TechEd India 2009


0
ajaypathak
5/4/2009 11:31:58 AM

This: GridView1.Attributes.Add("SelectedIndexChanged", "javascript:tranRight(Str)")

will never work as there is no SelectedIndexChanged event client-side.

NC...

0
NC01
5/4/2009 11:34:33 AM

NC01:

This: GridView1.Attributes.Add("SelectedIndexChanged", "javascript:tranRight(Str)")

will never work as there is no SelectedIndexChanged event client-side.

NC...

 

 

it will work just perfect. You can easily test it. 

Just drag and drop a new GridView to the form and then add the following code:

 

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strArray() As String = {"First", "Second", "Third"}
Me.GridView1.DataSource = strArray
Me.GridView1.DataBind()
End Sub

Protected Sub
GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "alert", "<script>alert('" & GridView1.SelectedIndex & "');</script>")
End Sub

 

btw, we are not talking about a client-side event handler .. it is just server-side event and as you see it works just fine. Wink


When you ask a question, remember to click "Mark As Answer" when you get a reply which answers your question.
0
Kulrom
5/4/2009 1:49:31 PM

I said this won't work: GridView1.Attributes.Add("SelectedIndexChanged", "javascript:tranRight(Str)")

since that is attaching a call to a JavaScript function named tranRight to a CLIENT_SIDE event named SelectedIndexChanged. In your code, you are handling the SERVER_SIDE event and rendering a call to the client-side function which will happen AFTER the page then re-renders.

NC...

0
NC01
5/4/2009 2:22:25 PM

 ups sorry i overlooked that.

I didn't notice that actually you refer his original post and his try to attach an (client-side) event SelectedIndexChanged.

 

Yes, as you mentioned it won't work .. thanks!


When you ask a question, remember to click "Mark As Answer" when you get a reply which answers your question.
0
Kulrom
5/4/2009 2:39:35 PM
Reply:

Web resources about - Gridview SelectedIndexChanged Event Not Firing Off Javascript - asp.net.client-side

Does “display:none” render the control ineffective?
I am using a CMS for my site and im trying to alter the Skin of one module in the CMS. I do not have the source code of the module. Only the ...

Resources last updated: 12/17/2015 5:54:57 PM