AmanMJ
AmanMJ

Reputation: 117

How to call function from textbox using asp.net?

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>

Upvotes: 2

Views: 982

Answers (1)

Jorrex
Jorrex

Reputation: 1500

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 :)

Upvotes: 1

Related Questions