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>     
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
ashT
  • 13
  • 1
  • 7
  • 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" – Satyaki Chatterjee Feb 23 '16 at 06:16
  • Can't I call javascript function using Textbox.Attributes.Add() method? – ashT Feb 23 '16 at 06:18
  • Why are you calling it from server side? You can do it on Js onchange event. – Vivek Mishra Feb 23 '16 at 07:06

3 Answers3

2

Try using this:

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

Describe any further problem (if there is any).

TalhaAhmed
  • 60
  • 1
  • 7
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.

Community
  • 1
  • 1
Bhavana
  • 311
  • 2
  • 12
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);
pst
  • 59
  • 1
  • 2
  • 10