How to call function from textbox using asp.net?

AmanMJ

I have been trying to call the function formatDate(). I try put it the text = "" / value = "" but it doesn't return correctly.

How can I fix this?

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1 Runat="Server">
    <script type="text/javascript">
        function formatDate(date) {
            var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();
            year = year.toString().substr(2, 2);
            dateRecord = [year, month].join('/');
            return dateRecord
        }
    </script>

    <h2><asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>

    <table width="625" cellpadding="0" cellspacing="1" style="margin-left:180px;">
        <tr>
            <td>&nbsp;Report </td>
            <td><asp:Textbox id="txtReport" text="return formateDate(date)" Runat="Server" Width="250px" />
            </td>
        </tr>
    </table>
</asp:Content>
Jorrex

This might be the solution for your problem. I haven't tested it out, but I'm working with the same thing. I'm just doing this the other way around, using HTML inputs with jQuery and ASP Server side code to access those controls as well.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1 Runat="Server">

          <script type="text/javascript">

           function formatDate(date) {
                var d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();
                    year = year.toString().substr(2, 2);
                    dateRecord = [year, month].join('/');
                    $("#txtReport").val(dateRecord);
            }
        </script>

    <h2><asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>

        <table width="625" cellpadding="0" cellspacing="1" style="margin-left:180px;">
         <tr>
             <td>&nbsp;Report </td>
             <td><asp:Textbox id="txtReport" ClientIDMode="Static" Runat="Server" Width="250px" />

             </td>
         </tr>

       </table>
</asp:Content>

Instead of calling a function to place the text (which I don't even know if it is possible, could be), try placing the text with jQuery.

You might need to change your ASP Control to this:

<asp:Textbox id="txtReport" ClientIDMode="Static" Runat="Server" Width="250px"></asp:Textbox>

ClientIDMode will make sure that the ID assigned to your element is used, instead of a generated ID from ASP.

EDIT I just tested this and this is my code. It works without a problem, the only difference is that my "date" variable is hard-coded because I needed an example. I also included the link to jQuery and put the scripting code in the header. Is this what you wanted?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var date = "02/03/1994";
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();
        year = year.toString().substr(2, 2);
        var dateRecord = [year, month].join('/');


        //Placing your date with jQuery
        $(document).ready(function () {
            $("#txtReport").val(dateRecord + " - this was done with jQuery");
        });

        //Placing your date with pure Javascript
        window.addEventListener("load", function() {
            document.getElementById("txtReportJS").value = dateRecord + " - this was done with pure JS";
        }, false);
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <h2>
            <asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>

        <table width="625" cellpadding="0" cellspacing="1" style="margin-left: 180px;">
            <tr>
                <td>&nbsp;Report </td>
                <td>
                    <asp:TextBox ID="txtReport" ClientIDMode="Static" runat="Server" Width="250px" />
                    <asp:TextBox ID="txtReportJS" ClientIDMode="Static" runat="Server" Width="250px" />

                </td>
            </tr>

        </table>
    </form>
</body>
</html>


I edited my example with both jQuery and pure Javascript methods to set text to your TextBox ;) I hope this is what you need and if so, please mark as answer :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call function from textbox using asp.net?

From Dev

How to call javascript function from asp:textbox?

From Dev

How to call function from one page to another page in asp.net using c#

From Dev

How to call a codebehind function from javascript in asp.net?

From Dev

How to pass user input from ASP.NET TextBox to JavaScript function?

From Dev

How to call JS function in asp.net

From Dev

how to copy text from an asp:textbox to another asp:textbox in asp.net webfroms aspx

From Dev

How do I call a server side VB.net function from jquery in an asp.net form?

From Dev

How to call a backend C# function from front end Javascript function in ASP.NET

From Dev

How to call a backend C# function from front end Javascript function in ASP.NET

From Dev

Submit data from textbox to asp net mvc controller using ajax

From Dev

how to fix value of textbox using asp.net

From Dev

How to refresh UserControl or Call User control function from Parent serverside in ASP.Net C#?

From Dev

How do I call a JavaScript function from Asp.Net Code Behind?

From Dev

Call ASP.net function / method from div onclick

From Dev

Call JavaScript function from asp.net checkbox

From Dev

Call C# function from asp.net

From Dev

Call controller function from view! asp.net MVC

From Dev

How to call method of a controller from View without using ajax in ASP.NET MVC?

From Dev

Fill textbox from url query and call function

From Dev

How to call java script function with asp.net button?

From Dev

How to call javascript function before ConfirmButtonExtender in asp.net?

From Dev

Call a method from button click in ASP.net, using controller

From Dev

Dynamic Textbox Using Asp.net

From Dev

How to Upload Files using ajax call in asp.net?

From Dev

Call Javascript function from codebehind using vb.net

From Dev

How to call unsafe code from ASP.NET xproj

From Dev

How to call confirm message from code behind in asp.net?

From Dev

How to call confirm message from code behind in asp.net?

Related Related

  1. 1

    How to call function from textbox using asp.net?

  2. 2

    How to call javascript function from asp:textbox?

  3. 3

    How to call function from one page to another page in asp.net using c#

  4. 4

    How to call a codebehind function from javascript in asp.net?

  5. 5

    How to pass user input from ASP.NET TextBox to JavaScript function?

  6. 6

    How to call JS function in asp.net

  7. 7

    how to copy text from an asp:textbox to another asp:textbox in asp.net webfroms aspx

  8. 8

    How do I call a server side VB.net function from jquery in an asp.net form?

  9. 9

    How to call a backend C# function from front end Javascript function in ASP.NET

  10. 10

    How to call a backend C# function from front end Javascript function in ASP.NET

  11. 11

    Submit data from textbox to asp net mvc controller using ajax

  12. 12

    how to fix value of textbox using asp.net

  13. 13

    How to refresh UserControl or Call User control function from Parent serverside in ASP.Net C#?

  14. 14

    How do I call a JavaScript function from Asp.Net Code Behind?

  15. 15

    Call ASP.net function / method from div onclick

  16. 16

    Call JavaScript function from asp.net checkbox

  17. 17

    Call C# function from asp.net

  18. 18

    Call controller function from view! asp.net MVC

  19. 19

    How to call method of a controller from View without using ajax in ASP.NET MVC?

  20. 20

    Fill textbox from url query and call function

  21. 21

    How to call java script function with asp.net button?

  22. 22

    How to call javascript function before ConfirmButtonExtender in asp.net?

  23. 23

    Call a method from button click in ASP.net, using controller

  24. 24

    Dynamic Textbox Using Asp.net

  25. 25

    How to Upload Files using ajax call in asp.net?

  26. 26

    Call Javascript function from codebehind using vb.net

  27. 27

    How to call unsafe code from ASP.NET xproj

  28. 28

    How to call confirm message from code behind in asp.net?

  29. 29

    How to call confirm message from code behind in asp.net?

HotTag

Archive