Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

programatically assigning an asp.net button a javascript function OnClientClick

Posted on 2013-01-15
6
Medium Priority
?
2,592 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Technology Partners: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

688 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