Adding evaluation value to the argument of an onclick JavaScript function in an ASP datalist (Front-End)

Can anyone help me with adding a JavaScript function to my checkbox within a datalist, where the ID of the control is passed as a parameter?

I would like to achieve this on an ASP page without using the OnItemDataBound method. I have tried the following code but it's not working. Any suggestions?

<asp:CheckBox ID='chkDatabase' runat="server"  CssClass="NormalBold" Text='<%# Eval("Name") %>' onclick='javascript:showme("<%# Eval("ID") %>")' ></asp:CheckBox>

This is part of the datalist code:

<asp:DataList runat="server" ID="ddlDatabases" CssClass="ddlDatabases" CellSpacing="0" CellPadding="0" RepeatLayout="Table" OnItemDataBound="ddlcount_ItemDataBound">
    <ItemTemplate>
        <div style=" float: left; width:150px;padding:20px; " >
            <asp:CheckBox ID='chkDatabase' runat="server"  CssClass="NormalBold" Text='<%# Eval("Name") %>' onclick='javascript:showme("<%# Eval("ID") %>")' >
            </asp:CheckBox>
        </div>
    </ItemTemplate>
</asp:DataList>

Thank you in advance for any assistance!

Answer №1

One way to incorporate the + operator within <%# ...%> is demonstrated in this example:

onclick='<%# displayID(" + Eval("ID") + ")"%>'>

Avoiding the use of javascript is recommended for simplicity.

Answer №2

It is not possible to pass parameters in JavaScript as shown.

Please remove the following from the markup:

onclick='javascript:showme("<%# Eval("ID") %>")'

To prevent data loss, add the following code in the DataList_OnItemDataBound event:

OnItemDataBound="DataList1_OnItemDataBound"

In the code behind:

protected void DataList1_OnItemDataBound(object sender, DataListItemEventArgs e)
{
   DataListItem row = e.Item;

  CheckBox chkDatabase = (CheckBox)row.FindControl("chkDatabase");

  chkDatabase.Attributes.Add("onclick", "showme('" + ID + "')");
}

Answer №3

functions incredibly well

<asp:GridView runat="server" ID="grdAdvTravel">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <a href="javascript:EditAdvanceTravelFee( '<%#Eval("fee_catg_srno")%>','<%#Eval("trvl_amt")%>','<%#Eval("pay_date")%>','<%#Eval("fee_name")%>','<%#Eval("status")%>' )" class="btn btn-primary">MODIFY</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After May 2015, LinkedIn oAuth1 began consistently displaying the message "Member xxx does not have permission to access company xxx."

We have successfully implemented a portal to fetch 'num-followers', however, after a recent LinkedIn upgrade, we are now encountering a 403 error message: <error> <status>403</status> <timestamp>1440149000543</time ...

One issue I've encountered is that Angularfire seems to have trouble reading the "facebook" property. So my question

I have been developing an android game using the ionic framework and firebase. My goal is to implement a feature where users can log in using Facebook login with Firebase, and then save the game data to the user's database key. The initial part of t ...

Guide on setting up a new NPM and JavaScript project within Visual Studio 2015

Whenever I watch tutorials, they always mention having a "special npm reference" that I seem to be missing. https://i.sstatic.net/I52dn.png All I can find are the "normal" references (.net assemblies). I also can't locate any project type labeled as ...

Error: Module cannot be found when using node.js local modules

I am currently working on incorporating local modules into my application Within the project root folder, I have created a folder named test with a file named index.js module.exports = { myFunction: function() { console.log('ok'); ...

Error occurred while initiating Angular frontend application with npm

https://i.sstatic.net/JCy3s.png > ng serve sh: ng: command not found npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ERR! errno ENOENT npm ERR! <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b2a ...

Obtaining data from a callback function within a NodeJS application

There is a function in my code that performs a backend call to retrieve an array of names. The function looks something like this: module.exports.getTxnList = function(index, callback) { ....some operations ..... .... callback(null, respon ...

Triggering the click event three times on a dynamically generated element

I am currently facing a simple issue. I am attempting to implement a click event on dynamically generated elements such as this: $(document).ready(function(){ $('ul').on('click', 'li.clickable', function() { conso ...

Communication within the intermediate scopes between the directive and the controller

In my AngularJS HTML code, I have the following: <div ng-controller="myController"> <div ng-grid="myGrid"></div> </div> The ngGrid component creates a table-like structure with a viewport, rows, and cells, each having their ow ...

I'm having trouble getting the aggregation of a child collection to work properly in MongoDB when attempting to apply the $count

Could someone help me troubleshoot my aggregate query? I'm trying to sum the count values for each beacon, but it keeps returning 0. Please let me know if you spot any mistakes in the query. Sample Data [ { ...

Discovering the droppable container that a draggable element is positioned within

Currently, I am utilizing jQuery UI for drag and drop functionality. My main goal is to determine which droppable area a draggable element has been placed in. Can anyone offer assistance? Below is the code I am working with: $(".draggable").draggable({ ...

Tips for solving errors in Microsoft JScript runtime: "The definition of '$' is not clear"

While trying to transfer my live site to Visual Studio 2010, I encountered an issue. After copying and pasting the code from Internet Explorer's view source into VS2010->Newwebsite, I ran into an error upon testing. Microsoft JScript runtime err ...

Firebase Storage Metadata Disappearing Act

Despite trying to add custom metadata to my image in Firebase, it doesn't seem to be accepting it. I expect to see my metadata listed under 'Other Metadata' after I upload the image. Currently, this is how it appears for me: https://i.sstat ...

Configuring React classes for compilation within Play

After incorporating a basic React class into my Play project at /app/assets/js: var Question = React.createClass({ render: function() { return (<div> <p>{this.props.content}</p> <br> <p>{this.props.an ...

Is it possible to send both props and a function within a single onClick event in React?

After spending hours searching for the right solution, I have yet to find it. Let me explain my situation clearly. I have an Image Carousel feature on my website that should open when a user clicks on an image. I have 5 images on the website, and when a us ...

Issue with button style in dropdown menu in Twitter Bootstrap

<nav class="nav-collapse user"> <div class="user-info pull-right"> <img src="http://placekitten.com/35/35" alt="User avatar"> <div class="btn-group"> ...

Is it better to store data individually in localStorage or combine it into one big string?

When it comes to keeping track of multiple tallies in localStorage, one question arises: Is it more efficient to store and retrieve several small data points individually or as one larger chunk? For example: localStorage.setItem('id1', tally1); ...

Looking for a Regular Expression Validation Pattern!

Is there anyone who can provide insight into how to establish this validation pattern? All sentences must start with a capital letter, and the message should conclude with a full stop. There should be no spelling errors. ...

Utilizing a JavaScript/jQuery/alternative library extension for transforming HTML into XML format

Are there any JavaScript libraries that can convert HTML to XML? Can these libraries handle both clean HTML and real-world malformed (or 'dirty') HTML? Is there a JavaScript library available for this task, or would I need to use a server-side ...

How to make divs occupy the entire space of a parent div?

I've been scouring the website for a solution, but nothing I've found has been helpful. I'm attempting to create a grid within a containing div where a default (and eventually user-specified) number of divs will fill up the space. I'm t ...

Can anyone explain how to successfully implement AJAX data in a PHP file?

I've been struggling to transfer data from my JavaScript file to a PHP variable. I've spent hours searching for solutions, but none seem to apply to my unique code. Here's the JavaScript code I'm working with... $(function(){ ...