Solved

calling js from c#

Posted on 2016-09-20
16
49 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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