add strong element in ItemDataBound around span - c#

Is there a way in the code-behind to add a strong element around my asp:Label in the ItemDataBound event (between the li and asp:Label elements) when rendered?
the markup
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<li>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
csharp code behind, this is where I was hoping there is a way to add the strong element.
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label test = (Label)e.Item.FindControl("Label1");
}
}
I know there are easier ways to do this but I need to do it this way specifically unfortunately.

This seems pretty hacky and doesn't actually give you a strong control to work with, but I suppose something like this might work:
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label test = (Label)e.Item.FindControl("Label1");
Literal literal1 = new Literal();
literal1.Text = "<strong>";
e.Item.Controls.AddAt(e.Item.Controls.IndexOf(test), literal1);
Literal literal2 = new Literal();
literal2.Text = "</strong>";
e.Item.Controls.AddAt(e.Item.Controls.IndexOf(test) + 1, literal2);
}
}

Not easily. So far as I know, there is no <asp:Strong> tag. The easiest thing to do here would be to just put the tag in your code.
However, if you need to optionally implement the strong tag, the same thing can be accomplished by setting the css on your label to font-weight: bold (which is the same as wrapping content in the strong tag), or declaratively by setting the CssClass attribute of the label, ie
Label1.CssClass = "myStrongCssClass";

In the code behind you will need to find the label within the template then
Label1.Font.Bold = true;

Related

C# Get first repeater item column value to use on HeaderTemplate

I'm trying to get a column value of the first element on a repeater, and use it on the repeater's HeaderTemplate. After searching and trying many ways of achieving this through intellisense, I gave up and decided to post this question here.
My code is as follows:
Frontend
<asp:Repeater ID="states" runat="server">
<HeaderTemplate>
<h1>Stage: <asp:Literal ID="stageName" runat="server"></asp:Literal></h1>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# DataBinder.Eval(Container.DataItem, "StateName") %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Backend
protected void BindStageStates()
{
List<StagesStatesModel> statesList = App.Services.Stages.StagesService.GetStatesByStage(Page.DefaultApp, 1, Page.DefaultCultureId).Where(p => p.StateActive == true).ToList();
states.ItemDataBound += new RepeaterItemEventHandler(rptStagesStatesDataBound);
states.DataSource = statesList;
states.DataBind();
}
void rptStagesStatesDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Literal stageName = (Literal)e.Item.FindControl("stageName");
stageName.Text = // Something to go here..
}
}
Thanks in advance!
Try OnItemCreated
protected void Repeater_OnItemCreated(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.FindControl(ctrl);
}
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.FindControl(ctrl);
}
}
By:
How to find controls in a repeater header or footer

Get dynamic controls and text in Repeater

I have a Repeater which loads some data from a SQL database
<asp:Repeater ID="Repeater" runat="server" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:Label ID="QuestionLabel" runat="server" Text=""></asp:Label>
<asp:TextBox ID="AnswerLabel" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="AnswerSubmit" runat="server" Text="Insert"/>
In code behind, i assign to get the questions on another button click to load and bind the Repeater. In ItemDataBound i find the controls and assign the questions to the label.
How should i get the answers that the user enters and store the ID of the question with the answer they enter in a button click event?
At first, I tried this in the button click event
foreach (RepeaterItem item in Repeater.Items)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
but it couldnt find the QuestionLabel. Looking at the page source i believe thats due to the label having a different value (QuestionLabel_0, QuestionLabel_1 etc), so struggling to find a way to approach this?
Edit
protected void QuestionButton_Click(object sender, EventArgs e)
{
Repeater.DataSource = QuestionsByID(ID);
Repeater.DataBind();
}
You see
foreach (RepeaterItem item in Repeater.Items)
{
if (item.ItemType == ListItemType.Item
|| item.ItemType == ListItemType.AlternatingItem)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
}

Binding controls dynamically to PlaceHolder in asp:DataList

