How To Bind Javascript Function With Onclientclick Event With Eval?
Solution 1:
You can build the entire contents of OnClientClick
as a string within the code brackets and it will output like you're expecting.
<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit"
OnClientClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>' />
This is assuming LocationId is a valid number- there are no quote marks to wrap your value when it renders, so outputting something like msgDisp(hello);
is going to break. I don't know how to address that in this manner, so if you have to do that I would recommend setting OnClientClick
server side during the ItemDataBound
event. Here's what it would like where the parent is a Repeater
control.
protectedvoidnotesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
MyClass item = (MyClass)e.Item.DataItem;
LinkButton lbtnEdit = (LinkButton)e.Item.FindControl("lbtnEdit");
lbtnEdit.OnClientClick = string.Format("msgDisp('{0}');", item.LocationId);
}
Solution 2:
If you are getting your binding expression tags (<%# ... %>) rendered in the markup, it means your LinkButton is not initialized in a binding container. A binding container can be, as @lincolnk demonstrated, an Repeater or GridView item, a Calendar cell, etc. Also, you do not have to prefix your function call with "javascript:". The value of the OnClientClick property is rendered as the handler of the anchor's onclick event.
Solution 3:
Looked everywhere on the net. Everyone says use CodeBehind. See my solution, which works even when my datavalue has a single quote in it like O'Neal. This will not work if your data item contains doublequotes. But works for what I needed it to do which was pass in a person's name. Note the backslashes inside the alert call.
OnClientClick="<%#string.Format("alert(\"{0}\"); return false; ", Eval("NAME"))%>"**
Solution 4:
You can do like OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'
Solution 5:
I want to thank lincolnk for his answer. I'm currently helping to build a new social network for googam.com. I have been searching for a few days for a solution to view a user's profile, in a datalist, in a jquery modal dialog popup. Setting the linkbutton OnClientClick in the ItemDataBound event solved the problem of passing the user id to the JQuery function to open a acsx user control in the popup window.
jQuery(document).ready(function () {
var mydiv = jQuery("#mydialog").dialog({
autoOpen: false,
resizable: false,
modal: true,
width: '500',
height: '400'
}).css("font-size", "0.8em");
});
functionShowPopup(uid) {
var mydiv = jQuery("#mydialog")
//alert(uid)// Load the content using AJAX
mydiv.load('Profile.aspx?id=' + uid);
// Open the dialog
mydiv.dialog('open');
}
//////////////
ProtectedSub DataList1_ItemDataBound(ByVal sender AsObject, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem ThenDim imageControl = TryCast(e.Item.FindControl("Image1"), Image)
Dim Uid AsString = imageControl.ImageUrl
Dim ProfileBtn As LinkButton = TryCast(e.Item.FindControl("ProfileButton"), LinkButton)
ProfileBtn.OnClientClick = String.Format("ShowPopup('{0}');return false;", Uid)
EndIfEndSub
Post a Comment for "How To Bind Javascript Function With Onclientclick Event With Eval?"