asp .net list control binding - asp.net

First of all sorry about the bad title but i didn't know how to describe my problem correctly.
Well, i am quite new to asp .net so go on easy on me ^^
I have html file with a structure like the following :
<ul id="Ministries">
<li id="Ministry1"> Ministry1 title</li>
<li id="Ministry2"> Ministry2 title </li>
<li id="Ministry3"> Ministry3 title </li>
</ul>
<ul id="ministryDescriptions">
<li id="Ministry1Description">
<div id="Ministry title" class="x"> Ministry1 description</div>
<li id="Ministry1Services">
<ul id="Ministry1servicesTtiles">
<li id="Ministry1Service1">Ministry1Service1Title</li>
<li id="Ministry1Service2">Ministry1Service2Title</li>
<li id="Ministry1Service3">Ministry1Service3Title</li>
</ul>
<ul id="Ministry1servicesDetails">
<li id="Ministry1Service1Details">
<div id="service1description" > desc service1 of ministry1</div>
<p>whatever here</p>
<ul id="Ministry1Service1RequiredDocs">
<li id="Ministry1Service1RequiredDoc1">Doc1</li>
<li id="Ministry1Service1RequiredDoc2">Doc2</li>
<li id="Ministry1Service1RequiredDoc3">Doc3</li>
</ul>
</li>
<li id="Ministry1Service2Details"></li>
<li id="Ministry1Service3Details"></li>
</ul>
</li>
</li>
<li id="Ministry2Description"> </li>
<li id="Ministry3Description"> </li>
</ul>
I am getting objects from database in a List where Ministrry has attributes like
public class MinistryModel
{
public string MinistryName { get; set; }
public string MinistryMission { get; set; }
public string MinistryVision { get; set; }
public string MinistrySpecialities { get; set; }
public string MinistryUrl { get; set; }
public List<string> MinistryServices { get; set; }
}
I need to find a way to generate this structure in that exact html form and structure with the database objects.
I used to use Asp:ListView bit in this case the ul and li are not (all) nested, this is why i am a bit lost.
I hope you understood my problem and waiting forward for some help.
PS: I can't change HTML structure i must use it as it is

I'm not sure what source your data is coming from so this is general code for use of DataList for your example.
<asp:DataList ID="DataListTitles" runat="server" >
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate> DataBinder info for Ministry# title</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:DataList>
<asp:DataList ID="DataListDescriptions" runat="server">
<HeaderTemplate>
<div>DataBinder info for Ministry# Description</div><ul>
</HeaderTemplate>
<ItemTemplate>
-DataBinder info for Ministry# Service# Titles
-DataBinder info for Ministry1servicesDetails
-DataBinder info for Docs
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:DataList>

You might need to use nested Listviews to have the exact output.
Or simply create the HTML code dynamically on codebehind.

Related

Create dynamic links in the navigation panel

I am trying to dynamically create my top navigation panel in ASP.NET Core MVC 6.
I know it's simple but I cannot figure out how to make it work. Here is what I do (simplified):
My Model:
public class IP_Category
{
public int id { get; set; }
public string DisplayName { get; set; }
}
in my controller:
public IActionResult Index()
{
//This way I dynamically pass data to my View
ViewBag.Categories = _repository.ReturnCategories();
return View();
}
in my cshtml page:
#{
//this info is in the top of the page, here I retrieve data passed from
//controller and save it as a local variable
var categories = (List<IP_Category>)ViewBag.Categories;
}
Then later in the _Layout where I take care of the navigation:
<ul class="nav navbar-nav">
<li><a asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
#foreach (var category in categories)
{
<li><a asp-controller="Home" asp-action="#category.DisplayName">#category.DisplayName</a></li>
}
</ul>
The problem occurs with asp-action="#category.DisplayName" which does not generate appropriate href in my actual page.
So the question is what am I doing wrong? How can I pass category.DisplayName to my asp-action tag so the links work correctly?
Edit 1 - Adding more code:
Here is what was generated (note the missing href tag)
<ul class="nav navbar-nav">
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Item1</li>
<li>Item2</li>
</ul>
if the controller and action don't match an existing controller and action then no href will be rendered. I suspect that #category.DisplayName does not match any actual action name on your home controller. Seems more likely that you have an action named Category that expects a parameter corresponding to the #category.DisplayName so it should be passed as a route parameter not as the action name
#foreach (var category in categories)
{
<li><a asp-controller="Home"
asp-action="Category"
asp-route-category="#category.DisplayName">#category.DisplayName</a></li>
}

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

