Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys. got a huge problem here. How can I get the controls inside gridview using javascript:
I am using this code but I dont know if it is right or not.
It is on my server side
VB
Protected Sub grdCategory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCategory.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
 Dim txttry As TextBox = DirectCast(e.Row.FindControl("txtCourseCategory"), TextBox)
            Dim labeltry As Label = DirectCast(e.Row.FindControl("lbllastname"), Label)
            labeltry.Attributes.Add("onclick", "javascript:return validate('" + txttry.ClientID + "','" + labeltry.ClientID + "')")
end if

now on my client side
C#
function validate(perobj, stockobj) {
          alert(perobj)
      }

it says on my alert
ctl00_ContentPlaceHolder1_gridCategory_ctl02_txtCourseCategory
and when I change it to
XML
function validate(perobj, stockobj) {
           document.getElementById('<%=perobj.ClientID%>').style.display = 'none';
       }

Im getting an error saying perobj is not declared

All I want Is to get the control txtCourseCategory and lbllastname on javascript inside my gridview

Thanks and sorry for my english
Posted

Of course it is not defined.

The text "perobj.ClientID" appears on server side where it makes no sense. The code is generated on server side first. What is placed in <% ... %> is the code on the server side. This code is evaluated on the server side and the text result of it is combined with the text of HTML+JavaScript (inserted in it). From the standpoint of server-side code, everything around it is just the text. The declaration of function validate(perobj, stockobj) is the JavaScript code which will be interpreted only after the HTTP response is sent to the client, and only on client side. This client-side code simply does not exist as such for the server-side code.

The whole idea is wrong, so I think this code is totally beyond repair.

You apparently have no idea what happens on server side and client side, and that's why you have all kinds of problems. From this and some of your previous questions I cannot see how can you knowingly develop ASP.NET and any other advanced types of applications. You rather need a good book on C# and .NET and read it all from beginning to the end, do simple exercises using just the console applications.

—SA
 
Share this answer
 
Comments
Espen Harlinn 6-Feb-12 6:33am    
Good reply :)
Tech Code Freak 6-Feb-12 6:38am    
5up! Correct!
Hi,
if your alert shows
ctl00_ContentPlaceHolder1_gridCategory_ctl02_txtCourseCategory
then do not use the server tag <%=... %> just use
perobj itself like
JavaScript
function validate(perobj, stockobj) {
           document.getElementById(perobj).style.display = 'none';
       }


hope this will work.
 
Share this answer
 
v2
Comments
Espen Harlinn 6-Feb-12 6:32am    
My 5 :)
Tech Code Freak 6-Feb-12 6:38am    
5up!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900