Solved

calling js from c#

Posted on 2016-09-20
16
34 Views
Last Modified: 2016-09-21
Hi,

I have two questions regarding this topic.

1. I am trying to call a js function from c#, this is my test code, but it is not working
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="test2.aspx.cs" Inherits="_test2" %>


<form runat="server" name="form1">

<SELECT multiple size="7" name="testCategory" > 
			      <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
                  <option value="audi">Audi</option>
			</SELECT> 
			    			
			    			<asp:Button id="testbtn" runat="server" text="test call" />
			    			
<SELECT  CLASS="AdHoc_Form_RIGHT" multiple size=17 name=mainlist></SELECT> 			    			
			    			</form>
			    			
			    			


        
<SCRIPT>   
    function testfuntion() {
        alert("here");
    }
</script>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void testbtn_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(GetType(), "javascript", "javascript:testfunction();", true); 
        //scriptManager.RegisterClientScriptBlock(this, GetType(), "hello", "testfunction);", true);
    }

}

Open in new window



2. This is the code I use in classic ASP when calling a js function, Is there a way to use it in .net?  thanks
<INPUT onclick="AddListElements(this.form,this.form.testCategory)" type=button value=">>">

Open in new window

0
Comment
Question by:mcrmg
16 Comments
 
LVL 4

Expert Comment

by:Steven Kribbe
Comment Utility
In the HTML your function is called testfuntion so ....
0
 

Author Comment

by:mcrmg
Comment Utility
duh.....I fixed the function name, but the alert is not showing..thanks
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
    protected void Page_Load(object sender, EventArgs e)
    {

	this.testbtn.Attributes.Add("onClick", "testfunction()");
    }

Open in new window

0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 100 total points
Comment Utility
i dont think u need javascript in front

        ClientScript.RegisterClientScriptBlock(this, typeof(string), "some unique string", "testfunction();", true);

Open in new window

0
 

Author Comment

by:mcrmg
Comment Utility
Both ways work fine....Thank you.

How can I pass the form element in c#?  thanks

<INPUT onclick="AddListElements(this.form,this.form.testCategory)" type=button value=">>">
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
why are you using client-side control instead of server-side?
0
 

Author Comment

by:mcrmg
Comment Utility
Is it possible that you could shed some light?  I just recently started .net programming, I used to do classis ASP..thanks
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
some of your controls are server-side (the ones that have runat="server" attribute), and some are client-side. if you are using .NET, you should have all server-side controls, unless there is a specific reason not ot
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 400 total points
Comment Utility
one way would be to do somrthing like that:

 <form runat="server" name="form1">
    <SELECT multiple size="7" id="testCategory" name="testCategory" runat="server"> 
			      <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="mercedes">Mercedes</option>
      
                  <option value="audi">Audi</option>
			</SELECT> 
			    			
			    			<asp:Button id="testbtn" runat="server" text="test call" />
			    			
<SELECT  CLASS="AdHoc_Form_RIGHT" multiple size=17 name="mainlist" runat="server"></SELECT> 			    			
			    			
</form>
			    		

Open in new window


another would be to use .NET server-side controls

 <form runat="server" name="form1">
         <asp:ListBox ID="testCategory" runat="server" SelectionMode="Multiple">

            <asp:ListItem Value="Volvo">Volvo</asp:ListItem>
             <asp:ListItem Value="Saab">Saab</asp:ListItem>
             <asp:ListItem Value="Mercedes">Mercedes</asp:ListItem>
             <asp:ListItem Value="Audi">Audi</asp:ListItem>
        </asp:ListBox>
  <asp:Button id="testbtn" runat="server" text="test call" />
        <asp:ListBox ID="mainlist" runat="server" CssClass="AdHoc_Form_RIGHT" SelectionMode="Multiple">

        </asp:ListBox>
			    						    			
			    			
</form>
			    		

Open in new window

0
 

Author Comment

by:mcrmg
Comment Utility
I see..thank you.   What gave me hard time is I have code that I used in ASP and now I want to modify so I can use it in .net.   I have tough time making js work in .NET. (ex. passing form element to js)  thanks
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
you could just add runat="server" to your controls so that server sees them. then you can access any control by the ID

