programatically assigning an asp.net button a javascript function OnClientClick

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');";
LVL 2
Paulmc999Asked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
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
 
masterpassCommented:
try something like this

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

Open in new window

0
 
Paulmc999Author Commented:
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(&#39;CBL_&#39;2&#39;,&#39;cblError1&#39;)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Paulmc999Author Commented:
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(&#39;CBL_2&#39;,&#39;cblError1&#39;)
0
 
masterpassCommented:
In this markup :

onclick="return CheckListValidation(&#39;CBL_&#39;2&#39;,&#39;cblError1&#39;)

you are having an extra single quote between CBL_2. Can you check that ? Or can you post the code behind ?
0
 
Paulmc999Author Commented:
Yes you are right I had an extra single quote in solution you gave me. Thanks, now it works.
0
All Courses

From novice to tech pro — start learning today.