im trying to change a value inside my repeater : (via itemdatabound event)
if the year is empty - set value blabla
my repeater :
<ItemTemplate>
<tr >
<td >
<%#Eval("year") %>
</td>
my c# code :
void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (string.IsNullOrEmpty(((DataRowView)e.Item.DataItem)["year"].ToString()))
{
(((DataRowView)e.Item.DataItem)["year"]) = "blabla"; // ???????
}
}
it does change but not displayed in repeater ( the old value is displayed).
one solution is to add a server control or literal ( runat server) in the itemTemplate - and to "findControl" in server - and change its value.
other solution is by jQuery - to search the empty last TD.
but - my question :
is there any other server side solution ( ) ?
you can try something like this :
Repeater in .aspx:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td> <%# GetText(Container.DataItem) %></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
.cs :
protected static string GetText(object dataItem)
{
string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
if (!string.IsNullOrEmpty(year))
{
return year;
}
else
{
return "blahblah";
}
}
IN GetText Method you can able to check by string that Empty or not than return string.
You could try to use the itemcreated event which occurs before the control is bound and not after the control is bound. Example in first link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcreated.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx
ItemDataBound Occurs after an item in the Repeater control is data-bound but before it is rendered on the page.
HTML FILE
<asp:Repeater ID="RPT_Bordereaux" runat="server">
<ItemTemplate>
<table>
<tr>
<td> <%# GetValue(Container.DataItem) %></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
.CS CODE
protected void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
}
}
protected static string GetValue(object dataItem)
{
string year = Convert.ToString(DataBinder.Eval(dataItem, "year"));
if (!string.IsNullOrEmpty(year))
{
return Convert.ToString(year);
}
else
{
return "blahbla";
}
}
This should work
Related
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
I need to create a data-bound control where each item renders <tbody><ItemTemplate></tbody>. Further, I'd also like to put the <HeaderTemplate> in a <thead> rather than a <tbody> row as the <asp:DataList> does. I need full control of the item template. It needs to be able to render an editable row, like the DataList does.
I believe I need to make a custom control for this (but if not, please let me know--it would save a lot of time). What's the best parent class to extend? My guess is that it's either DataList or DataBoundControl.
Looks like what I'm looking for is <asp:Repeater> with a little extra code for editing since it doesn't have an <EditItemTemplate>.
.ascx
<asp:Repeater id="rItems" runat="server" OnItemCommand="rItems_ItemCommand" OnItemDataBound="rItems_ItemDataBound">
<HeaderTemplate>
<table>
<thead>...</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody id="itemRows" runat="server">
...
<asp:Button ... CommandName="Edit" />
...
</tbody>
<tbody id="editItemRows" runat="server" visible="false">...</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.ascx.cs
private int m_editIndex = -1;
protected void rItems_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
m_editIndex = e.Item.ItemIndex;
break;
... // Cancel, Update, Delete, etc
case "Cancel":
m_editIndex = -1;
break;
}
BindItemsDataSource();
}
protected void rItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
bool edit = e.Item.ItemIndex == m_editIndex;
HtmlGenericControl tbody = e.Item.FindControl("itemRows") as HtmlGenericControl;
tbody.Visible = !edit;
tbody = e.Item.FindControl("editItemRows") as HtmlGenericControl;
tbody.Visible = edit;
if (edit)
{
PopulateEditableFieldValues(e.Item);
}
}
}
I have dropdownlist inside of a repeater control that I'm trying to get the value of and I'm getting "Object reference not set to an instance of an object". I am not sure what else to try. Thanks
ASPX CODE:
<asp:Repeater ID="GeneralRepeater" runat="server"
OnItemDataBound="GeneralRepeater_OnItemDataBound"
onitemcommand="GeneralRepeater_ItemCommand">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style2">
</td>
<td class="style2">
<asp:DropDownList ID="GeneralDDL" AppendDataBoundItems="true" DataTextField="DiagnosisCode"
DataValueField="DiagnosisCode" runat="server" AutoPostBack="True" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
</asp:Panel>
CODE BEHIND:
protected void GeneralRepeater_OnItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
Diagnosis oDiagnosis = new Diagnosis();
DataView dv = new DataView(oDiagnosis.GetDiagnosis());
myDDL.DataSource = dv;
myDDL.DataTextField = "DiagnosisCode";
myDDL.DataValueField = "DiagnosisCode";
myDDL.DataBind();
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
string ProductSelected;
string FeatureSelected;
string SectionSelected;
foreach(RepeaterItem dataItem in GeneralRepeater.Items)
{
ProductSelected = ((DropDownList)GeneralRepeater.FindControl("GeneralDDL")).SelectedItem.Text; //error Object reference not set to an instance of an object.
}
}
I having problem on Saving the selected value..
What i find in the code is you are looping through the generalrepeater items. You are accessing the repeater item as dataItem. I tried out this code out here and ideally your code should say
foreach(RepeaterItem dataItem in GeneralRepeater.Items)
{
ProductSelected = ((DropDownList)dataItem.FindControl("GeneralDDL")).SelectedItem.Text; //No error
}
instead of
foreach(RepeaterItem dataItem in GeneralRepeater.Items)
{
ProductSelected = ((DropDownList)GeneralRepeater.FindControl("GeneralDDL")).SelectedItem.Text; //error Object reference not set to an instance of an object.
}
ASPX PAGE:
<asp:Repeater ID="GeneralRepeater" runat="server"
OnItemDataBound="GeneralRepeater_OnItemDataBound">
<ItemTemplate>
<tr>
<td>
DxPoc:
<asp:DropDownList ID="GeneralDDL" DataTextField="DiagnosisCode"
DataValueField="DiagnosisCode" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
CODE BEHIND:
protected void GeneralRepeater_OnItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
Diagnosis oDiagnosis = new Diagnosis();
DataView dv = new DataView(oDiagnosis.GetDiagnosis());
myDDL.DataSource = dv;
myDDL.DataTextField = "DiagnosisCode";
myDDL.DataValueField = "DiagnosisCode";
myDDL.DataBind();
}
}
The given shown above is not working properly. During page load it nothing happens on the
dropdownlist inside the repeater.
QUESTIONS:
a.) How I'll get the values of my dropdownlist with list of objects inside the repeater?
thanks!
if(!IsPostBack)
{
BindRepeater();
}
I know how to use a simple If statement wrapped in the <%# tags to hide something, but I don't know how to do it in a repeater when I need to access Container.DataItem, as in I need the dataItem currently being 'repeated'
eg
if (CurrentValidationMessage.Link != "")
{
show a hyperlink
}
Markup:
<asp:Repeater ID="repValidationResults" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<a href='<%# ((MttImportValidationMessage)Container.DataItem).EditLink %>'> Link to erroneous Milestone </a>
<%# ((MttImportValidationMessage)Container.DataItem).Message %>
<br />
</ItemTemplate>
</asp:Repeater>
It might be more maintainable if you just tagged the controls in the repeater with id's and runat='server' and reference the DataItem in the ItemDataBound event by using e.Item.DataItem.
Then use e.Item.FindControl to reference your controls in the ItemTemplate and perform your logic.
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Domain.Employee employee = (Domain.Employee)e.Item.DataItem;
Control myControl = (Control)e.Item.FindControl("controlID");
//Perform logic
}
}
use ItemDataBound Event with repeater, and make the "a" tag with a runat="server" property and ID
protected void repValidationResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
{
HyperLink link = (HyperLink) item.FindControl("link");
//Do all your logic here :)
}
}
MarkUp:
<asp:Repeater ID="repValidationResults" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<a runat="server" ID="link"> Link to erroneous Milestone </a>
<%# ((MttImportValidationMessage)Container.DataItem).Message %>
<br />
</ItemTemplate>
</asp:Repeater>