How to bind server value in javascript onmousedown in aspx page - javascript

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"/>

Related

how to add value inside JavaScript function?

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") & "\")" %>'

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") + "')" %>' />

ASP.NET HyperLink Eval in a Javascript function

I've added a HyperLink control to a GridView within a TemplateField and I want it to redirect the user to a specific page, passing the ID from the selected row. The following works just fine:
<asp:HyperLink ID="hlViewBasketItem" runat="server" Text="Detail"
NavigateUrl='<%# Eval("BasketItemID", "/BasketItemDetail.aspx?popUp=true&id={0}") %>'>
</asp:HyperLink>
The ID evaluates correctly; for example: "/BasketItemDetail.aspx?popUp=true&id=52"
The problem I have is that I need to make a call to a Javascript function to control the display of my page (so that it appears within a popup). I've tried the following:
<asp:HyperLink ID="hlViewBasketItem" runat="server" Text="Detail"
NavigateUrl='<%# Eval("BasketItemID", "javascript:dnnModal.show('/BasketItemDetail.aspx?bid={0}&popUp=true',false,550,950,false)") %>'>
</asp:HyperLink>
...but I'm getting a "server tag is not well formed" error.
Is there a actually a way to do this?
This was the answer:
NavigateUrl=<%# "Javascript:dnnModal.show('BasketItemDetail.aspx?popUp=true&bid=" + DataBinder.Eval(Container.DataItem, "BasketItemID").ToString() + "')"%>
I can go to bed now :)
rather than using asp.net hyperlink add use html anchor tag and call java script function as follows:
//javascript function
<script>
function openPop(drp) {
window.open('/BasketItemDetail.aspx?bid='+drp+'&popUp=true', false, 550, 950, false);
}
</script>
// Anchor tag
Click Here

pass the id of textbox to dropdownliast onchange event in javascript

I have a dropdownlist defined:
<asp:DropDownList Width="300px" ID="PlaceHoldersDropDownList" runat="server"
AppendDataBoundItems="True" TabIndex="3" onchange="PasteTextInEditor
(this.options[this.selectedIndex].value), '<%=
SubjectTextBox.ClientID %>'" >
So, I pass the selected text and a textbox ID to a javascript function. The text box is defined as:
<asp:TextBox Width="660px" ID="SubjectTextBox" Text='<%# Bind( "Subject") %>'
runat="server" TabIndex="4" MaxLength="100">
</asp:TextBox>
So, in script function, when i put alert like this :
alert(text); //shows selected value
alert(editor); // shows undefined
So editor value is turning up undefined. So, can you please let me know the mistake I've been doing. I would like to save that value selected from dropdown into textbox.Please help me fixing th eproblem. Thank You
It looks like your bracket is in the wrong place in your "onchange": you're only passing one parameter and not the client id.
But regardless, the ClientID is not going to be parsed here. Instead, I suggest you add your onclick in the code behind, either in your Page_Load or Page_PreRender:
PlaceHoldersDropDownList.Attributes.Add("onchange", "PasteTextInEditor(this.options[this.selectedIndex].value, '" + SubjectTextBox.ClientID + "');")

Javascript to back button

I have one GridView with Employee search results in it.
This GridView shows results of EmpNo, EmpName, Salary. For each EmpNo cell in the GridView, there is a link to ManageEmployee.aspx page.
Till here okay, no problem.
In ManageEmployee.aspx page there are two buttons 1.Update, 2.Cancel
When user clicks on Cancel button, the page should navigate to Employee results page.
Can anybody give suggestion how to do this?
Add in a HyperLink field in a TemplateField. If you pass the search term to the details page as ~/Details.aspx?query=John%20Smith this will make a URL that is ~/SearchResults.aspx?query=John%20Smith.
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# String.Format("~/SearchResults.aspx?query={0}", Request["query"]) %>'>Cancel</asp:HyperLink>)
</ItemTemplate>
</asp:TemplateField>
If you want JavaScript (be careful about postbacks)
<asp:TemplateField HeaderText="">
<ItemTemplate>
Cancel
</ItemTemplate>
</asp:TemplateField>
If you have troubles with postbacks you will probably need to add in a direct URL like the HyperLink version above, or simply a window.location=".." version instead of history.go().
if you are looking for navigation using ASP.Net you can use
Response.Redirect("Your URL")
Example on how to use it - MSDN
Use the onclick attribute:
onclick="javascript:window.location.href='/relative/path/to/employeeResults.aspx'"

Categories