Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

programatically assigning an asp.net button a javascript function OnClientClick

Posted on 2013-01-15
6
Medium Priority
?
2,622 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Industry Leaders: 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

877 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