Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
this source of the site:
function __submit(action)
{
	document.forms[0].comboChange.value=action;
        document.forms[0].submit();
}





one
two




three
four


I make a project and visit this webpage and set value of the combobox.after set value should run javascrript function __submit('1 or 2')but a can not execute this.
please help me to execute this.

What I have tried:

I want run a javascript function with input parameter.
Posted
Updated 10-Jul-17 2:50am
v2
Comments
F-ES Sitecore 10-Jul-17 7:16am    
How do you "visit the webpage"?
Andy Lanng 10-Jul-17 8:38am    
Can you add the source back in and make sure you "paste as source". Looks like the tags have been wiped out
mosifallah 10-Jul-17 10:55am    
I cant edit source of the page;
I want make a robot to fill this page; I use webbrowser companent in visual# and load page.i want run that function in the my program;thanks
Richard Deeming 10-Jul-17 13:14pm    
Andy wasn't referring to the source of the page. He was referring to your question, where the source code you pasted has been removed.

Click the green "Improve question" link, and add the missing source back in.

If you get the popup asking how you want to paste it, select "Code block". Otherwise, select the pasted code and click the "code" button in the editor toolbar.

If you don't get either option, then you'll need to manually HTML-encode the source, and wrap it in <pre>...</pre> tags.

Hi,

As I do not know, how you visit the page.
I will propose 2 solution to this problem.

First Solution:

You use only html and javascript.
In the onload event of the body tag you call the function for set value in your combobox and after the function for process this value.

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Run JS</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            function _submit()
            {
                var e = document.getElementById("ddlView");
                var id = e.options[e.selectedIndex].value;
                
                alert('Value selected is: ' + id); 
                
            }
            
            function visit()
            {
                document.getElementById("ddlView").value = '1'; 
            }
        </script>
    </head>
    <body onload="visit();_submit();">
        <div>
            <select id="ddlView">
                <option value="0" selected="selected">Select one item</option>
                <option value="1">Item 1</option>
                <option value="2">Item 2</option>
                <option value="3">Item 3</option>
                <option value="4">Item 4</option>
            </select>
            
        </div>
    </body>
</html>
 
Share this answer
 
Second Solution

You use html, javascript e C#.

In the html code, you will use the button to call the function to process the value.
This button will be hidden.

JavaScript
function _submit() 
{
    var e = document.getElementById("combobox");
    var id = e.options[e.selectedIndex].value;
    alert('Value selected is: ' + id);
}

<div>
        <asp:DropDownList ID="combobox" runat="server">
            <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
            <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
            <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
            <asp:ListItem Text="Item 4" Value="4"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="btnOCRunScript" style="visibility:hidden;"  runat="server" OnClientClick="_submit();" Text="RodarScript" />
    </div>


In the code-behind, in the page load event, you set the value, and then register the script to dynamically click the button on the html page.

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //set the item 3. value 3 
                this.combobox.SelectedIndex = 2;

                //script for click in button dynamically 
                string script = "document.getElementById(\"btnOCRunScript\").click();";

                //register script in html
                // Check to see if the startup script is already registered.
                if (!this.Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "runbutton"))
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "runbutton",script, true);
                }
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900