I can't seem to figure this one out but I'm trying to add a user control to a DataList at runtime (because the actual control type can differ). So if I hard code the control reference in the markup like this it works:
<asp:DataList ID="myDL" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemDataBound="myDL_Item_Bound">
<ItemTemplate>
<prefix:MyControl ID="myControl1" runat="server" />
</ItemTemplate>
</asp:DataList>
But if I try to add it programmatically to a placeholder, it does not render the user controls (just empty td tags):
<asp:DataList ID="myDL" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemDataBound="myDL_Item_Bound">
<ItemTemplate>
<asp:PlaceHolder ID="ph" runat="server">
</asp:PlaceHolder>
</ItemTemplate>
</asp:DataList>
protected void myDL_Item_Bound(Object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
if (ph != null) {
MyControl ctrl = new MyControl();
ctrl.SomeProp = "xyz";
ph.Controls.Add(ctrl);
}
else {
MyControl ctrl = (MyControl)e.Item.FindControl("myControl1");
ctrl.SomeProp = "xyz";
}
}
}
what am I missing?
You are not adding the control to the page. You need to add it:
Control ctrl = (Control)Page.LoadControl("MyControl.ascx");
// MyControl ctrl = new MyControl();
ctrl.SomeProp = "xyz";
ph.Controls.Add(ctrl);

Accessing a control within a Repeater

Lets say I have the following:
<asp:Repeater ID="repSubItems" runat="server" DataSource="<%# SubItems %>" >
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1"
FieldName="BlurbSpot_Content_SubHeading"
runat="server"
Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
</ItemTemplate>
</asp:Repeater>
I want to in code behind be able to do:
FieldRenderer1.Style["Width"] = MyCoolWidth;
But within the Repeater I cannot access the FieldRenderer1 control.
You will need to handle the ItemDataBound event of the repSubItems repeater. Example:
protected void repSubItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var fieldRenderer1 = e.Item.FindControl("FieldRenderer1") as Sitecore.Web.UI.WebControls.FieldRenderer;
if (fieldRenderer1 != null)
{
fieldRenderer1.Style["Width"] = MyCoolWidth;
}
}
}
You need to find the row your are looking for specifically in the Repeater and then find the control. Here is an example that can do it for all items in your Repeater:
// repeater item
foreach (Control cr in repSubItems.Controls)
{
// assuming this is your templated control name and not the final output name
FieldRenderer founcControl = cr.FindControl("FieldRenderer1") as FieldRenderer;
founcControl .Style["Width"] = MyCoolWidth;
}
The better way to do this would be to implement the OnDataBinding for your control specifically because then you have no searching to do:
<sc:FieldRenderer ID="FieldRenderer1" FieldName="BlurbSpot_Content_SubHeading"
runat="server" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>"
OnDataBinding="FieldRenderer1_DataBinding" />
protected void FieldRenderer1_DataBinding(object sender, System.EventArgs e)
{
FieldRenderer rend = (FieldRenderer)(sender);
// you can do whatever you want to rend at this point and it is scoped to ONLY
// the control so you never have to search for it.
rend.Style["Width"] = MyCoolWidth;
}

control image visibility in repeater based on session variable

I'm having trouble figuring out how to control the visibility of an image in a gridview based on a session variable.
<asp:Image runat="server" ID="imgImportedData" Width="20px" Height="20px" ImageUrl="~/images/warning.png" CausesValidation="false" />
I tried using Visible='<%# mySessionVariable %>' but I got a message saying mySessionVariable was unavailable. I think this is because it's in a grid because I am using this variable in the code behind for another part of the page outside of the gridview without any problems.
EDIT: I just realized this in a Repeater control and not a GridView.
I tried both of these and still get The name 'MySession' does not exist in the current context
Visible='<%# (bool)MySession.IsImportedData == "true" ? true : false %>'
Visible='<%# MySession.IsImportedData == "true" ? true : false %>'
<%# is a DataBinding ASP server tag. What happens when you change <%# to <%=?
If that doesn't work, I would suggest setting the column's visibility in a RowDataBound event, like so:
MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image imgImportedData = (Image) e.Row.FindControl("imgImportedData");
// Assuming that mySessionVariable isn't already a bool, which it really should be.
imgImportedData.Visible = bool.Parse(mySessionVariable);
}
}
Try this:
<asp:Image runat="server" ID="imgImportedData"
Visible='<%# Session["mySessionVariable"] == "foo" ? true : false %>' />
I got this to work. Thank you for everyone's help. I found an example on this page that helped.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
protected void rptAlternateNames_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem != null)
{
Image imageImportedData = ((Image)e.Item.FindControl("imgImportedData"));
if (MySession.IsImportedData)
{
imageImportedData.Visible = true;
}
}
}
}

Categories

Resources