Solved

programatically assigning an asp.net button a javascript function OnClientClick

Posted on 2013-01-15
6
2,533 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Suggested Courses

623 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