How to write javascript coding in c # code?

I am creating text fields dynamically, so how to call the javascript function for the "onchange" event in the text field?

<script type="text/javascript">
    debugger;
    function myFunction() {
        var btn = document.getElementById('<%= temp.ClientID%>').value;
        var btntemp = document.getElementById('<%= txttemp2.ClientID%>').value;
        var val = parseInt(btn) + parseInt(btntemp);
        document.getElementById('<%= TextBox1.ClientID%>').value = val;
    }        
</script>
<asp:TextBox ID="temp" runat="server" onchange="myFunction()"></asp:TextBox>
   <asp:TextBox ID="txttemp2" runat="server" onchange="myFunction()"></asp:TextBox>

Here iam creates a dynamic text box.

Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1");
for (int i = 0; i < rowsCount; i++)
 {
   for (int j = 0; j < colsCount; j++)
    {
     TextBox tb = (TextBox)table.Rows[i + 1].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
     tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];

here iam gets the text field value of the first column

else if (j == 2)
 {
   int quantityText;
   TextBox quantity = (TextBox)table.Rows[i +1].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);

here iam gets the column value of the second column

else if (j == 3)
  {
    double rateText;
    TextBox rate = (TextBox)table.Rows[i + 1].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);

here iam generates text fields dynamically.

private void GenerateTable(int rowsCount)
 {
  Table table = new Table();
            table.ID = "Table1";
            PlaceHolder1.Controls.Add(table);
           for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                row.ID = "Row_" + i;
                else if (j < colsCount - 1)
                    {
                        TableCell cell = new TableCell();
                        TextBox tb = new TextBox();
                      tb.ID = "TextBoxRow_" + i + "Col_" + j;
                      cell.Controls.Add(tb);
                     row.Cells.Add(cell);
                    }
+1
source share
4 answers

I received my answer successfully, which is below. The script side should write as below

    <script type="text/javascript">
             function myFunction() {
                 var btn = document.getElementById('<%= TextBox1.ClientID%>').value;
                 var sum = [0, 1, 2]
                 for (var j = 0; j <= btn; j++) {
                     var elements = document.getElementsByClassName("sum"+j);
                     for (var i = 0, length = elements.length; i < length; i++) {
                         if (elements[i].value) {
                             sum[0] = parseInt(elements[0].value);
                             sum[1] = parseInt(elements[1].value);
                             sum[2] = parseInt(elements[2].value);
                         }
                         elements[2].value = sum[0] * sum[1];
                     }
                 }
             }        
    </script>
</head>
<body>
    <form id="form1" runat="server">
<div style="display:none">
    <asp:HiddenField ID="HiddenField2" runat="server" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div> </form>
</body>
</html>

and when creating dynamic text fields, you need to add a class to which we should get the values, and calculate it, and then show the result

private void GenerateTable(int rowsCount)
        {
            //ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
            TextBox1.Text = rowsCount.ToString();
            Table table = new Table();
            table.ID = "Table1";
            PlaceHolder1.Controls.Add(table);
            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                row.ID = "Row_" + i;
for (int j = 0; j < colsCount; j++)
                {
if (j < colsCount - 1)
                    {
                        TableCell cell = new TableCell();
                        TextBox tb = new TextBox();
if (j == 2)
                        {
                            tb.ID = "TextBoxRow_" + i + "Col_" + j;
                            tb.CssClass = "sum"+i;
                            tb.Attributes.Add("onchange", "myFunction();");
                        }
                        else if (j == 3)
                        {
                            tb.ID = "TextBoxRow_" + i + "Col_" + j;
                            tb.CssClass = "sum"+i;
                            tb.Attributes.Add("onchange", "myFunction();");
                        }
                        else if (j == 4)
                        {
                            tb.ID = "TextBoxRow_" + i + "Col_" + j;
                            tb.ReadOnly = true;
                            tb.CssClass = "sum"+i;
                        }
                        cell.Controls.Add(tb);                       
                        row.Cells.Add(cell);
                    }
table.Rows.Add(row);
            }  
                SetPreviousData(rowsCount, colsCount);    
            rowsCount++;
            ViewState["RowsCount"] = rowsCount;
        }
0
source

script, runat="server", . , script #, . :

<script runat="server">
    void textBox_Change(Object sender, EventArgs e) {
        TextBox1.Text = Int32.parse(temp.Text) +                      
            Int32.parse(txttemp2.Text)
    }
</script>
<asp:TextBox ID="temp" runat="server" ontextchanged="textBox_Change" autopostback="true"></asp:TextBox>
<asp:TextBox ID="txttemp2" runat="server" ontextchanged="textBox_Change" autopostback="true"></asp:TextBox>

. TextBox ontextchange="", , autopostback="true"... , TextBox.

jQuery javascript:

$(document).on("change","#temp,#txttemp2",myFunction);

. , , , . , :

 $(document).on("change",".waitingForChangeForMyFunction",myFunction);

, , :

TextBox tb = new TextBox();
tb.CssClass="waitingForChangeForMyFunction";
0

use this to call javascript function in code for

ClientScript.RegisterClientScriptBlock(myFunction(), "AlertMsg", "<script> 
                              alert('Inserted successfully');</script>", true)
0
source

you can use RegisterClientScriptBlock:

String scripts = "function myFunction(clientID) {
        var btn = document.getElementById('clientID').value;
        var btntemp = document.getElementById('clientID').value;
        var val = parseInt(btn) + parseInt(btntemp);
        document.getElementById('clientID').value = val;
    }     ";

ClientScript.RegisterClientScriptBlock(this.GetType(), 
               "CounterScript", scripts, true);    

for (int i = 0; i < rowsCount; i++)
 {
   for (int j = 0; j < colsCount; j++)
    {
     TextBox quantity = (TextBox)table.Rows[i +1].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);

     quantity .Attributes.Add("onchange", "jsFunc('TextBoxRow_'" + i + "Col_" + j + "')");
    }

}   
0
source

All Articles