In href, ~ is not working when binding from code behind - asp.net

When binding href tag from code behind it's not working. This is my aspx code. When I am directly putting <a> tag on aspx it's working whereas as soon as I doing it through Response.write it's reading ~ as such not as a root directory.
<% Response.Write("<a id=\"A1\" runat=\"server\" class=\"menuItem\" href=\"~/HR/Emp/EmpDetails.aspx?mid=167\">My PIPs</a> "); %>
<a id="A1" runat="server" class="menuItem" href="~/HR/Emp/EmpDetails.aspx?mid=167">My PIPs</a>
binding through literal also it's not working
Literal.text = "<a id=\"A1\" runat=\"server\" class=\"menuItem\" href=\"~/HR/Emp/EmpDetails.aspx?mid=167\">My PIPs</a> ";
Please help on this.
these are the HTML tags
<ul id="jMenu">
<li>
<a runat="server" class="fNiv" href="~\Default.aspx?mid=1&">Home</a>
</li>
<li>
<a runat="server" class="fNiv" href="">Human Resources</a>
<ul>
<li class="arrow" />
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\PIPWorkFlow.aspx?mid=166">Performance Improvement Plan (PIP)</a>
<ul>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\PIPWorkFlow.aspx?mid=171">Data Protection Statement</a>
</li>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\EmpPIPDetails.aspx?mid=167">My PIPs</a>
</li>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\PIPHome.aspx?mid=168">Team PIPs</a>
</li>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\HRPIPHistory.aspx?mid=169">Employee PIPs</a>
</li>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\AppraisalRatingUpload.aspx?mid=170">Upload Appraisal Ratings</a>
</li>
<li>
<a runat="server" class="menuItem" href="~\HR\EMP\PIPRoleAssignment.aspx?mid=472">PIP Spoc Role Assignment</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a runat="server" class="fNiv" href="~\ContactUs.aspx?mid=39">Contact Us</a>
</li>
</ul>
these are the html tags... if we are binding these tags with Literal or div or TD ... they are reading ~ sign as such...
if I am putting these tags directly in HTML its working perfectly fine

Response.Write and Literal.Text output whatever you give them with no processing.
This means whatever you give these methods/property will be output verbatim. No URL resolution will occur and the runat=server attribute is meaningless.
You can use the Control.ResolveClientUrl() method to generate the link yourself, or manipulate the link as a server control in the codebehind.
Option 1
// this line is fine
string url = ResolveClientUrl("~/some-url/foo.aspx");
// this line is quite ugly, but should work
var lit = new Literal { Text = "<a href='" + url + "'>a link</a>" };
// add it the page/control's control hierarchy
this.Controls.Add( lit );
Option 2
// instantiate the control dynamically
// URL resolution is done automatically
var lnk = new HtmlAnchor { HRef = "~/some-url/foo.aspx" };
// add it the page/control's control hierarchy
this.Controls.Add( lnk );
Option 3
Let's say that you must deal with a string, not the control hierarchy (as per the comments). This is not ideal, but it is possible if the URLs in the string are relative to the application root.
// resolve the application root
string root = ResolveClientUrl("~/");
// string of raw HTML
string html = "My PIPs";
// replace the urls with a resolved version
// this is very simplistic...a proper parser would be more reliable
html = html.Replace( "~/", root + "/" );
// output as desired
Response.Write( html );

Related

How to pass html li tag value to aspx.cs page?

When i scan qr code the qr code value stored into {{scan.content}} . Now i want to get this value of scan.content in aspx.cs page. How to pass {{scan.content}} value to aspx.cs after scanning the qr code .
This is my code:
<section class="scans">
<h2>Scans</h2>
<ul v-if="scans.length === 0">
<li class="empty">No scans yet</li>
</ul>
<transition-group name="scans" tag="ul">
<li id="scanvalueid" name="scanvaluename" v-for="scan in scans" :key="scan.date" :title="scan.content" >{{ scan.content }}</li>
</transition-group>
</section>
You can put runat="server" attribute in li and can access in backend.

Issue with pagination in PagedList MVC

