problems with client scripting invoked from DataGrid OnItemDataBoundEvent

  • Thread starter Thread starter Hrvoje Vrbanc
  • Start date Start date
H

Hrvoje Vrbanc

Hello,

I have the following problem:
I have a DataGrid control on my page with a column with TextBox controls and
another one with ImageButton controls. In order to force a call to
JavaScript client-side script when ImageButton is pressed, I added the
following code to OnItemDataBoundEvent (check for Item Types not shown
here):

Dim tbKolicina As TextBox = e.Item.Cells(6).FindControl("tbKolicina")
Dim imgbtKosarica As ImageButton =
e.Item.Cells(7).FindControl("imgbtKosarica")
imgbtKosarica.Attributes.Add("onClick", "return validacija('" &
tbKolicina.Text & "')")

"tbKolicina" is the TextBox control whose value I want to pass to JavaScript
for validation when ImageButton called "imgbtKosarica" is clicked.
JavaScript is currently just an elementary script (no validation yet) and
should simply display an alert box with the passed parameter
(tbKolicinaText) displayed when the ImageButton is clicked.

It works fine (finds the controls and calls the JavaScript) up to one final
point: the value of the TextBox (tbKolicina.text) doesn't reach the
JavaScript script. Whatever I write into the TextBox, I receive an empty
alert box. But, if I set the value for the tbKolicina.text programatically,
that value does get passed. I concluded therefore that only the initial
value reaches JavaScript.

What did I do wrong and how could I make this validation work?

Thank you in advance,
Hrvoje
 
Hi

This should pass the value of tbKolicina field to the function
imgbtKosarica.Attributes.Add("onclick", "return
validacija(this.form.tbKolicina.value)")

--
Best Regards
Vidar Petursson
==============================
Microsoft Internet Client & Controls MVP
==============================
 
Vidar,

thank you for the advice.
Unfortunately, it still doesn't work - it displays error message within the
alert box: "'this.form.tbKolicina.value' is null or not an
object"....somehow it doesn't recognize it.

Hrvoje
 
Hi

Make sure the field is there and you have the name spelled correctly ( case
sensitive )

--
Best Regards
Vidar Petursson
==============================
Microsoft Internet Client & Controls MVP
==============================
 
Hello and thank you, John.

As alert box shows, nothing was put in JavaScript, i.e., the source says
onclick="return validacija('')", just as if the text box was empty (no value
for the parameter). Funny, but the same source shows correct value in the
text box parameters, e.g. <input name="dgProdukti:_ctl4:tbKolicina"
type="text" value="711" id="dgProdukti__ctl4_tbKolicina" />, 711 being the
entered number that doesn't reach the JavaScript....
Funny!

Hrvoje
 
I just took a quick look back at your code. You're taking the value out of
the TextBox in OnItemDataBound, but when did you put a value _into_ the
TextBox?
 
Hello John,
hm, I thought that OnItemDataBound I just wire the script and that values
could be transferred later.....obviously a mistake. I was thinking about
that but if I put my code inside OnSelectedIndexChanged event, then the page
doesn't react on the first click (because only then is the script activated)
but only when the same ImageButton is clicked for the second time....
Therefore, it seems I need the new solution.

Thank you,
Hrvoje
 
Hrvoje Vrbanc said:
Hello John,
hm, I thought that OnItemDataBound I just wire the script and that values
could be transferred later.....obviously a mistake.

Nope. In OnItemDataBound you're supposed to bind data to the controls in the
item if they don't do that on their own. For instance, I have an employee
list datagrid with a Delete LinkButton in it. I use ItemDataBound to set the
JavaScript which displays the confirm dialog, and I take the opportunity to
concatenate the first and last name of the employee being deleted. I have a
HyperLink control in the same grid, and I set the NavigateUrl in the
ItemDataBound event to include the employee ID in the query string of the
URL.

So, I use it for things which are too complicated for a databinding
expression to handle, or for things where the databinding expression is
simply too complicated or ugly to look at in the .aspx file. I move it into
the .aspx.cs file where nobody needs to see it!
 
I understand.
But what is the solution for my problems then (a rhetorical question :-))?

Hrvoje
 
Back
Top