Why this is wrong?
<asp:Repeater ID="repeaterContent" runat="server">
<ItemTemplate>
<ArticleControlItem:ArticleControlItem runat="server"
Title='<%# Eval("Title") %>' ></ArticleControlItem:ArticleControlItem>
</ItemTemplate>
</asp:Repeater>
ArticleControlItem is my own Web Control, and I need to set parametr (Title). The control is placed in Repeater. How do I get variable "Title" from database to parametr Title? Why do I get this error?
CS0426: The type name ArticleControlItem does not exist in the type ArticleControlItem.ArticleControlItem.
This works perfect:
<asp:Repeater ID="repeaterContent" runat="server">
<ItemTemplate>
<ArticleControlItem:ArticleControlItem runat="server" Title="TEST" ></ArticleControlItem:ArticleControlItem>
</ItemTemplate>
</asp:Repeater>
Thank you.
Thank you for helping me. Relevant part of ArticleControlItem:
public string Title
{
get
{
object o = ViewState["Title"];
if (o == null) return "Default Title";
return (string)o;
}
set
{
ViewState["Title"] = value;
}
}
Can I save value from Eval("Title") to another variable? Something like
string TitleFromDB = Eval("Title")
Related
I have a table in DB:
NOTICE(NUM,TITLE,CONTENT)
I use Repeater Control in ASP to show all the notices in DB, like:
+----------+------------+|
|title1 (readmore)|
|
|title2 (readmore)|
|
|title3 (readmore)|
......
+------------------------+
All I want is: I read a "title" then I clicked on (readmore), the new page will be opened ( show detail's notice) with the "content" of that notice. How can I assign the num of notice without display it to define the notice in next page?
I just assign the title to property Text of a Label ID="TITLE" because I want to show the title of each notice.
All information I want to show in the this page is: title and the readmore( link to the next page). So that I don't know how to assign the num
My asp page: notice.asp
<asp:Repeater ID="RepDetails" runat="server" >
<HeaderTemplate>
<table style=" width:565px" cellpadding="0" class="borber">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Title" runat="server" Text='<%#Eval("TITLE") %>' />
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" > (readmord)</asp:HyperLink>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
My C# code:notice.asp.cs
private void BindRepeaterData()
{
string sql = "select num,title from NOTICE";
DataTable ds = l.EXECUTEQUERYSQL(sql);
RepDetails.DataSource = ds;
RepDetails.DataBind();
}
And the next page: detailnotice.asp.cs
private void GetNotice()
{
string sql = "select * from NOTICE where num=" ;// num= the num of notice I choose in notice.asp page.
}
How can I assign a num in Label without display it? What property of Label Control or I should use a other Control ?
Hope you understand what I say. If you don't, please ask?
Basically the same as Sain but using the NavigateURL
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("NUM","~/detailpage.aspx?id={0}") %>' > (readmord)</asp:HyperLink>
hi you can ues anchor tag in place of hyper link button. you can pass num in the query string to the detail page.
<a href='detailpage.aspx?id=<%#Eval("NUM") %>'> (readmord)</a>
On details page you can get query string value and fetch details from database.
int myKey = 0;
if (!string.IsNullOrEmpty(Request.QueryString["id"]))
{
myKey = int.Parse(Request.QueryString["id"]);
// Use myKey to retrieve record from database
}
I'm a rookie attempting to build an e-commerce site. My pager control that I use to display page numbers uses a repeater, which looks pretty dull. Can anyone show me how to programmatically change the background color of the page number that the user has selected? Or programmatically connect it to a CSS style sheet block of code that does it. Example: change the color behind the number to an orange square with a black border around it. Thanks.
Current code :
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="UserControls_Pager" %>
<p> Page <asp:Label ID="currentPageLabel" runat="server" />
of <asp:Label ID="howManyPagesLabel" runat="server" /> |
<asp:HyperLink ID="previousLink" Runat="server">Previous</asp:HyperLink>
<asp:Repeater ID="pagesRepeater" runat="server">
<ItemTemplate>
<asp:HyperLink ID="hyperlink" runat="server" Text='<%# Eval("Page") %>'
NavigateUrl='<%# Eval("Url") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:HyperLink ID="nextLink" Runat="server">Next</asp:HyperLink>
</p>
UserControl ASPX:
<p> Page <asp:Label ID="currentPageLabel" runat="server" /> of
<asp:Label ID="howManyPagesLabel" runat="server" /> |
<asp:HyperLink ID="previousLink" Runat="server">Previous</asp:HyperLink>
<asp:Repeater ID="pagesRepeater" runat="server"
onitemdatabound="pagesRepeater_ItemDataBound"> <ItemTemplate>
<asp:HyperLink ID="hyperlink" runat="server" Text='<%# Eval("Page") %>'
NavigateUrl='<%# Eval("Url") %>' /> </ItemTemplate>
</asp:Repeater>
<asp:HyperLink ID="nextLink" Runat="server">Next</asp:HyperLink> </p>
UserControl Code behind:
public class p
{
public string Page { get; set; }
public string Url { get; set; }
public p(string url, string page)
{
Page = page;
Url = url;
}
}
protected void Page_Load(object sender, EventArgs e)
{
List<p> arr = new List<p>();
arr.Add(new p("a.aspx", "a"));
arr.Add(new p("b.aspx", "b"));
pagesRepeater.DataSource = arr;
pagesRepeater.DataBind();
}
protected void pagesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HyperLink lnk = (HyperLink)e.Item.FindControl("hyperlink");
string[] URL = Request.Url.Segments;
string currentUrl = URL[URL.Length - 1];
if (lnk != null)
{
string lnkUrl=lnk.NavigateUrl;
if (lnkUrl == currentUrl)
{
lnk.BackColor = Color.Orange;
lnk.Style.Add("border", "1px solid #000000");
lnk.Style.Add("background-color", "orange");
lnk.Style.Add("text-decoration", "none");
}
}
}
You can do that through client side Jquery, for an example if your control that you want to color has an ID "MyControl"
$("#MyControl").css( "color", "red" ),
you don't have to change it from code behind.
Call this code in the document ready of your page as following:
<script>
$(document).ready(function() {
$("#MyControl").css( "color", "red" )
});
</script>
remember its always better to put your script code at the end of the page as it makes your page run faster.
I've an asp repeater which has some fields inside an ItemTemplate. Each item in the repeater has an "add to cart" asp:ImageButton and an invisible asp:Label as well. The code looks like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="addToCart">
<HeaderTemplate>
<table id="displayTable">
</HeaderTemplate>
<ItemTemplate>
<td>
<!-- fields like name, description etc in the repeater are present; i've omitted to show them here-->
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
</td>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
When a particular ImageButton in the repeater is clicked, I'm trying to display "added to cart" as the text of its corresponding Label, and make the clicked ImageButton Visible=false. I've tried using the OnItemCommand function for the ASP:Repeater. The method is "addToCart":
<>
void addToCart(Object Sender, RepeaterCommandEventArgs e)
{
Cart cart = new Cart();
cart.instrument_id = //id of product from repeater based on user click
String userName = Membership.GetUser().ToString();
cart.user_name = userName;
cart.quantity = 1;
var thisLbl = (Label)e.Item.FindControl("addedToCartLabel");
var thisImg = (ImageButton)e.Item.FindControl("addToCartImg");
try
{
database.Carts.InsertOnSubmit(cart);
database.SubmitChanges();
thisImg.Visible = false;
thisLbl.Text = "Added to Cart!";
thisLbl.Visible = true;
}
catch (Exception ex)
{
thisImg.Visible = false;
thisLbl.Text = "Processing failed;please try again later";
thisLbl.Visible = true; ;
}
}
The aspx page is populated properly. However, when I click on any of the ImageButtons in the repeater, I get the following error:
Server Error in '/mysite' Application.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback
or callback data for validation.
Can someone help me with this?
May I suggest doing this with client-side javascript rather than a server-side call?
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
becomes
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" onclick="javascript:function() { this.this.style.display='none'; document.getElementById(this.parentNode.firstChild.id).style.display='block'; }" ImageUrl="hi.jpg" Width="75px" Height="50px" />
I have this repeater on my page..Under the default column what I want is that there
should be an IF condition that checks my table's "IsDEfault" field value.
If IsDefault=True then the lable below "label1" ie "Yes" should be
displayed inside the repeater else "Make DEfault" link should be displayed..
Now how do I include this IF statement as inline code in my repeater to accomplish what I want ?
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
I have an idea :-
<%# If DataBinder.Eval(Container.DataItem,"IsDefault") = "True"
Then%>
<%End If%>
How should I form the "Then" statement now?
Please help me with the proper syntax..thnx
Do I need to like make a method that checks if "IsDefault" is true or not and then call it inside of inline code in my repeater ? How do I go about it ?
[EDIT]
I tried as follows:-
<% If (Eval("Container.DataItem,"IsDefault"")="True"?
("<asp:LinkButton ID="lnk1" Text="Set as Default" CommandName="SetDefault1" runat="server" CommandArgument='<%#Eval("User1ID") %>'
CausesValidation="false" Visible=true></asp:LinkButton>") : ("<asp:Label ID="label1" Text="Yes" runat="server" Visible=true></asp:Label>")
)%>
didnt work :( Help!!
If you want some control to be visible only on some condition, set the Visible property according to that condition:
<asp:Label ID="label1" Text="Yes" runat="server"
Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault") %>" />
EDIT
If you want the control INvisible for the "IsDefault" situation, reverse the test with something like Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault")==False %>".
I'm not quite sure about the exact syntax, but you should get the idea.
Here's your repeater markup. Notice both controls are hidden at the start:
<asp:Repeater runat="server" ID="rpt1" OnItemDataBound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand">
<ItemTemplate>
<p>
ID: <%# Eval("Id") %>
IsDefault: <%# Eval("IsDefault") %>
Name: <%# Eval("Name") %>
<asp:Label BackColor="Blue" ForeColor="White" runat="server" ID="lDefault" Text="DEFAULT" Visible="false" />
<asp:Button runat="server" ID="btnMakeDefault" Text="Make Default" Visible="false" CommandArgument='<%# Eval("Id") %>' />
</p>
</ItemTemplate>
</asp:Repeater>
And some code to go with it. Note I've simulated the retrieval of your collection of blluser objects, so there is some additional code there relating to this which you won't require as, presumably the bllusers collection that you bind to is coming from a db or something?
Anyway I think this is what you're looking for, but let me know if its not ;-)
//Dummy object for illustrative purposes only.
[Serializable]
public class bllUsers
{
public int Id { get; set; }
public bool isDefault { get; set; }
public string Name { get; set; }
public bllUsers(int _id, bool _isDefault, string _name)
{
this.Id = _id;
this.isDefault = _isDefault;
this.Name = _name;
}
}
protected List<bllUsers> lstUsers{
get
{
if (ViewState["lstUsers"] == null){
ViewState["lstUsers"] = buildUserList();
}
return (List<bllUsers>)ViewState["lstUsers"];
}
set{
ViewState["lstUsers"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
buildGui();
}
}
private List<bllUsers> buildUserList(){
lstUsers = new List<bllUsers>();
lstUsers.Add(new bllUsers(1, false, "Joe Bloggs"));
lstUsers.Add(new bllUsers(2, true, "Charlie Brown"));
lstUsers.Add(new bllUsers(3, true, "Barack Obama"));
return lstUsers;
}
private void buildGui()
{
rpt1.DataSource = lstUsers;
rpt1.DataBind();
}
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bllUsers obj = (bllUsers)e.Item.DataItem;//this is the actual bllUser the row is being bound to.
//Set the labels
((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault;
((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault;
//Or use a more readable if/else if you want:
if (obj.isDefault)
{
//show/hide
}
else
{
//set visible/invisible
}
}
}
Hope this helps :-)
Sorry to say you to be honest i was unable to understand what actually you wanted to do
If your looking to use the condition in the Item Templet then i think
the following systax will help you
<asp:LinkButton ID="Label1" runat="server"
Text='<%# ((Eval("Cond"))="True" ? Eval("Result for True") : Eval("Result for False") )%>'></asp:LinkButton>
I have two nested repeaters in my *.aspx page.
<asp:Repeater runat="server" id="rptMain">
<ItemTemplate>
<h1><%#DataBinder.Eval(Container.DataItem, "Name")%></h1>
<asp:Repeater runat="server" DataSource='<%# getUser(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "FieldKey"))) %>'>
<HeaderTemplate><ol></HeaderTemplate>
<ItemTemplate>
<li class="<%#DataBinder.Eval(Container.DataItem, "CSSStyle")%>" id="li<%#DataBinder.Eval(Container.DataItem, "FieldKey")%>">
<%#DataBinder.Eval(Container.DataItem, "NameSubject")%>
</li>
</ItemTemplate>
<FooterTemplate></ol></FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
getUser is a protected method. It must returns the List with the following properties:
CSSClass
FieldKey
NameSubject
But CSSClass property is defined into the anonymous type.
protected List<????> getUser(int id)
{
DataClassesDataContext datacontext = new DataClassesDataContext();
var t1= from t in datacontext.GetAllCustomSubject(id).ToList()
select new { t.NameSubject, t.FieldKey, CSSStyle = t.IsDeleted ? "deleted hidden" : "real visible" };
return t;
}
How can I return such List? What kind of datatype can I Use instead of ???? ?
In general, can I use nested repeaters with anonymous types?
Well, the repeater won't care, I suspect - so just declare it to return IEnumerable.