I'm trying to create a blog page using a ListView. I am creating a reply function which adds a other css style to the listview when the message contains a parentmessageid.
This is working. Now I need to change the order of the message so the reply message will be placed under the parentmessage. Below you will find my implementation:
Function for datasource to fill the Listview:
protected void LoadMessages(int userid)
{
Bericht berichten = new Bericht();
if (berichten.LaadBerichten(userid).Tables.Count > 0)
{
ListViewMessages.DataSource = berichten.LaadBerichten(userid);
ListViewMessages.DataBind();
}
}
Function to add css style on items where LabelMessageID contains a value:
protected void ListItemMessages_Load(object sender, EventArgs e)
{
HtmlGenericControl li = (HtmlGenericControl)sender;
ListViewItem container = (ListViewItem)li.NamingContainer;
Label LabelParentMessageID = (Label)container.FindControl("LabelParentMessageID");
if (LabelParentMessageID.Text != string.Empty)
{
li.Attributes.Add("class", "reply");
}
}
ASP.NET ListView Source:
<asp:ListView ID="ListViewMessages" runat="server">
<ItemTemplate>
<li id="ListItemMessages" runat="server" onload="ListItemMessages_Load">
<img src="<%# Eval("[imagelocation]")%>" alt="image" />
<div class="top-pointer"></div>
<div class="pointer"></div>
<!--Hidden Controls-->
<asp:Label ID="LabelMessageID" runat="server" Text='<%# Eval("[messageid]")%>' Visible="false"></asp:Label>
<asp:Label ID="LabelParentMessageID" runat="server" Text='<%# Eval("[parentmessageid]")%>' Visible="false"/>
</li>
</ItemTemplate>
</asp:ListView>
Can someone help me out with changing the order of the items? Because I have no idea how to accomplish this.
try something like ds.Tables[0].DefaultView.Sort = "SortField DESC";
Solved using a SQL stored procedure.
Related
I am using an ASP.NET Dynamic Data Entities web application which contains a QueryableFilterRepeater server control inside the content placeholder on page List.aspx, when I execute the website, the filter control shows all the filters (Label Name along with the corresponding Drop Down) vertically.
Is there any way we can change the layout and display the filters horizontally?
Please find the .aspx code below
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" />
</ItemTemplate>
</asp:QueryableFilterRepeater>
The corresponding .cs file code:-
protected void Label_PreRender(object sender, EventArgs e)
{
Label label = (Label)sender;
DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
if (fuc != null && fuc.FilterControl != null)
{
label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
}
}
protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
}
Need suggestions.
Thanks in advance.
I usually wrap the control in a span element and then use CSS to style it the way that i want:
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<span class="filter">
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged"/>
</span>
</ItemTemplate>
i have a repeater:
<ul>
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource5" onitemcommand="Repeater4_ItemCommand">
<ItemTemplate>
<li class="sports_menu1">
<asp:LinkButton ID="LinkButton1" runat="server" class="selected onclick="sideMenuSports1_Click">
<%#Eval("iconURL")%>
</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
and i want to handle all the buttons in the same function:
protected void sideMenuSports1_Click(object sender, EventArgs e)
{
changeDataSources(); /*-----> this function supose to derict to URL(in a div on the same page) acording the id of the item of the repeater */
removeAllClasses(); /*this supose to remove all class named selected from the element in the <asp:button> element */
((LinkButton)sender).Attributes.Add("class", "selected");
}
to sumerise, what i need is:
i need to remove the class "selected" in all the repeater <asp:button> elements, then add it only to the element the user clicked
each button clicked redirect to other URL ,how i pass the button the id of the repeater element?
Had the same problem, solved it like this:
.aspx part:
<asp:Repeater ID="itemsRepeater" runat="server" OnItemCommand="itemsRepeater_ItemCommand">
<ItemTemplate>
<li>
<asp:LinkButton id="location_button" CommandArgument='<%# Eval("Key") %>' Text='<%# Eval("Value") %>' runat="server"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
code-behind part:
protected void itemsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// for the sample purpose, setting the value here - replace it with your own
int locationId = 2;
// looping through all the repeater items (buttons)
foreach (RepeaterItem i in ((Repeater)source).Items)
{
// finding the repeater items having ID set to "location_button"
LinkButton btn = (LinkButton)i.FindControl("location_button");
// custom class added when location matches button's CommandArgument
if (btn.CommandArgument == locationId.ToString())
btn.CssClass = "button_main active";
else
btn.CssClass = "button_main";
}
}
I used repeater in asp.net. My problem is don't know how to hide a fields in repeater. There is a regular price and now price if regular price is equal to zero it will hide the fields and if not it will show the value of the regular price. i hope you can help on this.
here my code in asp:
<a href="<%=Utility.GetSiteRoot() %>/BookInfo.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>">
<img width="150px" src='<%# Eval("lb_picturepath")%>'>
</td>
<td valign="top">
<asp:Label ID="lb_titleLabel" runat="server" CssClass="center-head" Text='<%# Eval("lb_title") %>' />
<p><asp:Label ID="lb_descriptionLabel" runat="server" Text='<%# Eval("lb_description") %>' /></p>
<div class="price"><%# "Price: " + decimal.Round((decimal)Eval("lb_sellingprice"),2)%></div>
</td>
</tr>
<tr>
<td></td>
<td>
<a class="addtocart" href="<%=Utility.GetSiteRoot() %>/AddToCart.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>" >Add To Cart</a>
<a href="<%=Utility.GetSiteRoot() %>/BookInfo.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>" class="readmore">
View Details
</a></td>
thanks!
You would need to handle the OnItemDataBound event, and then change the visibility of the control. An example of this is shown below:
ASPX Page
<asp:Repeater ID="MyRepeater" OnItemDataBound="MyRepeater_OnItemDataBound" runat="server">
<ItemTemplate>
<asp:Label ID="RegularPriceLabel" runat="server" />
<br/>
<asp:Label ID="BuyNowPriceLabel" runat="server" />
</ItemTemplate>
</asp:Repeater>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyRepeater.DataSource = GetDataSource();
MyRepeater.DataBind();
}
}
protected void MyRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// This will be your data object
MyEntity o = (MyEntity) e.Item.DataItem;
// Get the labels
Label RegularPriceLabel = (Label) e.Item.FindControl("RegularPriceLabel");
Label BuyNowPriceLabel = (Label) e.Item.FindControl("BuyNowPriceLabel");
// Only show regular price if it is set
RegularPriceLabel.Visible = (o.RegularPrice > 0);
// Populate labels
RegularPriceLabel.Text = o.RegularPrice.ToString();
BuyNowPriceLabel.Text = o.BuyNowPrice.ToString();
}
}
I would take a look at the ItemDataBound event of the Repeater. It will fire for every item in the repeater and allow you to do any code-behind (like hiding labels) more easily.
Edit: For your specific example, since you are formatting the price as well, it may be easier to just call a custom method to to render the price, like so:
ASPX:
<%#RenderPrice((decimal)Eval("lb_sellingprice"))%>
Method:
protected string RenderPrice(decimal price) {
if (price > 0) {
return "Price: $" + decimal.Round(price);
} else {
return string.Empty;
}
}
It's quick-and-dirty but it works.
aspx page:-
<asp:Repeater ID="rptAdd" OnItemCommand="rptAdd_ItemCommand" runat="server">
<ItemTemplate>
<td>
<asp:LinkButton ID="lnkBill" Text="Make Default" runat="server" Visible="true" CommandName="DefaultBill"></asp:LinkButton>
<asp:Label ID="labelBill" Text="Yes" Visible="false" runat="server"></asp:Label>
</td>
</ItemTemplate>
</asp:Repeater>
Code Behind:-
protected void rptAdd_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DefaultBill")
{
Users objBill = new Users();
objBill.IsDefault = true;
e.Item.FindControl("labelBill").Visible=true;
e.Item.FindControl("lnkBill").Visible = false;
}
}
In code behind intellisense is not detecting "labelBill" and "lnkBill"..what could be wrong ?
Also need to know...that's how u access controls in a repeater ?? like using findControl() ...right ?
[EDIT]
Changed code as follows..still not working...
((Label)e.Item.FindControl("labelBill")).Visible=true;
((LinkButton)e.Item.FindControl("lnkBill")).Visible = false;
Why wont intellisense detect these two IDs??
The problem is that your controls are inside a repeater, the find control wont search recursively. Try this instead.
rptAdd.FindControl("labelBull").Visible = true;
All the ways I can think to do this seem very hackish. What is the right way to do this, or at least most common?
I am retrieving a set of images from a LINQ-to-SQL query and databinding it and some other data to a repeater. I need to add a textbox to each item in the repeater that will let the user change the title of each image, very similar to Flickr.
How do I access the textboxes in the repeater control and know which image that textbox belongs to?
Here is what the repeater control would look like, with a submit button which would update all the image rows in Linq-to-SQL:
alt text http://casonclagg.com/layout.jpg
Edit:
This code works
Just make sure you don't blow your values away by Binding outside of if(!Page.IsPostBack) like me.. Oops.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="itemBox">
<div class="imgclass">
<a title='<%# Eval("Name") %>' href='<%# Eval("Path") %>' rel="gallery">
<img alt='<%# Eval("Name") %>' src='<%# Eval("Path") %>' width="260" />
</a>
</div>
<asp:TextBox ID="TextBox1" Width="230px" runat="server"></asp:TextBox>
</div>
</ItemTemplate>
</asp:Repeater>
And Submit Click:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox txtName = (TextBox)item.FindControl("TextBox1");
if (txtName != null)
{
string val = txtName.Text;
//do something with val
}
}
}
Have you tried something like following on the button click:-
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox txtName= (TextBox)item.FindControl("txtName");
if(txtName!=null)
{
//do something with txtName.Text
}
Image img= (Image)item.FindControl("Img");
if(img!=null)
{
//do something with img
}
}
/* Where txtName and Img are the Ids of the textbox and the image controls respectively in the repeater.*/
Hope this helps.
.aspx
<asp:Repeater ID="rpt" runat="server" EnableViewState="False">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" />
</ItemTemplate>
</asp:Repeater>
.cs
foreach (RepeaterItem rptItem in rpt.Items)
{
TextBox txtQty = (TextBox)rptItem.FindControl("txtQty");
if (txtQty != null) { Response.Write(txtQty.Text); }
}
Be sure to add EnableViewState="False" to your repeater, otherwise you will get empty string. (That wasted my time, dont waste yours :) )
On postback, you can iterate over the collection of RepeaterItems in repeater.Items. You could then retrieve each TextBox with code such as
TextBox tbDemo = (TextBox)rptr.Items[index].FindControl("textBox");