read Javascript File from .net

460
9
Jump to solution
04-02-2020 07:00 AM
jaykapalczynski
Frequent Contributor

Having issues here.....I have a webform .net C# application that I am trying to inject some code....

I just want to show that this works...I am simply trying to click an asp button and call a function in JavaScript

Please help

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="xx.Edit" MaintainScrollPositionOnPostback="true" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <script src="https://js.arcgis.com/4.14/" type="text/javascript"></script>
    <script src="../js/index.js" type="text/javascript"></script>

<!--  SNIP  -->

     <asp:Button ID="btnGISPush" runat="server" OnClientClick="btnGISPush_Click()" Text="GIS Push"  CausesValidation="true" /><br />
‍‍‍‍‍‍‍‍‍‍‍‍‍

require([
    "esri/Map",
    "esri/views/MapView",
    "esri/layers/FeatureLayer"
], function (Map, MapView,FeatureLayer) {

        function btnGISPush_Click() {
            console.log("fggfdgfdgdfg");
            //document.getElementById('<%= btnGISPush.ClientID %>').click();
            alert("somrtindfgdfg");
        }
});
0 Kudos
1 Solution

Accepted Solutions
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi jay kapalczynski‌,

You are getting there. I just give you a simplest way to solve. (There are many other ways as well)

Firstly, you shall create an object, below sample is static object store into JsController in window scope.

 require(["esri/Map",
                "esri/views/MapView",
            "esri/layers/FeatureLayer"],
           function (Map, MapView, FeatureLayer) {
               "use strict";
               window.JsController = {
                   btnGISPush_Click : function () {
                            console.log("fggfdgfdgdfg");
                            
                            alert("somrtindfgdfg");
                        }
               };// js object block end
               
            });

And call the method from that object

<asp:Button ID="btnGISPush" runat="server" OnClientClick="window.JsController.btnGISPush_Click()" Text="GIS Push" /><br />

View solution in original post

0 Kudos
9 Replies
jaykapalczynski
Frequent Contributor

I can get this to work from within the asp page with a couple script tags at the top

but I want to call the JavaScript page because I have most of my code there.

<!-- IN THE ASP PAGE  -->

<script>
function btnGISPush_Click() {
  alert("somrtindfgdfg");
}
</script>‍‍‍‍‍‍‍
0 Kudos
MichaelBranscomb
Esri Frequent Contributor

Hi,

I've moved your post to the forum for ArcGIS API for JavaScript | ArcGIS for Developers (Latest) .

Thanks

Mike

0 Kudos
jaykapalczynski
Frequent Contributor

any have any thoughts....I simply want to register a JS page in .net and then have an asp button that calls the JS page and have the JS page run some code.

0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi jay kapalczynski‌,

You are getting there. I just give you a simplest way to solve. (There are many other ways as well)

Firstly, you shall create an object, below sample is static object store into JsController in window scope.

 require(["esri/Map",
                "esri/views/MapView",
            "esri/layers/FeatureLayer"],
           function (Map, MapView, FeatureLayer) {
               "use strict";
               window.JsController = {
                   btnGISPush_Click : function () {
                            console.log("fggfdgfdgdfg");
                            
                            alert("somrtindfgdfg");
                        }
               };// js object block end
               
            });

And call the method from that object

<asp:Button ID="btnGISPush" runat="server" OnClientClick="window.JsController.btnGISPush_Click()" Text="GIS Push" /><br />
0 Kudos
jaykapalczynski
Frequent Contributor

awesome...thank you 

0 Kudos
jaykapalczynski
Frequent Contributor

Last question....can I still get a value of a text box like this and set it to a variable?

const distanceUnit = document.getElementById("distance-select");

if not how do I get the value of an element

0 Kudos
jaykapalczynski
Frequent Contributor

I did it this way....but trying to figure out if I can do this easier.....

Is there an easier or shorter way to grab a textbox value and WRITE back to another textbox?

I am reading it now but would liketo write back results to another textbox on the aspx page

I made the textbox and button in .aspx page

I added the RegisterStartupScript line to the Page_Load

Made the window.JsController wrap with my function...

.ASPX page

            <asp:TextBox ID="txtAddressCall" Width="800px" Height="60px" MaxLength="300" runat="server" TextMode="MultiLine" Wrap="true" Enabled="false" Visible="false"></asp:TextBox><br />
            
            <asp:Button ID="btnGISPush" runat="server" OnClientClick="window.JsController.btnGISPush_Click()" Text="GIS Push"  CausesValidation="true" /><br />

ASPX.CS Page

protected void Page_Load(object sender, EventArgs e)
  {
                
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "JsVariables", "var txtboxClientId='" + txtAddressCall.ClientID + "';", true);

SNIP

JS Page

        window.JsController = {
            btnGISPush_Click: function () {

                var name = document.getElementById(txtboxClientId).value;
                alert(name);

            }
        };// js object block end
0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi jay kapalczynski‌,

You can try this way.

Register keyup event in your pageload event like below code.

You btnGISPushClick shall have clientId value param in it.

And you can do what you need with document.getElementById(param).value = .......;

txtAddressCall.Attributes.Add("onKeyUp", "window.JsController.btnGISPushClick('"+[youranotherTextBox.ClientID]+"');");
0 Kudos
jaykapalczynski
Frequent Contributor

What keyup event?

I need more than one variable set...

Where does this go?

txtAddressCall.Attributes.Add("onKeyUp", "window.JsController.btnGISPushClick('"+[youranotherTextBox.ClientID]+"');");

I want to create two different variables values from a C# page
protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "JsVariables", "var JSONstring='" + txtInfo.ClientID + "';", true);
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "JsVariables", "var teststring='" + TextBox1.ClientID + "';", true);
}‍‍‍‍‍

    window.JsController = {
        btnGISPush_Click: function () {

            // Capture String value to populate params DEFINED in the demo.aspx.cs page
            // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "JsVariables", "var JSONstring='" + txtInfo.ClientID + "';", true);
            var dictstring = document.getElementById(JSONstring).value;
            //var dictstring2 = document.getElementById(mutiaddressinput).value;
            alert("1: " + dictstring);

            var dictstring22 = document.getElementById(teststring).value;
            alert("2: " + dictstring22);

// SNIP‍‍‍‍‍‍‍‍‍‍‍‍‍

I get a return on JSONstring

teststring keeps coming up undefined

0 Kudos