Getting DropDownList values in a repeater - c#

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();
}

Related

Prevent DataList to Refesh for ClickEvents

I have a DataList Control with HeaderTemplate as LinkButtons.
The markup is
<asp:DataList runat="server" ID="dlYears" OnItemDataBound="dlYears_ItemDataBound">
<HeaderTemplate>
<asp:LinkButton ID="lblYear1" runat="server" OnClick="Year_Click"></asp:LinkButton>
<asp:LinkButton ID="lblYear2" runat="server" OnClick="Year_Click"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblValue" runat="server"Text='<%# String.Format("{0:f2}",DataBinder.Eval(((IDataItemContainer)Container).DataItem, "Value")) %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
Actually this datalist contains year numbers as Headers as 2016, 2015.
Binding as
dlYearlys.DataSource = ds;
dlYearlys.DataBind();
On the ItemDataBound I'm styling the values of totals as
protected void dlYearlyTotals_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int i = e.Item.ItemIndex;
if (val != ((Label)e.Item.FindControl("lblValue")).Text)
e.Item.ForeColor = System.Drawing.Color.Red;
}
}
On clicking the headers I'm displaying the another datalist values.
protected void Year_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
BindAnotherDataList((Convert.ToInt16(btn.Text)));
}
This causing the page to refresh and the year values where I'm changing the colour as red is no more being displayed.
So how can I make dlYears Datalist control not to refresh and retain their colour on clicking of headers
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinddlYear();
// Binding Some other controls
}
}

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

Nested repeater not being triggered

I have three repeaters with a parent child relationship (so we have a parent repeater, and child repeater, and a child-child repeater) with the inner-most repeater not being triggered. here is my aspx page for the layout:
<asp:Repeater ID="rptMission" runat="server">
<HeaderTemplate>
<ul id="acc1" class="ui-accordion-container">
</HeaderTemplate>
<ItemTemplate>
<li>
<div class="ui-accordion-left"></div>
<a class="ui-accordion-link acc1"><%# Eval("Name") %><span class="ui-accordion-right"></span></a>
<div>
<ul class="ui-accordion-container" id="acc2">
<asp:Repeater ID="rptActivity" runat="server">
<ItemTemplate>
<li>
<div class="ui-accordion-left"></div>
<a class="ui-accordion-link acc2"><%# Eval("Name") %>
<span class="ui-accordion-right"></span></a>
<div>
<asp:Repeater ID="rptProject" runat="server">
<ItemTemplate>
<%# Eval("Name") %><br/>
</ItemTemplate>
</asp:Repeater>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
The top two repeaters work great but the 'rptProject' repeater doesn't get triggered. Here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
_presenter = new TierTypesPresenter(this);
rptMission.ItemDataBound += new RepeaterItemEventHandler(rptMission_ItemDataBound);
RaiseStartUp();
}
void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Mission row = (Mission)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptActivity = (Repeater)item.FindControl("rptActivity");
var activity = _presenter.GetActivitiesByMission(row.Id);
rptActivity.DataSource = activity;
rptActivity.DataBind();
}
}
void rptActivity_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Activity row = (Activity)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptProject = (Repeater)item.FindControl("rptProject");
var project = _presenter.GetProjectsByActivities(row.Id);
rptProject.DataSource = project;
rptProject.DataBind();
}
}
public void SetMissions(IEnumerable<Mission> missionList)
{
rptMission.DataSource = missionList;
rptMission.DataBind();
}
If I could trigger the second ItemDataBound event 'rptActivity_ItemDataBound' I think it would work fine but it gets skipped over. Thanks for any insight!
I solved my problem by putting the third repeater binding into the second repeaters ItemBoundEvent. Here is the updated code for any interested:
void rptMission_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Mission row = (Mission)item.DataItem;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rptActivity = (Repeater)item.FindControl("rptActivity");
var activity = _presenter.GetActivitiesByMission(row.Id);
var i = 0;
foreach (Activity data in activity)
{
RepeaterItem activityItem = rptActivity.Items[i];
var rptProject = (Repeater)activityItem.FindControl("rptProject");
var project = _presenter.GetProjectsByActivities(data.Id);
rptProject.DataSource = project;
rptProject.DataBind();
i++;
}
rptActivity.DataSource = activity;
rptActivity.DataBind();
}
}
I guess you are missing this line of code:
rptActivity .ItemDataBound += new RepeaterItemEventHandler(rptActivity_ItemDataBound);
It is easier to set the events in the asp file by the way...

Edit Textbox in Repeater

I have a repeater with a textbox inside. I am trying to edit the information inside the textbox, retrieve the new data, and write to the DB. With my code its giving me the original info that was in the box. Not the new information that I have added. Here is my code
html:
<asp:LinkButton id="saveReviewLinkButton" text="Save" runat="server" onCommand="saveReviewLinkButton_OnCommand" />
<table>
<asp:Repeater id="ReviewRepeater" runat="server" onItemDataBound="ReviewRepeater_ItemDataBound">
<itemtemplate>
<tr >
<td ><asp:TextBox id="titleLabel" runat="server" width="200px" textMode="MultiLine"/></td>
</tr>
</itemtemplate>
</table>
c#:
protected void ReviewRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
Review review = (Review)e.Item.DataItem;
TextBox titleLabel = (TextBox)e.Item.FindControl("titleLabel");
titleLabel.Text = review.Title;
}
}
protected void saveReviewLinkButton_OnCommand(object sender, EventArgs e)
{
TextBox titleLabel = new TextBox();
foreach (RepeaterItem dataItem in ReviewRepeater.Items)
{
titleLabel = (TextBox)dataItem.FindControl("titleLabel");
string newInfo = titleLabel.Text;
}
}
Please make sure, you are binding the data to the repeater by checking in page load
if(!IsPostBack)
BindData();

Get selected dropdownlist value inside a repeater control

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.
}

Categories

Resources