the following code should show the list of websites in a repeater but it does not (though I get no errors):
In the aspx file I have:
<div>
<asp:Repeater ID="Rpt1" Visible="true" runat="server"></asp:Repeater>
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.GetDataItem(), "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.GetDataItem(), "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</div>
In the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
CaricaSiti();
}
protected void CaricaSiti()
{
ListDictionary L = new ListDictionary();
L.Add("google", #"http://google.com");
L.Add("microsoft", #"http://microsoft.com");
L.Add("yahoo", #"http://yahoo.com");
Rpt1.DataSource = L;
Rpt1.DataBind();
}
The repeater's ItemTemplate must be inside of the repeater :-)
<asp:Repeater ID="Rpt1" Visible="true" runat="server">
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.DataItem, "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</asp:Repeater>
Related
I am building a website right now using DataLists and I want to ask a question:
When using OnLoad on specific control in DataList item, the DataList has 5 items, but the function is called only 4 times (does what it needs to do but not on the last item)
C# code:
protected void ibDeleteAlbum_Load(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
ImageButton btnDelAlbum = item.FindControl("ibDeleteAlbum") as ImageButton;
btnDelAlbum.Visible = true;
}
}
HTML code:
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" >
<ItemTemplate>
<asp:Label ID="lblAlbumID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Visible="false"></asp:Label>
<asp:ImageButton ID="ibDeleteAlbum" type="image" runat="server" style="position:absolute; margin-right: 1.4vw; margin-top: 2.4vh;" Visible="false" ImageUrl="~/Images/ic_delete.png" OnClick="ibDeleteAlbum_Click" ToolTip="מחיקה" OnLoad="ibDeleteAlbum_Load" />
<asp:ImageButton ID="ibShowAlbums"
runat="server"
class="album"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "coverPhoto") %>'
CommandName="album"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "ID") %>'
OnCommand="ibShowAlbums_Command" />
<div class="albumNameShow" runat="server"><%#DataBinder.Eval(Container.DataItem, "albumName") %></div>
</ItemTemplate>
</asp:DataList>
Thanks ahead!
I'm working on a shopping cart functionality which allows users to add items to a shopping cart(via a add to cart button.) I have a specials page where members get discounted price for items. i have successfully passed the id(database) and sales price (calculated value) from the specials page to a add to cart page.
I couldnt post images because i dont have reputations so here is what the url looks like:
http://localhost:51231/LegoBits/Shopping/ShoppingCartItem.aspx?Id=1&SalesPrice=0.89
You can see the url is getting the values Id and SalesPrice from another page, but , i'm having trouble showing the salesPrice value in datalist of current page.
here is my code for the shoppingCartItem.aspx
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="Id" ItemStyle-HorizontalAlign="Left" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Thumbnail", "{0}") %>' />
<asp:Label ID="PictureURLLabel" runat="server" Text='<%# Eval ("Thumbnail") %>' Visible="false"></asp:Label>
<br />
<strong style="text-align: left;">Name: </strong>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
<strong style="text-align: left;">Model: </strong>
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>'></asp:Label>
<br />
<strong style="text-align: left;">Ages: </strong>
<asp:Label ID="AgesLabel" runat="server" Text='<%# Eval("Ages") %>'></asp:Label>
<br />
<strong style="text-align: left;">Price: </strong>
$
<asp:Label ID="PriceLabel" runat="server"></asp:Label>
<br />
<br />
<asp:ImageButton ImageUrl="../Images/AddToCart.jpg" ID="Button1" runat="server" OnClick="Button1_Click" />
</ItemTemplate>
</asp:DataList>
and its code behind:
public partial class Shopping_ShoppingCartItem : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
string salePrice = Request.QueryString["Sales Price"];
string price = Request.QueryString["price"];
if (Roles.IsUserInRole("Member"))
{
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
else {
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = price;
}
}
The priceLabel should show the SalesPrice value passed as querystring from another page. Please help me out. Thanks in advance!!!
Please try with the below code snippet.
ASPX
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="ID" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:Label ID="PriceLabel" runat="server"></asp:Label>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
ASPX.CS
protected void Page_Load(object source, System.EventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1"}
};
DataList1.DataSource = data;
DataList1.DataBind();
}
protected void Page_PreRender(object source, System.EventArgs e)
{
string salePrice = Request.QueryString["SalesPrice"];
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
Update 1
ASPX
<asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" DataKeyField="ID" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("SalesPrice") %>' ></asp:Label>
</ItemTemplate>
</asp:DataList>
ASPX.CS
protected void Page_Load(object source, System.EventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1", SalesPrice = 10.00}
};
DataList1.DataSource = data;
DataList1.DataBind();
}
protected void Page_PreRender(object source, System.EventArgs e)
{
if (Request.QueryString["SalesPrice"] != null && !string.IsNullOrEmpty(Request.QueryString["SalesPrice"]))
{
string salePrice = Request.QueryString["SalesPrice"];
((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text = salePrice;
}
}
Let me know if any concern.
Just remove the space.
string salePrice = Request.QueryString["SalesPrice"];
I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}
What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Im trying to get the values in a specific gridview row when clickin on it or when clicking on a button.
The problem is that nothing happen when i click on the row, Not even when i click on the button.
The rows are unclickable.
Any help
Here my code for the ASPx page.
<%# Page Language="C#"Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Order>" %>
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = ViewData["list"];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox x;
foreach (DataGridItem di in GridView1.Rows)
{
x = (CheckBox)di.FindControl("checkBox1");
if (x.Checked == true)
{
Label5.Text = GridView1.SelectedRow.Cells[2].Text;
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label5.Text = string.Format("YOu selected row{0} with {1} {2}",
GridView1.SelectedIndex + 1,
GridView1.SelectedRow.Cells[0].Text,
GridView1.SelectedRow.Cells[1].Text);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondblclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "');");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Tillverkning" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TillverkningsOrder") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Datum" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OrderDatum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Antal" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kundnamn" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("KundNamn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Lina,
Although i don't have the answer for your problem I have however found a data consistency issue.
When your scrolling through the checkboxes you are overtyping the Label5.Text each time. Therefore the scrolling is pointless as Label5 will only ever show the value of the last Checkbox it found to have been ticked.
May i suggest the following alternative:
CheckBox x;
foreach (DataGridItem di in GridView1.Rows)
{
x = (CheckBox)di.FindControl("checkBox1");
if (x.Checked == true)
{
if(Label5.Text == "")
{Label5.Text = GridView1.SelectedRow.Cells[2].Text;}
else {Label5.Text = Label5.Text + " - " + GridView1.SelectedRow.Cells[2].Text;}
}
}
This will at least append the data onto the string seperating it with " - " if there are more the 1 Checkboxs selected. Feel free to change the separator as required.
I have a DataList inside another DataList. I want to access the child DataList "dlQuestion" events, ItemDataBound event. Also, I'm tring to find the control LableControl "lblQuestion" in the child datalist. How do I do that? Here's the mark-up:
<asp:DataList ID="dlSection" runat="server" Width="100%">
<ItemTemplate>
<div>
<asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
<asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
</div>
<asp:DataList ID="dlQuestion" runat="server" >
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
<asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
You need to handle ItemDataBound event of the dlQuestion DataList and get lblQuestion Label in that event handler:
Markup:
<asp:DataList ID="dlSection" runat="server" Width="100%">
<ItemTemplate>
<div>
<asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
<asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
</div>
<asp:DataList ID="dlQuestion" runat="server" OnItemDataBound="dlQuestion_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
<asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
Code-behind:
protected void dlQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var lblQuestion = e.Item.FindControl("lblQuestion") as Label;
if (lblQuestion != null)
{
lblQuestion.ForeColor = Color.Red;
}
}
}
This is one way finding label control in child datalist...
//here I am finding item(DataList) of child Datalist
DataList dlSubChild = (DataList)childItem.FindControl("dlSubChild");
foreach (DataListItem subChildItem in dlSubChild.Items)
{
//here I am finding item(TextBox) of sub child Datalist
TextBox txtName = (TextBox)subChildItem.FindControl("txtName");
//set literal(litName) text
litName.Text = string.Format("{0}{1}", "Welcome ", txtName.Text);
}
i hope it will helps you ...