I am using MVC PagedList to handle pagination in my project. Everything works fine except that in my controller I have to initialize pageNumber parameter with "1" (Default page number). The problem is the blue activated button on page 1 that remains there no matter which page button link you click. Moreover this button is also missing the actionLink.
One last thing I am integrating my search with pagination as you can see in the controller code.
public PartialViewResult List_Items(string keyword, int? page)
{
var newlists = from s in db.TrMs
orderby s.S1
select s;
int pageNumber = (page ?? 1);
int pageSize = 10;
var data = newlists.Where(f => f.S1.ToString().Contains(keyword) && f.S100 == vt).ToPagedList(pageNumber, pageSize);
return PartialView(data);
}
Here is the generated Html for PagedList in my view
<div class="pagination-container">
<ul class="pagination">
<li class="active">
<a>1</a>
</li>
<li>
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#searchResult" href="/TrM/Search_Items?keyword=&page=2">2</a>
</li>
<li>
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#searchResult" href="/TrM/Search_Items?keyword=&page=3">3</a>
</li>
<li>
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#searchResult" href="/TrM/Search_Items?keyword=&page=4">4</a>
</li>
<li class="PagedList-skipToNext">
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#searchResult" href="/TrM/Search_Items?keyword=&page=2" rel="next">»</a>
</li>
</ul>
</div>
Here is how I am using the pagination helper.
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("List_Items", "TrM", new {keyword="", page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "searchResult" }))
Your paging controls are most likely outside of the searchResult container. When you click on a page you're replacing the table but not the paging controls. Move your paging controls inside of your searchResult container!
Hope this helps.

How can I elegantly preserve filter parameters when paginating with Thymeleaf?

