Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why this code doesn't work ?

Status
Not open for further replies.

liuk

Programmer
Jan 16, 2006
54
IT
hi
i write this code :
VB.NET Code

Private Sub DG1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG1.ItemDataBound

If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
CType(e.Item.FindControl("btnEdit"), ImageButton).Attributes.Add("onclick", "SaveObjID(this.id)")
CType(e.Item.FindControl("btnDelete"), ImageButton).Attributes.Add("onclick", "SaveObjID(this.id)")

End If
End Sub


JAVASCRITP CODE (within asp.net page)


function SaveObjID(OBJID)
{
document.getElementById('ObjectID').value = OBJID ;
}

function Scroll()
{
document.getElementById(document.getElementById('ObjectID').value).scrollIntoView();
}

where the Scroll function is invoked on the body.onload .

On postback , into the Scroll function the document.getElementById('ObjectID').value is correct but the whole instruction return NULL

The stange is that if i save explicitly (into the SaveObjID function) an object id, it works!


Any suggest?
thanks
 
are you saying
document.getElementById('ObjectID').value
holds the correct value?

if so have you tried...
Code:
function Scroll()
{
var myid = document.getElementById('ObjectID').value;
document.getElementById(myid).scrollIntoView();
}

also you could place and alert on myid to check it's value while testing.

...
Code:
function Scroll()
{
var myid = document.getElementById('ObjectID').value;
alert("ObjectID = " + myid);
document.getElementById(myid).scrollIntoView();
}


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes, initially i wrote something like that. It's always the same... the variable has the correct value,but it seems it can't find the object.
But if i write document.getElementById('ObjectID') = 'ucEditGrid_DG1__ctl5_btnEdit' into the SaveObjId function, the scroll function works well.
 
so the call
Code:
CType(e.Item.FindControl("btnEdit"), ImageButton).Attributes.Add("onclick", "SaveObjID(this.id)")
is saving the correct id of the element which is 'ucEditGrid_DG1__ctl5_btnEdit' but the getElementbyid is erroring with 'is null or not an object' error?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
when is this sub called ?
Code:
Private Sub DG1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG1.ItemDataBound

If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
CType(e.Item.FindControl("btnEdit"), ImageButton).Attributes.Add("onclick", "SaveObjID(this.id)")
CType(e.Item.FindControl("btnDelete"), ImageButton).Attributes.Add("onclick", "SaveObjID(this.id)")

End If
End Sub


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
On datagrid item databound
When the grid is bounded i attach the script code to each edit and cancel button in order to reposition that row into the DIV where the grid is placed.
When the user click the edit button the id of the button is saved into the hidden field and then, after postback, that id is retrieved with the Scroll function (javascript) and the reposition should take place.
 
right so this is when a user clicks something yes? , but you are invoking the scroll 'onload'
where the Scroll function is invoked on the body.onload

so at that time the onclick event hasn't happend and SaveObjID(this.id) hasn't placed a value yet in document.getElementById('ObjectID').value , do you think this is the problem?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Well, this is what happened (tested with debug) when i click the edit button in the grid :

- The page load event handler is executed
- The UserControl (my GRID) page load event handler is executed
- Datagrid's ItemDataBound (i re-bind my grid)
- the scroll function (within the Page ) stops with error
and the hidden field has the correct value (last button clicked).

I also modified the scroll function in this way :

function Scroll(){
var ObjId = document.getElementById('ScrollPos').value;
if(ObjId!=null)
{
alert(ObjId);<--- show the correct value
document.getElementById(ObjId).scrollIntoView(); <-- ERROR
}
}
 
odd if the object is on the page and the value is correct, then it shouldn't error with object is null or not an object, does the object definately have the method scrollIntoView(); defined at this stage?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Look at this:
function Scroll(){
var ObjId = document.getElementById('ScrollPos').value;
document.getElementById('UcEditGridCarico_DG1__ctl8_btnEdit').scrollIntoView();
alert(ObjId);
}

When i click any of the edit button in grid different from UcEditGridCarico_DG1__ctl8_btnEdit the grid scrool correctly on the row containing that button and the alert shows the clicked button id.
When i click right the UcEditGridCarico_DG1__ctl8_btnEdit btn, the grid scroll on top and the alert doesn't start!

I think i'm getting crazy!
 
lol - I know the feeling, if it's not a typo, the method is defined, the object on the page then I have no idea!

though i must say thats a nasty name for an ID, is there anything different with this name than any of the other that do work?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Ops, in the code before read ObjectId for ScrollPos.

No, all edit button id have the same structure , change only the number (ctl0, ctl1, ...) (the id is the client id of any button as is in the HTML code of the result page).


 
Show the final output code, not the ASP.NET scripting, so we can see the HTML you're working with. You can find this using View -> Source

And copy and paste the code in here, don't type it in, so we see what the browser is working with.

Lee
 
Ops, in the code before read ObjectId for ScrollPos.
? have you cracked it ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
No i didn't crack it... i have two test page and the last code refers to the second one.


This is the HTML section for the script :

function SaveObjID(OBJID)
{
document.getElementById('ObjectId').value = OBJID ;
}

function Scroll(){
var ObjId = document.getElementById('ObjectId').value;
document.getElementById('UcEditGridCarico_DG1__ctl8_btnEdit').scrollIntoView();
alert(ObjId);
}


whereas this is a portion of the page :

<tr class="DG2ItemStyle" style="font-size:7pt;">
<td>
<span id="UcEditGridCarico_DG1__ctl8_lblArticolo">itemA</span>
</td>
<td>
<span id="UcEditGridCarico_DG1__ctl8_lblMagazzino">stockA</span>
</td>
<td align="Right">
<input name="UcEditGridCarico:DG1:_ctl8:txtQuantita" type = "text" value ="15" id="UcEditGridCarico_DG1__ctl8_txtQuantita" class="dgdefault" style="width:80px;" />
</td>
<td>
<input type="image" name="UcEditGridCarico:DG1:_ctl8:btnEdit" id="UcEditGridCarico_DG1__ctl8_btnEdit" onclick="SaveObjID(this.id)" src="/img/icone/modifica.gif" alt="" border="0" />
</td>
<td>
<input type="image" name="UcEditGridCarico:DG1:_ctl8:btnDelete" id="UcEditGridCarico_DG1__ctl8_btnDelete" onclick="SaveObjID(this.id)" src="/img/icone/elimina2.gif" alt="" border="0" />
</td>
</tr>
 
can we see a copy paste of the code that does work, it's hard to see if there is a difference otherwise!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
do you want to see the HTML of the whole page or the vb.net code ?
 
HTML / JS

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Have to note that input type="image" is a control which will submit upon clicked. Hence, I would suggest change the control from "input" to a simple img tag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top