how to add value inside JavaScript function? - javascript

error: linked button The server tag is not well formed.
I am using VB asp.net and JavaScript
I have a linked button inside repeater. When user click on remove button then I want to run onclick() function and pass in a eval() value.
after that I want to get the value inside javascript function.
Note, creating hidden field wont work because it is inside repeater and there is no way for me to get the value inside JavaScript function.
function RemoveItemJS(obj) {
alert("test" + obj);
}
vb asp.net repeater code:
<asp:Repeater ID="BagRepeater" runat="server">
<ItemTemplate>
<asp:HyperLink ID="ProductNameHL" NavigateUrl='<%# Eval("Product_ID", "../Shop/ShopDetail.aspx?Product_ID={0}") %>' runat="server">
<h4 class="Text-Spacing"><%# Eval("Name")%></h4>
</asp:HyperLink>
<h5><strong>$<%# Eval("Price") %></strong></h5>
<h5>Color: <%# Eval("Color") %></strong></h5>
<h5>Size: <%# Eval("Size") %></strong></h5>
<h5>QTY: <%# Eval("QTY") %></strong></h5>
<asp:LinkButton ID="RemoveItemLB" OnClientClick="RemoveItemJS('"+<%# Eval("Product_ID") %>+"');" runat="server"><span class="glyphicon glyphicon-remove"></span></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

Try with 'onclick'
<asp:LinkButton ID="RemoveItemLB" onclick='<%# "RemoveItemJS(" +Eval("Product_ID") + " );" %>' runat="server"><span class="glyphicon glyphicon-remove"></span></asp:LinkButton>

Follow the approach as below. I have tried this and it works.
You need to escape the quotes inside the value of onclientclick
property as in code below.
Note that double quotes are escaped with a backslash i.e. \
character and also the entire JavaScript code is emitted from
server-side tag.
Use & as string concatenation operator in VB.Net and not + within a server-side expression in markup.
OnClientClick='<%# "RemoveItemJS(\"" & Eval("Product_ID") & "\")" %>'

Related

ASP.NET GridView set values using JavaScript Fucntion

In the ItemTemplate of TemplateField of the GridView itis posible to set value using functions from CodeBehind. In the example I call getImgUrl and getNavUrl to set Image and Navigate URLs. I would like to know the syntax required to call a JavaScript function instead of a code behind function. That is, the combination of brackets, percent signs, hashtags, etc.
<asp:HyperLink ID="hHoldEm" Target="_parent" runat="server" Font-Underline="false" ImageUrl='<%# getImgUrl(Eval("CER_ACTIVE"))%>' ToolTip="Hold/UnHold..." ImageWidth="20" NavigateUrl='<%# getNavUrl(Eval("CER_ACTIVE"), Eval("CER_NO"))%>'/>
You cannot call a function in code behind with a HyperLink by adding a OnClick event as you would a Button.
But you can call javascript function in an anchor like this
Click for JavaScript
So to do the same in the GridView you can do this
<asp:HyperLink ID="hHoldEm" runat="server" NavigateUrl='<%# "javascript:" + getNavUrl(Eval("CER_ACTIVE"), Eval("CER_NO")) %>' />
or
<asp:HyperLink ID="hHoldEm" runat="server" NavigateUrl='<%# "javascript:myFunction('" + Eval("CER_ACTIVE") + "', '" + Eval("CER_NO") + "')" %>' />

How to bind server value in javascript onmousedown in aspx page

I have one Hyperlink in aspx page. I need to do two things
When I Navigate from mousedown I need to log some activity
While logging EmployeeID value I need to bind from server.
But i am facing error like server tags not properly formed.
Below is my code:
<asp:HyperLink id="hyperlink1"
NavigateUrl="#"
Text="EMPLOYEE ID"
onmousedown="javascript:record('User clicked Eployee ID=') +<%#fasteval("EmpID")%>
runat="server"/>
Note: record is my function which will log into database one thing i want to bind the EmpID value from my own column EmpID.
Can you please help me on this
You have unescaped double quote in double quote, use single quote for outer most quotes for onmousedown event handler.
<asp:HyperLink id="hyperlink1"
NavigateUrl="#"
Text="EMPLOYEE ID"
onmousedown='javascript:record("User clicked Eployee ID=") +<%#fasteval("EmpID")%>'
runat="server"/>
Use string.format
<asp:HyperLink id="hyperlink1"
NavigateUrl="#"
Text="EMPLOYEE ID"
onmousedown='<%# string.Format("javascript:record('User clicked Eployee ID= {0}')", Eval("EmpID")) %>'
runat="server"/>

getting error while sending querystring in on click javascript in hyperlink in .net

