Link to home
Start Free TrialLog in
Avatar of Steege
Steege

asked on

Sending Client ID's To Javascript from Nested Controls

SO this is what i am doing, i need to provide the customer with for lack of a better word three tiered grouping control. So to explain I have two levels of groupings, there is a super high level, the user the clicks on that and it expands to the next leve. The user can then expand one further and there is the details. The detail row constists of the record information and it gives the users the ability to sort these projects. So what i did to accomplish this was use a table defined in the aspx call the database and get all the rows for that table, then for each individual row i would add the additional rows and then check to see if there were any child rows. If so i would call the database and then build another table with the second tier of information. I subsequently do the same where i check to see if there are any details for that. If so i add a user control i created ( a gridview) and then bind it with information. I handle the collapsing and expanding through javascript. So this all works but what i need to happed is there is a text box  and a drop down that i need to fire a javascript funtion when the values are changed. I am adding this code in the user control code behind using the attributes.add("onchange", "Function( " + control.ClientID +")" but when the event fires it says that the control does not exist , when i view source the client side control name does not match with what is the function is receiving. I am building the controls on the Init so i would think it would not be a problem. I can provid the code if that would help
Avatar of sunithnair
sunithnair

Try like this
Code behind inside Griview_RowDataBoundEvent
 
e.Row.FindControl("ControlName").Attributes.Add("onchange", "JFunction( '" + control.ClientID +"')")
 
JavaScript
<script language='javascript'>
function JFunction(src)
{
  var control = document.getElementById(src);
  //do whatever you want with the "control" now
}
</script>

Open in new window

Avatar of Steege

ASKER

Then i get 'null' is null or not an object,. i found some code that does work to find the cotrol, but there are often lots of controls to look through so if the control being changed is at the end of the list it does take a while to make the change. I was using a cotrol that i found off the web called HierarGrid which allows you to nest gridviews but the page size ended up getting pretty bloated. i am using the same control and it worked before so i cant see what i am doing wrong with this one
function findObjWithClientId(Id)

{

var ctrls = document.all;

for(var count = 0; count < ctrls.length ; count ++)

{

var index = ctrls[count].id.indexOf(Id);

if(index != -1)

{

if((ctrls[count].id.length - index) == Id.length)

{

return ctrls[count];

}

}

}

return null;
}
This is the better way of doing it. dociment.all only works in IE use getElementById instead
function findObjWithClientId(Id)
{
  return document.getElementById(Id);
}

Open in new window

Avatar of Steege

ASKER

I get a Then i get 'null' is null or not an object when i use that fundtion
Try putting an alert in the function and see what is the output and see if that element exists in the html source
function findObjWithClientId(Id)
{
  alert(Id);
  return document.getElementById(Id);
}

Open in new window

Avatar of Steege

ASKER

its is not in the html source it exists there as id="ctl00_ContentPlaceHolder1_gvDetails_gvDetails_ctl02_sbPriorityRanking"   can i assume that the ctl00_ContentPlaceHolder1_gvDetails will always be there?
ASKER CERTIFIED SOLUTION
Avatar of sunithnair
sunithnair

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steege

ASKER

i could just add the ContentPlaceHolder1_ to the string and it should find it
yes try doing that
Avatar of Steege

ASKER

that does work, i am guessing "ContentPlaceHolder1_ " should be consistent
Anytime you use a masterpage, "ctl00_ContentPlaceHolder1_" is going to be before your control name.

ctl00 is the default name for the Form container that the content place holder is in.

So it goes FormID_ContentID_ControlID