In one .aspx webpage I have 2 contacts details in two different <div> like below
<div id="primaryContact">
Name - betty quay
Cell - 9867452389
designation - Build
</div>
<div id="secondryContact">
Name - francesco maitire
Cell - 9867452389
designation - Build
</div>
Here I want to show each div on alternate day of a week.
How can I do so, using ASP.NET or Javascript?
You can do it in C# like this:
protected void Page_Load(object sender, EventArgs e)
{
ShowContactAlternate();
}
private void ShowContactAlternate()
{
if ((int)DateTime.Now.DayOfWeek % 2 == 0)
primaryContact.Visible = true;
else
secondaryContact.Visible = true;
}
Also make sure that you change your markup to something like this and add runat="server and Visible="False" attributes to divs":
<div id="primaryContact" runat="server" Visible="False"> Name - betty quay Cell - 9867452389 designation - Build </div>
<div id="secondryContact" runat="server" Visible="False"> Name - francesco maitire Cell - 9867452389 designation - Build </div>
EDIT:
If you want to alternate every week you can do it like this probably:
Create this helper function:
private int GetWeekOfYear(DateTime date)
{
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
}
And you can use it in your if statement inside ShowContactAlternate line this:
if (GetWeekOfYear(DateTime.Now) % 2 == 0)
As every year has 52 weeks you have to be sure that you don't run in to trouble every new year. It shouldn't be a problem to solve.
Related
Here is what I can see when I do e.Item.FindControl for the TextBox I'm trying to get the value for
I have 2 date inputs in a list view and I am trying to retrieve their values but always get an empty string.
If I set the value in the front end then it comes through to the code behind but if I edit the field the original value is returned.
I am able to get the values from and controls without an issue.
I've tried using asp:TextBox and html
<asp:TextBox ID="editArrivalFrom" name="editArrivalFrom" TextMode="Date" runat="server" OnTextChanged="editArrivalFrom_TextChanged"></asp:TextBox>
<input type="date" id="editArrivalTo" name="editArrivalTo" runat="server" />
var arrivalFrom = (TextBox) e.Item.FindControl("editArrivalFrom");
if (!String.IsNullOrWhiteSpace(arrivalFrom.Text))
var arrivalTo = (HtmlInputGenericControl) e.Item.FindControl("editArrivalTo");
if (!String.IsNullOrWhiteSpace(arrivalTo.Value))
I've also tried using Request.Form, all of the below return null:
var test = Request.Form[arrivalFrom.UniqueID];
var test2 = Request.Form[arrivalFrom.ClientID];
var test3 = Request.Form["editArrivalFrom"];
There is also no sign of either of the inputs in Request.Form.AllKeys
The asp control doesn't show up in the designer.cs file and adding it only results in it being null anyway.
Any suggestions would be much appreciated I've been pulling my hair out for hours.
Relevant code for the form update is below. Please note that all of this is contained within an update panel
<asp:ListView ID="MarkupRuleList" runat="server"
OnItemCommand="MarkupRuleList_OnItemCommand">
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" CssClass="lstview_record_bttn button-spacing-7" Text="Update" CommandName="Update" CommandArgument='<%# Eval("Id") %>' runat="server" />
</EditItemTemplate
protected void MarkupRuleList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
int markupId = 0;
int.TryParse(e.CommandArgument.ToString(), out markupId);
var markup = new MarkupRuleDTO();
switch (e.CommandName)
{
case "Update":
// GetMarkupRuleFromPage contains all of the code from the top of this post
markup = GetMarkupRuleFromPage(e, markupId);
break;
}
}
Edit: Solved by ditching the date input and using regular textbox, then performing the following onkeyup JS function
function dateInputHandler(item, event) {
// key code 8 == backspace
if (event.keyCode != 8) {
// insert forward slashes after days and months
if (item.value.length == 2 || item.value.length == 5) {
item.value = item.value + '/'
}
// remove double slashes and prevent length exceeding 10 characters
// format should be dd/mm/yyyy
if (item.value.endsWith("//") || item.value.length > 10) {
item.value = item.value.slice(0, -1);
}
}
}
You may be better off casting the sender object rather than the EventArgs object.
var x = ((TextBox)sender).Text;
But you can access the control directly (or should be able to).
var x = editArrivalFrom.Text;
And to get from the form, use the correct variable name:
var x = Request.Form[editArrivalFrom.ClientID];
I need to select the value (hours) related to an specific date. For example in the html below I need to read the number 24:20 based on the number 6;
this is the html:
<div class="day-ofmonth">
<div class="day-ofmonth">
<span class="day-num">6</span>
<span class="available-time">24:20</span>
</div>
<div class="day-ofmonth">
<span class="day-num">7</span>
<span class="available-time">133:50</span>
</div>
<div class="day-ofmonth">
<div class="day-ofmonth">
if I use:
IWebElement t_value = d.FindElement(By.XPath(".//* [#id='calinfo']/div[9]/span[2]"));
var t_again2 = t_value.GetAttribute("textContent");
i will get 24:20; but i need to get the value 24:20(in this case) based on number 6 (6 refers to day of the month) and not the Xpath (everyday will be a different date). If anyone can point me in the right direction, Thanks
string availableTime = null;
// Find all elements with class = 'day-num'
var dayNums = d.FindElements(By.XPath("//span[#class='day-num']"));
foreach (IWebElement dayNum in dayNums)
{
// check if text is equal to 6
if (dayNum.Text == "6")
{
// get the following sibling with class = 'available-time', then get the text
availableTime = dayNum.FindElement(By.XPath("following-sibling::span[#class='available-time']")).Text;
break;
}
}
A one liner solution:
string availableTime = d.FindElement(By.XPath("//span[#class='day-num' and text()='6']/following-sibling::span[#class='available-time']")).Text;
xpath=//span[text()='6')]/following-sibling::span[1]
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
<HeaderTemplate>
<ul class="sf_newsList">
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="Publication_Date" runat="server" Text="{0}" />
...
Ok so i have a date coming back from the source, i need to strip this date into day and month as it currently
comes back like "march 12 2012".
once parsed into a day and a month string i want to then pass it back into
<asp:Literal ID="Publication_Day" runat="server" Text="{0}" />
now ive tried some things such as
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string newsdate = "";
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Literal ltDate = (Literal)e.Item.FindControl("Publication_Date");
newsdate = ltDate.Text;
//do parse code
Literal ltDay = (Literal)e.Item.FindControl("Publication_Day");
ltDay.Text = newsdate;
}
}
but when i step through the publication_date is only returning {0} as a string not the date.
i know the source is fine, as if i just put the date on the template is shows up on the front end.
Any ideas what im doing wrong?
It is returnin {0} because it is on the Text property of Literal.
You could use the DataBinder.Eval to read the column of your DataSource from e.Item.DateItem. Since you know what is the column you want to read you could get the DateTime object direct from datasource, for sample:
// get the dateTime from datasource
DateTime datePublication = DateTime.Parse(DataBinder.Eval(e.Item.DataItem, "DatePublicationColumn").ToString());
// show the day name
ltDay.Text = datePublication.ToString("dddd");
Where I add DatePublicationColumn, you specify the column.
I am using asp.net webform 4.5.1 code first with entity framework. I used one repeater and bind it to my entity class. I want use if statement to decide show one DIV in this repeater or not. my code is :
<asp:Repeater ID="ProductRepeater" runat="server"
ItemType="Models.Product"
SelectMethod="ProductRepeate_GetData">
<ItemTemplate>
<% if(Item.Rank > 5 && Item.X != null && Item.Y != null){%>
<div>I want show this div just if my if statement is True</div>
<%}%>
<div >
<%# Item.Name%>
</div>
</ItemTemplate>
</asp:Repeater>
I want show the first div just when the result of if statement is True. the error is : The name 'Item' does not exist in the current context
This isn't the kind of calculation that you would want to include inline; not only will it be very difficult to read, it will also be very difficult to debug.
Instead, create a label <asp:Label ID="outputLabel" runat="server" ></asp:Label> and set the value of the label from the ItemDataBound Event on the repeater.
protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Label output = (Label)item.FindControl("outputLabel");
Product product = (Product)item.DataItem;
if (product.Rank > 5 && product.X != null && product.Y != null)
{
output = "I want show this div just if my if statement is True";
}
else
{
output = product.Name;
}
}
I know you already got an answer, you can do it in markup also like this:
<%# (Item.Rank > 5 && Item.X != null && Item.Y != null)? "<div>I want show this div just if my if statement is True</div>" : "" %>
I am using MVC + EF
I have a Feed xml file url that gets updated every 7 minute with items, every time a new item gets added I retrieve all the items to a list variable and then I add these varible to my database table. After that I fill a new list variable which is my ViewModel from the database table. Then I declare the ViewModel inside my view which is a .cshtml file and loop throught all of the objects and display them.
How can I make sure that the newest items get placed on the top and not in the bottom and also the numbers displays in correct order?
This is how I display the items inside my cshtml note that I use a ++number so the newest item needs to be 1 and so on ::
#model Project.Viewmodel.ItemViewModel
#{
int number = 0;
}
<div id="news-container">
#foreach (var item in Model.NewsList.OrderByDescending(n => n.PubDate))
{
<div class="grid">
<div class="number">
<p class="number-data">#(++number)</p>
</div>
<p class="news-title">#(item.Title)</p>
<div class="item-content">
<div class="imgholder">
<img src="#item.Imageurl" />
<p class="news-description">
#(item.Description)
<br />#(item.PubDate) |
Source
</p>
</div>
</div>
</div>
}
</div>
This is how I fill the viewmodel which I use inside the .cshtml file to iterate throught and display the items
private void FillProductToModel(ItemViewModel model, News news)
{
var productViewModel = new NewsViewModel
{
Description = news.Description,
NewsId = news.Id,
Title = news.Title,
link = news.Link,
Imageurl = news.Image,
PubDate = news.Date,
};
model.NewsList.Add(productViewModel);
}
If you check this image thats how it gets displayed with the numbers, thats incorrect.
If you see the arrows thats how it should be, how can I accomplish that?
Any kind of help is appreciated :)
note: When I remove .OrderByDescending, the numbers are correctly on each grid. But I need the .OrderByDescending beacuse i want the latest added item in the top.
Try this:
#model Project.Viewmodel.ItemViewModel
#{
int number = 0;
var NewsItems=Model.NewsList.OrderByDescending(n => n.PubDate).ToList();
}
<div id="news-container">
#foreach (var item in NewsItems)
{
<div class="grid">
<div class="number">
<p class="number-data">#(++number)</p>
</div>
<p class="news-title">#(item.Title)</p>
<div class="item-content">
<div class="imgholder">
<img src="#item.Imageurl" />
<p class="news-description">
#(item.Description)
<br />#(item.PubDate) |
Source
</p>
</div>
</div>
</div>
}
</div>
Looking at your sketch I assume you have float: left or display: inline-block for a grid class. Adding float: right might do the trick.
If that does not help please post CSS you have.
just a quick word..
you are passing NewsViewModel to the view and performing iteration on ItemViewModel ..y?
do u think this may be the cause of the problem..
Regards
You could sort your news list using the CompareTo method:
model.NewsList.Sort((a, b) => b.PubDate.Date.CompareTo(a.PubDate.Date));
Once you have the list sorted correctly, you can simply use CSS to display the news list two items per row. See this fiddle.
The fiddle is a revised one which was provided to me in a similar question I asked before.
Try this one
private void FillProductToModel(ItemViewModel model, News news)
{
var newList = list.OrderByDescending(x => x.News.Date).toList();
var productViewModel = new NewsViewModel
{
Description = newList .Description,
NewsId = newList .Id,
Title = newList .Title,
link = newList .Link,
Imageurl = newList .Image,
PubDate = newList .Date,
};
model.NewsList.Add(productViewModel);