Solved

calling js from c#

Posted on 2016-09-20
16
45 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
ID: 41806629
In the HTML your function is called testfuntion so ....
0
 

Author Comment

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

Expert Comment

by:YZlat
ID: 41806676
    protected void Page_Load(object sender, EventArgs e)
    {

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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 100 total points
ID: 41806681
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
ID: 41806712
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
ID: 41807278
why are you using client-side control instead of server-side?
0
 

Author Comment

by:mcrmg
ID: 41807289
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
ID: 41807294
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
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 400 total points
ID: 41807301
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
ID: 41807322
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
ID: 41807334
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
ID: 41808799
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
ID: 41808822
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
ID: 41808829
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
ID: 41808921
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
ID: 41809024
thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

680 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