Solved

programatically assigning an asp.net button a javascript function OnClientClick

Posted on 2013-01-15
6
2,375 Views
Last Modified: 2013-01-15
I want to programatically assign a javascript function to the OnClientClick of an asp.net button, but the function accept two parameters, CBL_2 and cblError1.

However the first parameter name is dependent on what has come before so it could be CBL_1 to CBL_9, how can I assign this function call BUT dynamically change the first parameter name. Should be simple but the solution has eluded me!!

Thanks in advance guys.

AddB.OnClientClick = "return CheckListValidation('CBL_2','cblError1');";
0
Comment
Question by:Paulmc999
  • 3
  • 3
6 Comments
 
LVL 21

Expert Comment

by:masterpass
ID: 38780566
try something like this

AddB.OnClientClick = "return CheckListValidation('"+dynamicvaluehere+"','cblError1');"; 

Open in new window

0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38780597
Nope I had already tried that this is what I get in 'view code'

It doesn't work so I am presuming it should not look like this??

onclick="return CheckListValidation('CBL_'2','cblError1')
0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38780614
This is what it looks like if I just use a non-dynamic parameter name, so this is what it should look like after too.

onclick="return CheckListValidation('CBL_2','cblError1')
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 21

Expert Comment

by:masterpass
ID: 38780636
In this markup :

onclick="return CheckListValidation('CBL_'2','cblError1')

you are having an extra single quote between CBL_2. Can you check that ? Or can you post the code behind ?
0
 
LVL 21

Accepted Solution

by:
masterpass earned 500 total points
ID: 38780641
This is a small test that I created. It works.

    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="testButton" Text="click me!" />
    </div>
    <script type="text/javascript">
        function testFunction(val) {
            alert(val);
            return false;
        }
    </script>
    </form>

protected void Page_Load(object sender, EventArgs e)
{
     string CLB_2 = "hello world!";
     testButton.OnClientClick = "return testFunction('" + CLB_2 + "');";
}

Open in new window

0
 
LVL 2

Author Comment

by:Paulmc999
ID: 38780709
Yes you are right I had an extra single quote in solution you gave me. Thanks, now it works.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

20 Experts available now in Live!

Get 1:1 Help Now