Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 21st, 2005, 05:23 AM
Registered User
 
Join Date: Feb 2005
Location: Karachi, Sindh, Pakistan.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dungerdanish Send a message via Yahoo to dungerdanish
Default Accessing bound controls through javascript

Hi,

firstly this isnt a question, but a general share of knowledge.
we were having some problems on how to access the specific databound controls in a datagrid on the client side,

we did some searching on the net but didnt come up with any posts for it, so here's how we did it

the controls placed in the datagrid templateColumns cannot be accessed directly by their names on the client, when ASP.net

renders the pages in HTML, the control names are replaced in this form:

<datagridname>:_ctl<indexno>:<controlname>

for example, I have a datagrid named dgAmount and i have a bound textbox named txtAmount, now the first instance name of the

text box in the bound grid on the client side would probably render as :

dgAmount:_ctl1:txtAmount

similiar is the case with id , except that the colons are replaced with '_' :

dgAmount__ctl1_txtAmount


now this creates a problem in certain scenrario's for example, if a datagrid has 2 bound text boxes called txtbox1 and

txtbox2, now the value of txtbox2 ios dependant on the value of txtbox1 which the user inputs at runtime, and based on that

value the result is calculated (currency rate conversion for example) and placed in txtbox2

It would be more efficient to deal with this kind of calculation on the client side instead of a whole postback, but the issue on the client side is how to access the target textbox? (txtbox2), for this we came up witha simple javascript method

this is based on our scenario for rate conversion:


function calculateValues(srcControl,dstControlName){
                document.getElementById(getTargetControlName(srcCo ntrol,dstControlName)).value = srcControl.value * document.getElementById("txtAmount").value
        }

function getTargetControlName(srcControl,dstControlName){
            var endIndex = srcControl.name.lastIndexOf(":");
            var str = srcControl.name.substring(0,endIndex+1) + dstControlName;
            return str; //return the completed target control name
        }

to use these functions have them bound to the controls in the Itemdatabound event of the grid :

Private Sub dgCurrency_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCurrency.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim txtbox As TextBox = CType(e.Item.FindControl("txtRate"), TextBox)
            txtbox.Attributes.Add("onBlur", "calculateValues(this,""txtValue"")")
        End If
    End Sub


not that textbox control names here are txtRate and txtvalues, this script can be called on any of the html events instead of onBlur, the source control has to pass itself using the 'this' keyword, the secong argument of the function is the string name of the destination control for which u want to access the value

hope this will help people in a similiar scenario...







Similar Threads
Thread Thread Starter Forum Replies Last Post
Prefer Not To Use Data-Bound Controls... soundchaser59 ASP.NET 2.0 Basics 1 July 9th, 2007 09:13 PM
"Proper" way of updating bound controls and grids VBM2 Pro Visual Basic 2005 1 March 12th, 2007 09:36 AM
Bound Vs Unbound controls Richard_AU ADO.NET 1 January 16th, 2007 01:40 PM
Bound Controls cpanson ADO.NET 0 June 24th, 2006 07:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.