On many of my pages, I have several options to sort or filter a long listing of results.
I'd like to have those selections preserved properly when someone pages through the data. For example:
Start looking at the list of all tickets. (/tickets)
Search for all tickets like "foo." (/tickets?query=foo)
Go to the next page. (/tickets?query=foo&page=2)
See the next page of tickets like "foo", not the 2nd page of "all tickets." (e.g. not /tickets?page=2)
In JSP, I came up with a solution like this:
<c:if test="${page.hasNext()}">
<spring:url value="" var="nextLink">
<c:forEach items="${param}" var="curParam">
<c:if test="${curParam.key != 'page'}">
<spring:param name="${curParam.key}" value="${curParam.value}"/>
</c:if>
</c:forEach>
<spring:param name="page" value="${page.nextPageable().pageNumber}"/>
</spring:url>
<li>
<a href="${nextLink}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
That basically constructs a new URL pointing to the same path as the current page, but adds/replaces the "path" parameter to it's query parameters.
It's pretty ugly in JSP, but I don't see a way to really do this at all in Thymeleaf?
Or, perhaps there's some better way to do this using some Spring or Thymeleaf feature I haven't encountered yet?
My hope is to have a concise Thymeleaf fragment I can reuse everywhere I need pagination. So far, this is all I have, but it ends up missing missing the "query" parameter, etc. (I don't want to hard-code the query parameter here, because that would limit the reusability of this pagination code)
<li th:if="${page.hasNext()}">
<a href="pagination.html" th:href="#{''(page=${page.nextPageable().pageNumber})}" aria-label="next">
<span aria-hidden="true">»</span>
</a>
</li>
Maybe this will be usefull for someone using HTML and Spring with Thymeleaf.
In my case I use thymeleaf with spring. I have a DTO as a filter that is used in the controller to do the filtering.
public class Controller {
private final int BUTTONS_TO_SHOW = 5;
public String home(FilterDTO filterDTO #PageableDefault(size = 50) Pageable pageable, Model model){
List<YourEntity> rows = YourEntityService.findAll();
Pager pager = new Pager(rows.getTotalPages(), rows.getNumber(), BUTTONS_TO_SHOW);
model.addAttribute("rows", rows);
model.addAttribute("pager", pager);
model.addAttribute("filterDTO", filterDTO);
return "your-template";
}
}
and the paginator like this:
<nav style="display: inline-block">
<ul class="pagination">
<li th:class="page-item" th:classappend="${rows.number == 0} ? ''">
<a class="page-link" th:href="#{${#httpServletRequest.requestURI+'?'+filterDTO.toQueryString()}(page=0)}">«</a>
</li>
<li th:class="page-item" th:classappend="${rows.number == 0} ? ''">
<a class="page-link" th:href="#{${#httpServletRequest.requestURI+'?'+filterDTO.toQueryString()}(page=${rows.number - 1})}">←</a>
</li>
<li th:class="page-item" th:classappend="${rows.number == (page - 1)} ? 'active pointer-disabled'"
th:each="page : ${#numbers.sequence(pager.startPage, pager.endPage)}">
<a class="page-link" th:href="#{${#httpServletRequest.requestURI+'?'+filterDTO.toQueryString()}(page=${page - 1})}" th:if="${page != 0}"
th:text="${page}"></a>
</li>
<li th:class="page-item" th:classappend="${rows.number + 1 == rows.totalPages or pager.endPage == 0} ? ''">
<a class="page-link"
th:href="#{${#httpServletRequest.requestURI+'?'+filterDTO.toQueryString()}(page=${rows.number + 1})}">→</a>
</li>
<li th:class="page-item" th:classappend="${rows.number + 1 == rows.totalPages or pager.endPage == 0} ? ''">
<a class="page-link"
th:href="#{${#httpServletRequest.requestURI+'?'+filterDTO.toQueryString()}(page=${rows.totalPages - 1})}">»</a>
</li>
</ul>
</nav>
and in the FilterDTO class I implemented the method: toQueryString() that returns the query string with your applied filters.
Dandelion Datatables is project that can help you not reinvent the wheel.
http://dandelion.github.io/components/datatables/

Unable to Navigate using LinkText in Selenium C#

I got the following C# code from Selenium IDE:
driver.FindElement(By.LinkText("Sub Link 1")).Click();
But as it wasn't working, I modified the code below to wait. It is still not working.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.LinkText("Main Link 1")));
if (element != null)
{
var innerElement = wait.Until(ExpectedConditions.ElementExists(By.PartialLinkText(
"Sub Link 1")));
}
What might be the possible issue?
My HTML:
<html>
Show navigation
Hide navigation
<div id="navlogo"><a title="HOME" href="url">
<span style="position:absolute;width:100%;height:100%;top:0;left: 0;">
</span></a></div>
<ul class="clearfix">
<li>MXLMain1
<ul>
<li>ML1
</li>
<li>ML2
</li>
</ul>
</li>
<li>MXLMain2
<ul>
<li>MK2
</li>
</ul>
</li>
<li>Main Link 1
<ul>
<li>Sub Link 1
</li>
<li>Sub Link 2
</li>
</ul>
</li>
..........................
</html>
How about using xpath?
driver.FindElement(By.XPath("//a[.='Sub Link 1")).Click();
It looks like the link that you are trying to use is from a navigation bar. You will need to use Seleniums Actions class to activate the menu dropdown.
Then use Actions class to move to element and click on it.
public void MouseHover(By locator)
{
element = driver.FindElement(locator);
Actions action = new Actions(driver);
action.MoveToElement(element).Perform();
}
To click same steps.
action.MoveToElement(element).Perform();
action.Click().Perform();
Move to initial element
Move to the second element and use click method

change master page <a href link from content page

i have this on my master.page
<ul class="menu">
<li class="first" runat="server" id="Li2">
<a runat="server" id="A1" href="../NewEntry.aspx">Create a New Entry</a>
</li>
</ul>
when i go to content page ("NewEntry.aspx") i want the link name to be changed to "Update Entry"
<ul class="menu">
<li class="first" runat="server" id="Li2">
<a runat="server" id="A1" href="../UpdateEntry.aspx">Update Entry</a>
</li>
</ul>
any feedback?
Make the link an asp:Hyperlink. Then have the master page expose a function or property:
public void SetLink(string href, string text)
{
A1.NavigateURL = href;
A1.Text = text;
}
Call the function from the main page.
You can use a hyperlink control <asp:hyperlink> and set the url as well as the text values.
I would recommend handling this as a HyperLink control as others have mentioned. If for some reason you must handle this as a server-side HTML anchor, you can access it using the following code from your webform code-behind:
HtmlAnchor link = (HtmlAnchor)(this.Master).FindControl("A1");
link.InnerText = "Update Entry";
You can also define a content place holder where you have "Create a New Entry". Leave that as the default inside that place holder, and only in the content page set content for it to Update Entry.

Resources