In href, ~ is not working when binding from code behind

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 );

asp.net literal.text using multiple lines of html code

I am trying to initialize a literal with a big sequence of html code.
for example this is working just fine:
Literal1.Text.= "<ul><li>home<li/><ul/>";//just fine
But:
When i have more than one lines it doesn't work:
Literal1.Text.= "<ul><li>home
</li></ul>";//error
I know a simple solution which is this:
Literal1.Text.= "<ul><li>home"+
"</li></ul>";//nice but time wasting for many lines
But:
When i have 100 lines for example i don't want to spend my time concatenating the strings.Is there any more practical solution?
Specifically my html code is the folowing:
Literal1.Text.= "<ul id="nav">
<li class="current">Home</li>
<li>People
<ul>
<li>Customers
<ul>
<li>View</li>
<li>Edit</li>
<li>Print</li>
<li>Delete</li>
</ul>
</li>
<li>Employees
<ul>
<li>View</li>
<li>Edit</li>
<li>Print</li>
<li>Delete</li>
</ul>
</li>
</ul>
</li>
<li>Quotations
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Invoices
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Receipts
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Statements
<ul>
<li>Create</li>
</ul>
</li>
<li>About Us</li>
<li class="style1">Contact Us</li>
</ul>";
For a small number of lines of code, I generally use StringBuilder. That way there are no performance issues with concatenating large immutable strings.
StringBuilder sb = new StringBuilder();
sb.Add("<ul><li>home");
sb.Add("</li></ul>");
For a large number of lines, don't put them in your code at all. Read them in using System.IO.File, so that you can edit your HTML without worrying about doubling your quotes. If you're using the code a lot, read it in once using a static class so that you're not causing a lot of extra IO usage. Something like this (untested):
public static class HTMLStringClass
{
private static string html;
public static string GetHtmlString()
{
if (string.IsNullOrEmpty(html))
html = File.ReadAllText("path/to/file");
return html;
}
}
And then call it by doing this:
Literal1.Text = HtmlStringClass.GetHtmlString();
Use a verbatim string literal (one that starts with #):
Literal1.Text = #"<ul><li>home
</li></ul>";

check for condition in site.master page

I have a site.master page in an asp application which contains a div menu and it looks like this.
<div id="menu">
<ul>
<li class="first">home</li>
<li>manager services</li>
<li>employee services</li>
<li>my projects</li>
</ul>
</div>
What i want is that the manager services portion should be visible only if a particular employee has his 'Is_Manager' field set to True in the database. For the rest this tag should be invisible. How do I achieve this?? How do i check for this contidion over here in this site.master page? please help.
Do it inside of the mark up. In the code behind on page load, set the Is_Manager flag and look it up from the database. Then, in the markup, do this:
<% if(Is_Manager){%>
<li>Manager Services</li>
<%}%>
Given what you've provided so far ...
in the cs file for the master:
protected bool IsVisible;
protected void Page_Load(object sender, EventArgs e){
IsVisible = (bool)Session["isVisible"];
}
in the aspx for the master page
<style>
.manager { visibility = <%= (IsVisible) ? "visible" : "hidden" %>;}
</style>
<div id="menu">
<ul>
<li class="first">home</li>
<li class="manager">manager services</li>
<li>employee services</li>
<li>my projects</li>
</ul>
</div>

Resources