When the page first load i have a label who has 0 or 1. Look at the code and you will se what i trying to do. But it don't work because the page allready loaded.
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
if (lblName.Text == "1")
{
lblName.Text = lblName.Text + "Under arbete";
}
else if (lblName.Text == "0")
{
lblName.Text = "Fixad";
}
else { }
}
}
You really want to avoid this type of coding if at all possible. This will quickly grow into an unmaintainable web site.
Query the underlying data instead of GUI elements.
where you put your code? and be cautious e.Item.FindControl("lblBugStatus") as Label; may return null
it's just work fine to fine the control. But i figure out another way to get out the data i want. That i'am going to is fix the xml structur better.
You may have resolved this by now, but I think the problem with the code as you posted it is this:
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
Because you are referencing a control in a repeater, the controls inside each repeater item are named dynamically based on their context. Most likely the name of the control is something like:
"rptBugStatus$repeaterItem0$lblBugStatus"
To find out the exact name, hard code a value, run the page in a browser, and look at the HTML output (via "View Source" in the browser menu). You should be able to scroll down and see your repeater (it will be rendered as a <table> tag), its items, and the controls living inside each item. The IDs/Names will be set and you can copy/paste them into your FindControl method.
Hope this helps,
Nate
Ok, I am taking the assumption you have set this label (assuming to be in the repeater, from your code) to have a value - I have tried databinding to something exactly the same, using the below code.
protected override void OnPreRender(EventArgs e)
{
base.OnLoad(e);
List<string> strList = new List<string>();
strList.Add("1");
rptBugStatus.DataSource = strList;
rptBugStatus.DataBind();
}
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblBugStatus = e.Item.FindControl("lblBugStatus") as Label;
// have added this just so we are actually setting the text property on the bug status
// - i have assumed you do this.
lblBugStatus.Text = e.Item.DataItem.ToString();
if (lblBugStatus.Text.Equals("1"))
{
lblBugStatus.Text = lblBugStatus.Text + "Under arbete";
}
else if (lblBugStatus.Text.Equals("0"))
{
lblBugStatus.Text = "Fixad";
}
}
}
With the aspx being
<asp:Repeater runat="server" ID="rptBugStatus" OnItemDataBound="rptBugStatus_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblBugStatus" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
And I have no problems.
I get the below on the page.
1Under arbete
I think you are going to need to post more code if you have hidden any away from us :)
Tim
Related
Is there a way to set AlternateItemTemlate manually?
I suffered from this question more than once.
I want to use it only for the last item.
Maybe ItemDataBound event can be a solution but i can not figured out.
Only useful questions that I've found:
Is there a similar way to AlternateItemTemplate to do this
How can i get last record value from repeater control.?
The ItemDataBound is indeed a possible option, but for it to work, you would need the total count of the repeater items, so you can identify the last item:
protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
int itemsCount = ((List<SomeClass>)rptDummy.DataSource).Count;
if (e.Item.ItemType == ListItemType.Item)
{
if(e.Item.ItemIndex == itemsCount - 1)
{
//Do Things here
}
}
}
You could even have two placeholders within the same template, one specially for the last item:
<ItemTemplate>
<asp:PlaceHolder id="phIsNotLastOne" runat="server">Is not last</asp:PlaceHolder>
<asp:PlaceHolder id="phIsLastOne" runat="server">Is last</asp:PlaceHolder>
</ItemTemplate>
Then you could do something like this:
protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
long itemsCount = ((List<SomeClass>)rptDummy.DataSource).Count;
if (e.Item.ItemType == ListItemType.Item)
{
PlaceHolder phIsLastOne = (PlaceHolder)e.Item.FindControl("phIsLastOne");
PlaceHolder phIsNotLastOne = (PlaceHolder)e.Item.FindControl("phIsNotLastOne");
phIsLastOne.Visible = e.Item.ItemIndex == itemsCount - 1;
phIsNotLastOne.Visible = !this.phIsLastOne.Visible;
}
}
I am fairly new to ASP.NET (PHP programmer for 8 years), so please forgive me if my terminology is incorrect.
I am currently grabbing a remote XML file in a repeater XMLDataSource, and the DataFile attribute is being set in the code behind (I have the DataFile dynamically change based on URI parameters such as ?cat=blah etc). This works beautifully and is not a problem.
Within this repeater is a <asp:HyperLink>, whose URL is to be dynamically changed in the code behind as well.
Now to the question:
From the code behind, how can I see if XML nodes are empty, and how can I grab the value of nodes that are not?
I have been on this issue for about 3 days now (hours of searching), however I just cannot figure out something that seems like it should be so simple! Here is the code I currently have, however I am given an error of Object reference not set to an instance of an object for the code behind line that has CreateNavigator();
Hopefully somebody more adept than me with ASP.NET and C# can easily spot the problem or knows of a way I can accomplish this?
Code
<asp:XmlDataSource ID="rssFeed" runat="server"></asp:XmlDataSource>
<asp:Repeater runat="server" DataSourceID="rssFeed" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="articleURL" Text='<%#XPath("title")%>' runat="server" />
</ItemTemplate>
</asp:Repeater>
Code behind C#
protected void Page_Load(object sender, EventArgs e)
{
// rss variables
var rssSource = "http://websiteurl.com/xmlfeed/";
var rssPath = "rss/channel/item";
var searchQueries = "?foo=bar";
string currCat = Request.QueryString["cat"];
string searchTerms = Request.QueryString["s"];
// insert categories query
if(currCat != null){
searchQueries += "&cat=" + currCat;
}
// insert search query
if(searchTerms != null){
searchQueries += "&s=" + searchTerms;
}
// load RSS file
rssFeed.DataFile = rssSource + searchQueries;
rssFeed.XPath = rssPath;
}
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
XPathNavigator nav = ((IXPathNavigable)e.Item.DataItem).CreateNavigator();
string hyperlinkURL = nav.SelectSingleNode("link").Value;
// removed code here that changes URL to hyperlinkURL to shorten post
}
For those who may be interested in this as well, I found a solution where I can easily check and manipulate xml items.
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// make sure we are not checking item in ItemHeader or ItemFooter to prevent null value
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
string myVar = XPathBinder.Eval(e.Item.DataItem,"title").ToString();
// utilize and manipulate myVar XML value here
}
}
Question pretty much says it all. On my aspx page I have a GridView and under Columns I have a bunch of BoundFields, one of which is a TemplateField
<asp:TemplateField HeaderText = "Status">
<ItemTemplate>
<asp:HyperLink ID = "HyperLink1" runat = "server" Target = "_blank"
NavigateUrl = '<%# Eval("URL") %>'
Text = '<%#Eval("Status") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Now, I want this Hyperlink to map to a different image, depending on what the text is evaluated to. For example, 'Success' displays a big ol' smiley face instead, 'Failed' displays a frowney face, and so on. How can I achieve this?
Thanks for looking.
You can put an image in the hyperlink like
<img src='/images/status/<%#Eval("Status") %>.jpg' />
and just make a different image for each status by name. Otherwise you'll probably have to do something on the DataBind event.
Try this
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1");
if(SomeText == "Success")
HyperLink1.NavigateUrl = "Url to Smiley";
else
HyperLink1.NavigateUrl = "Url to Frowney";
}
}
HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
switch (HyperLink1.Text)
{
case "Completed":
HyperLink1.ImageUrl = "Images\\Success.png";
HyperLink1.ToolTip = "Completed";
etc
The ToolTip property maps to the alternate text for the image.
Thanks to codingbiz for getting me started.
If you are trying to set the ImageUrl property I suggest using the RowDataBound event. The handler method could would look something like:
protected void questionsGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
DataSourceDataType row;
HyperLink hyperLink1;
if (e.Row.RowType == DataControlRowType.DataRow & e.Row.DataItem is DataSourceDataType)
{
row = (DataSourceDataType)e.Row.DataItem;
hyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
hyperLink1.ImageUrl = (row.IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
Another trick I have used is altering the data object you are binding to to have a property which indicates the URL to use:
partial class DataSourceDataType
{
public string SuccessImgURL
{
get
{
return (IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
}
Then you bind to that property.
Note: IsSuccess would need to be replaced with your own field name or boolean condition.
I often use this with LINQ to SQL objects, so adding properties can be done in a separate file using partial classes. This way you do not have to worry about the LINQ to SQL tools removing your additions.
I want to find the control(hyperlink) in the gridview. Based on the value of the control I want to enable or disable the hyperlink.
I tried like this. But I am always getting null.
protected void gridResult_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink status = e.Row.FindControl("id") as HyperLink;
if ( status != null && status.Text == "AAAA" ) {
status.Enabled = false;
}
}
}
Please help.
Your "id" value is highly suspicious. My money is on the fact that you are supplying the wrong control name: FindControl("id!!!!!!!").
I would expect to see something like:
HyperLink status = e.Row.FindControl("hlStatus") as HyperLink;
If you are indeed supplying the correct control name (yuck), then it may be that your hyperlink control is nested too deep, in which case, you will need to 'crawl' your control hierarchy looking for it.
#dlev is absolutely correct, controls are often nested so you will need to create your own function of sorts to find what youre looking for, you could pass this function your control collection (e.Row.Controls()) and your id
private HyperLink FindControl(ControlCollection page, string myId)
{
foreach (Control c in page)
{
if ((HyperLink)c.ID == myId)
{
return (HyperLink)c;
}
if (c.HasControls())
{
FindControl(c.Controls, myId);
}
}
return null; //may need to exclude this line
}
I know you can utilize a ModalPopupExtender and have it be displayed when the user clicks a button or something of the sort by assigning the TargetControlID. What I'm looking to do is display this popup when an error occurs on my page. So by using conditional logic in the C# side of things, for example, if a certain variable is set to something, display this popup.
Is there a way I can do this, or something similar?
Yep, in your C# code you can call
my_ModalPopupExtender.Show();
Where my_ModalPopupExtender is the name of your popup extender.
It's that easy!
If, loading page, you know condition to show or not popup you can remove or not ModalPopupExtender!
In my case, populating a table using repeater :
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
ModalPopupExtender ModalPopupExtenderLinkButton =
e.Item.FindControl("ModalPopupExtenderLinkButton") as ModalPopupExtender;
if (condition)
e.Item.Controls.Remove(ModalPopupExtenderLinkButton);
}
}
Hope this will help!