Solved

calling js from c#

Posted on 2016-09-20
16
37 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
crm development 2 65
Multiple file Upload asp.net 2 36
How to access a DataBinder.Eval variable using C# ? 4 27
IIS Authorization for Web Service 2 19
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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

14 Experts available now in Live!

Get 1:1 Help Now