0

I am trying to get value for txtQuantity on javascript :

 <asp:gridview ID="gvPOAdd" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="gvPOAdd_RowDeleting">
        <Columns>
  <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>

                <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("Quantity") %>' CausesValidation="false" Enabled='<%# ((Session["isNewPurchaseOrder"] != null) && ((bool)Session["isNewPurchaseOrder"] == true )) ? false : true %>'></asp:TextBox>
                <br />

            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="Unit Cost">
            <ItemTemplate>

                  <asp:TextBox ID="txtUnitCost" runat="server" CausesValidation="false" onkeyup="calculateTotalCharge(this,'<%= txtQuantity.clientid %>');" ></asp:TextBox>


            </ItemTemplate>
        </asp:TemplateField>
<asp:TemplateField HeaderText="Total Cost">
            <ItemTemplate>
                <div style="text-align:right;">
                    <asp:Label ID="lblTotalCost" runat="server"></asp:Label>
                </div>
            </ItemTemplate>

PS : onkeyup="calculateTotalCharge(this,'<%= txtQuantity.clientid %>');

and I am using the following script :

<script language="javascript" type="text/javascript">
        function calculateTotalCharge(unitcost,quantity) {               
            alert(unitcost.value);           
            alert(quantity.value);
        }

// But I get an error quantity is null ?!

how to access other textbox on same line On asp:gridview

2 Answers 2

2

If you are comfortable with Jquery then Change onkeyup to this onkeyup="calculateTotalCharge(this);"

function calculateTotalCharge(obj)
{
    var vUnitCost = $(obj).val();
    var vQuantity = $(obj).parent().prev().find('input[type=text]').val();
    //alert('UnitCost: '+ vUnitCost + 'Quantity: ' + vQuantity);
}

Description:

  1. $(obj).parent() will return you parent object basically a <tr>
  2. Then .prev() will return you previous <tr> object.
  3. Inside that you can find and get the text box value .find('input[type=text]').val() within same row.
Sign up to request clarification or add additional context in comments.

9 Comments

not working with me ..error : TypeError: unitcost.parent is not a function
Please see my update code. There was a minor issue, it has been fixed now :)
this is work thanks .could you please tell me how to set next label value on same lint (next to unitcost ... is there opposite of prev() lilke next () ?!! var totalcost = $(unitcost).parent().next().find('Label').val();
Yeah there's a property named next() you can apply same.
what i should use instead of[ input[type=text]] because it is label ?!
|
1

I would use Rahul's way but just an alternative is below:

Use RowDataBound event of gridview as below.

 <asp:gridview ID="gvPOAdd" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="gvPOAdd_RowDeleting" OnRowDataBound="gvPOAdd_RowDataBound">

C#

    void gvPOAdd_RowDataBound(Object sender, GridViewRowEventArgs e)
    {

        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            if(e.Item.ItemType == ListItemType.Item)
            {
                TextBox txtQuantity= e.Row.FindControl("txtQuantity") as TextBox;
                TextBox txtUnitCost= e.Row.FindControl("txtUnitCost") as TextBox;

                txtUnitCost.Attributes.Add("onkeyup", "calculateTotalCharge(this,'" + txtQuantity.Text+ "');");
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.