Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

calling js from c#

Posted on 2016-09-20
16
Medium Priority
?
58 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
Technology Partners: 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 400 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 1600 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 1600 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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