this.controlID.Text

Open in new window


if you want to access listbox selected value, use this.testcategory.Selectedvalue
0
 

Author Comment

by:mcrmg
Comment Utility
This is what I have so far....it does not work, when I removed call to testfunction(), for some reason, it is still calling the function testfunction()

Please let me know if EEs think it is better to create a new question.  thanks


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="test2.aspx.cs" Inherits="_test2" %>


 <form id="Form1" runat="server" name="form1">
         <asp:ListBox ID="testCategory" runat="server" SelectionMode="Multiple">

            <asp:ListItem Value="Volvo">Volvo</asp:ListItem>
             <asp:ListItem Value="Saab">Saab</asp:ListItem>
             <asp:ListItem Value="Mercedes">Mercedes</asp:ListItem>
             <asp:ListItem Value="Audi">Audi</asp:ListItem>
        </asp:ListBox>
  <asp:Button id="testbtn" runat="server" text="test call" />
        <asp:ListBox ID="mainlist" runat="server" CssClass="AdHoc_Form_RIGHT" SelectionMode="Multiple">

        </asp:ListBox>
			    						    			
			    			
</form>
		
			    			
			    			


        
<SCRIPT>
    function testfunction() {
        alert("here");
    }

    function AddListElements(frm, list) {
        alert("there");
        alert(list.length);
        if (list.length != 0) {
            var eArray = new Array();
            var tArray = new Array();
            //looks for all the selects on the left side that contains selected items
            for (var j = list.length - 1; j >= 0; j--) {
                value = list.options[j].value;
                text = list.options[j].text;
                eArray[countElements] = value;
                tArray[countElements] = text;
                if (!isInArray(value, lArray)) {
                    lArray[countElements][0] = list.options[j].value;
                    lArray[countElements][1] = list.name;
                    lArray[countElements][2] = 1;
                    lArray[countElements][3] = list.options[j].text;
                }
                RemoveOption(list, j);
                DecrementIndexes(list.name);
                countElements++;
            }
            for (var j = eArray.length - 1; j >= 0; j--) {
                if (eArray[j] != null) {
                    AddOption(frm.mainlist, eArray[j], tArray[j]);
                    eArray[j] = null;
                    tArray[j] = null;
                    IncrementIndexes("mainlist");
                }
            }
        }
    }    
</script>

Open in new window



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.testbtn.Attributes.Add("onClick", "testfunction()");

    }

    protected void testbtn_Click(object sender, EventArgs e)
    {
       
        //ClientScript.RegisterClientScriptBlock(GetType(), "javascript", "testfunction();", true);

        ClientScript.RegisterClientScriptBlock(GetType(), "javascript", "AddListElements(this.form1,this.form1.Controls.Find('testCategory',true));", true); 



        


        


        //scriptManager.RegisterClientScriptBlock(this, GetType(), "hello", "testfunction);", true);
    }

}

Open in new window

0
 
LVL 35

Accepted Solution

by:
YZlat earned 400 total points
Comment Utility
If you are using server-side scripting (ASP.NET/C#), then you do not need client-side scripting (javascript).

this line: ClientScript.RegisterClientScriptBlock(GetType(), "javascript", "AddListElements(this.form1,this.form1.Controls.Find('testCategory',true));", true);  is not needed then

What are you trying to do when the user clicks a button?
0
 

Author Comment

by:mcrmg
Comment Utility
This is what I am trying to accomplish. When the button is clicked, all items on the left will be moved to the right hand side
 <asp:ListBox ID="mainlist" runat="server" CssClass="AdHoc_Form_RIGHT" SelectionMode="Multiple">
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
again, you do not need javascript, you can do that in your testBtn_Click event

 protected void testbtn_Click(object sender, EventArgs e)
        {
            

            foreach (ListItem li in testCategory.Items)
            {
                
                this.mainlist.Items.Add(li);
                
            }
            testCategory.Items.Clear();
            
        }

Open in new window

0
 

Author Closing Comment

by:mcrmg
Comment Utility
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now