admin管理员组

文章数量:1426642

I want to pass a parameter to a JavaScript function to use in document.getElementByID() as follows:

function PrinGridView(GridViewname)
{

 var TableRow= document.getElementByID("GridViewname").getElementsBytagName("tr");

 var td= TableRow.item(0).getElementsBytagName("th");


 if(td.length>0)
   alert('done');
 
}

In my asp page, I have an image button event:

onClicke="PrinGridView("<%=MyGrideView.ClientID%>")";

but it does not work well.

How can I pass the GridView to a function?

Thanks.

I want to pass a parameter to a JavaScript function to use in document.getElementByID() as follows:

function PrinGridView(GridViewname)
{

 var TableRow= document.getElementByID("GridViewname").getElementsBytagName("tr");

 var td= TableRow.item(0).getElementsBytagName("th");


 if(td.length>0)
   alert('done');
 
}

In my asp page, I have an image button event:

onClicke="PrinGridView("<%=MyGrideView.ClientID%>")";

but it does not work well.

How can I pass the GridView to a function?

Thanks.

Share Improve this question edited Oct 4, 2020 at 8:42 Overwatch 391 silver badge8 bronze badges asked Mar 26, 2013 at 11:30 SamerKourSamerKour 251 gold badge2 silver badges4 bronze badges 1
  • In document.getElementByID("GridViewname"), GridViewName shouldn't be in quotes – Basic Commented Mar 26, 2013 at 11:53
Add a ment  | 

4 Answers 4

Reset to default 3

Javascript is case sensitive; it's getElementById not getElementByID, getElementsByTagName not getElementsBytagName etc.

There are other typos; F12 in your browser and the Error/Console will display script errors.

You need to mix quotes as the below is not valid, aside from the typo its not a parseable string as the quotes are broken:

onClicke="PrinGridView("<%=MyGrideView.ClientID%>")";

Change to

onClick="PrinGridView('<%=MyGrideView.ClientID%>')";

Within the function you quote what should probably be the argument, change from

var TableRow = document.getElementByID("GridViewname")

to

var TableRow= document.getElementById(GridViewname)

You have a typo there, onClicke. This is wrong, should be onClick.

Try like this

onClick="PrinGridView(this)"

function PrinGridView(obj)
{
var gridName = obj.id;
 var TableRow= document.getElementByID(gridName).getElementsBytagName("tr");

 var td= TableRow.item(0).getElementsBytagName("th");


 if(td.length>0)
   alert('done');

}

this works perfectly for inline code

OnClientClick='<%#String.Format("buttonstatus(""{0}"",""{1}"",""{2}"",""{3}"");return false;", Eval("listingid"), "D", "Archived", Eval("EndDate"))%>'

本文标签: pass parameter to javascript function in aspnet PageStack Overflow