2014年7月13日 星期日

[RESOLVED] Get textbox control inside gridview from Javascript function


Hi, I have a gridview that allows row editing and one of the column (Column 1) contains a button to call a javascript function. The function will output 2 values which I need to place it inside the txtAddress_Update_XCoord  (Column 2) and txtAddress_Update_XCoord
 (Column 3) of the same gridview editing row but not sure how to do this. Please kindly advice. Thanks!


function calculateCoordinates() {

// Perform calculation and place the values in txtAddress_Update_XCoord and txtAddress_Update_YCoord of the same gridview editing row.

}

ItemStyle-HorizontalAlign="Center" SortExpression="Postal">

Postal







CssClass="field-validation-error"
ControlToValidate="txtAddress_Update_Postal"
Display="Dynamic"
SetFocusOnError="true"
ValidationExpression="^[0-9']{6,10}$"
EnableClientScript="true"
ValidationGroup="Update" />





ItemStyle-HorizontalAlign="Center">

X-Coordinate











ItemStyle-HorizontalAlign="Center">

Y-Coordinate













Add css classes to the textboxes in the edit templates so that you use class name for searching


<asp:TextBox ID="txtAddress_Update_XCoord" CssClass="coordinates update-x" runat="server">asp:TextBox>
...
<asp:TextBox ID="txtAddress_Update_YCoord" CssClass="coordinates update-y" runat="server">asp:TextBox>

Pass the source to the javascript function


<input id="btnCalculateCoordinatesEdit" type="button" value="Calculate Coordinates" onclick="calculateCoordinates(this);" class="btn gray" />

In the javascript function, you can use the source to get to the textboxes.


function calculateCoordinates(src) {
    //get the fields
    var y = jQuery(src).parents('tr').find('.coordinates').filter('.update-y');
    var x = jQuery(src).parents('tr').find('.coordinates').filter('.update-x');

    //get their values and processes them
    var xValue = x.val() * 10;
    var yValue = y.val() * 10;

    //update textboxes with the new values
    x.val(xValue);
    y.val(yValue);
}



沒有留言:

張貼留言