i have a hyperlink which should open a new window with querystring values. getting error saying . there is error in onclick property
server tag is not well formed
<asp:HyperLink Target="_blank" ID="Refname"
Text='<%#Eval("RefCount") %>' runat="server"
NavigateUrl=#
onClick='<%# "window.open('AssignedRequestsNew.aspx" +
"?RefId=" +
Server.UrlEncode(Eval("RefID").ToString())','name','height=550,
width=790,toolbar=no,directories=no,status=no,menubar=no,
scrollbars=yes,resizable=no'); return false;%>' />
i used this
<a id="HyperLink2"
runat="server"
NavigateUrl='#'
onclick="window.open('AssignedRequestsNew.aspx?RefId=','name','height=550, width=790,toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no'); return false;"
Target="_blank">
<%#Eval("RefCount") %>
</a>
compiling and opening new window, but unable to pass values through query string, can u help me to pass query string values
Your onClick value is indeed malformed. You have an inconsistent mix of single and double quotes.
Write a JavaScript function and call it on onclick like this:
<asp:HyperLink ID="hlDemo" runat="server" onclick="openNewWindows()"
style="cursor:pointer; text-decoration:underline;">HyperLink Demo
</asp:HyperLink>
The onClick function's quotes are messed up. This should work
<asp:HyperLink Target="_blank" ID="Refname" Text='<%#Eval("RefCount") %>' runat="server" NavigateUrl = '#' onClick='<%# "window.open('AssignedRequestsNew.aspx?RefId="& Server.UrlEncode(Eval('RefID').ToString()) & " ', name , height=550, width=790,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no'); return false; ')" %>' />

Passing variables to javascript in repeater imagebutton onclientclick

I want to pass my dynamic value to a javascript function from an imagebutton onclientclick event.
It works fine if I pass the static value like this
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('1')" />
However it doesn't work if I try to pass dynamic values as below.
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent" BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('"+<%#Eval("PujaType") %>+"')" />
I tried all possible combinations however it says tag is not well formatted.
Can anyone help on this?
Don't know ASP, but in JSP the expression has to be the entire value. Try:
OnClientClick="<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>" />
You don't need string concatenation around your Eval statement. Doing so is creating invalid markup.
Change
"javascript:return openWindow('"+<%#Eval("PujaType") %>+"')"
to
"javascript:return openWindow('<%#Eval("PujaType") %>')"
<asp:ImageButton
ImageUrl="~/Images/control_play_blue.png"ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('<%#Eval("PujaType") %>')" />
Its been awhile since I have worked with ASP.NET and currently cannot test this, but I seem to recall it requiring single quotes when wrapping the <%# %>.
Something like this:
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick='<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>' />
Some of the answers on this question suggest the same.
An alternative would be setting the value in the code behind. Assuming you are using this control inside a repeator or something similar, you should be able to set the value during data binding like this:
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
String pujaType = ((YourType)e.Item.DataItem).PujaType;
((ImageButton)e.Item.FindControl("btnChat")).OnClientClick = "javascript:return openWindow('" + pujaType + "')";
}
}
Repeater.ItemDataBound Event
Another alternative, since you are not making use of any serverside events, would be to use regular html instead of an ASP.NET control. This could be done with an anchor and img tag like so:
<a href="#" onclick="openWindow('<%# Eval("pujaType") %>'); return false;">
<img src='<%= ResolveUrl("~/Images/control_play_blue.png") %>' />
</a>

Passing Repeater Container.ItemIndex to a Javascript function

In C# Asp.Net I need to pass my repeater occurrence index into a Javascript function when OnClientClick is depressed from an ASP button. Here is my code
<asp:Button ID="btnGetNewQuote"
Text="Get New Quote"
class="pcbutton"
runat="server"
OnCommand="GetNewQuote"
CommandArgument ='<%# Container.ItemIndex %>'
OnClientClick="return getNewQuote(<%# Container.ItemIndex %>) />
If I hard code ....OnClientClick="return getNewQuote(0)"> it works and the JS gets invoked, but as soon as I put the # Container.ItemIndex # in there the JS gets overlooked and it just posts back to the code behind...
Passing the Container.ItemIndex into JS Functions works every where else on the page except this OnClientClick ?
does anyone know why this is and what I can do to get around it?
Try this
OnClientClick='<%# "return getNewQuote(" + Container.ItemIndex + ")" %>'
Instead of
OnClientClick="return getNewQuote(<%# Container.ItemIndex %>)
If we want to use in vb.net
OnClientClick='<%# "return getNewQuote(" & Container.ItemIndex & ")" %>'
otherwise it will give exception string is not in correct format

Categories