0

I have a Textbox in GridView I want to change the Label.Text if the text in Textbox is changed. But I am not able call javascript function written in .cs page. Is there any mistake in TbUnitCost_TextChanged method.

This is my aspx page

<ItemTemplate>
  <asp:TextBox Style="text-align: right" ID="TbUnitCost" runat="server" Width="80px" Text='<%#Bind("Unit_Cost")%>' AutoPostBack="true" OnTextChanged="TbUnitCost_TextChanged"  TextMode="Number"></asp:TextBox>
 </ItemTemplate>

Code behind Page is as follows

 protected void TbUnitCost_TextChanged (object sender, EventArgs e)
    {
        GridViewRow currentRow = (GridViewRow)((TextBox)sender).Parent.Parent;
        int rowNo = currentRow.RowIndex;
        string gridName = "GridWorkExpenses";
        TextBox tbunitCost = (TextBox)currentRow.FindControl("TbUnitCost");
        int row = Convert.ToInt32(tbunitCost.Text);

        tbunitCost.Attributes.Add("onchange","javascript:calculateTotalCost('"+rowNo+"','"+gridName+"');");

    }

and javascript Function is:

<script type ="text/javascript">
    function calculateTotalCost(rowNo, GridName) {                        

        if (GridName== 'GridWorkExpenses') {
            var rowscount = document.getElementById('<%=GridWorkExpensesSheet.ClientID%>').rows.length;
            return calculateTotalUnitCost("GridWorkExpensesSheet", rowscount,rowNo);
        }
   }
 </script>     
3
  • Not clear.. What exactly you want to achieve? do you want to change it from client side or server side? You have set AutoPostBack="true" , that means page will be submitted to server , I guess on events like OnItemDataBound you can attach the javascript call, make AutoPostBack="false" Commented Feb 23, 2016 at 6:16
  • Can't I call javascript function using Textbox.Attributes.Add() method? Commented Feb 23, 2016 at 6:18
  • Why are you calling it from server side? You can do it on Js onchange event. Commented Feb 23, 2016 at 7:06

3 Answers 3

2

Try using this:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "CalculateTotalCost", true);

Describe any further problem (if there is any).

Sign up to request clarification or add additional context in comments.

Comments

0
ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "<script type='text/javascript'>CalculateTotalCost</script>", false); 

is another method

This is a very old discussion [1]:Difference between RegisterStartupScript and RegisterClientScriptBlock?

Please refer this too.

Comments

0

hi you must write all of command in the one string

 ScriptManager.RegisterClientScriptBlock((sender as Control), GetType(), "jQueryCommand", "$('.MenuLoginLink').hide();$('.MenuUsername').show();$('.MenuUsername').text('" + Currentmember.Mobile  + "');$('.MenuExit').show();" +
                " $('.btnLoginInFriends').hide();$('#HiddenSession').val('" + int.Parse(Session["CurrentUserID"].ToString()) + "');", true);

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.