Prevent DataList to Refesh for ClickEvents - c#

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

Related

Cant find a combobox control in edittemplate in Gridview

I am trying to find this control in the Gridview's edititemtemplate section.
<EditItemTemplate>
<ajaxToolkit:ComboBox ID="GridviewCategoryComboBox1" AppendDataBoundItems="true" runat="server" AutoCompleteMode="Suggest" DataSourceID="GridViewCategorySqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" Style="display: inline;">
<asp:ListItem>Select Category</asp:ListItem>
</ajaxToolkit:ComboBox>
Here is the event handler where I try to fetch the control that is in the edititem template.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
int id = (int)GridView1.DataKeys[e.NewEditIndex].Value;
ComboBox ddl = GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("GridviewCategoryComboBox1") as ComboBox;
}
It returns null, no matter who I try to find it.
I also tried other variations such as this:
ComboBox ddl = GridView1.Rows[e.NewEditIndex].FindControl("GridviewCategoryComboBox1") as ComboBox;
You can use the RowDataBound event for this:
protected void GridView1_RowDataBound(object sender, GridViewEditEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
ComboBox ddl = (ComboBox)e.Row.FindControl("GridviewCategoryComboBox1");
}
}
}
Because it is likely that you have other code in the RowDataBound event, then this allows you to centralize all your code in that event and avoid duplicate code.

How to get text in textbox inside an asp.net repeater on postback?

I bind data to the repeater on Page Init:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
repeaterInfo.DataSource = //getDataSource;
repeaterInfo.DataBind();
}
}
Here's the markup page
<table class="beautifulTable">
<asp:Repeater runat="server" ID="repeaterInfo" OnItemCreated="repeaterInfo_ItemCreated">
<ItemTemplate>
<tr>
<td style="display: none">
<asp:TextBox runat="server" Width="90%" ID="txtUserInput"></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
Here's the Item created event:
protected void repeaterInfo_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem == null)
return;
TextBox txtUserInput= e.Item.FindControl("txtUserInput") as TextBox;
txtUserInput.Text = "0.0"; //Default value
}
And I would like to save the user input to database when the user clicks the submit button:
protected void btnSubmit_Click(object sender, EventArgs e)
{
List<repeaterType> source = repeaterInfo.DataSource as List<repeaterType>;
for (int z = 0; z < source.Count; z++)
{
TextBox txtUserInput = repeaterInfo.Items[z].FindControl("txtUserInput") as TextBox;
//Get text and do logic here
}
//Saves data to database
}
And here's the problem:
The repeaterInfo datasource is null on postback
If I remove the !IsPostBack, the txtUserInput text will be resetted (0.0)
I've enabled the viewstate on the markup page using EnableViewState="true"
How I can get the text in txtUserInput?
The repeater's datasource is not persisted in cross postbacks. If you just want to iterate and get the user inputs, you can just iterate through the items and get those like this:
foreach (RepeaterItem item in repeaterInfo.Items)
{
if(item.ItemType == ListItemType.Item)
{
var txtUserInput = item.FindControl("txtUserInput") as TextBox;
}
}
If you want the datasource to be persisted (may be to avoid database calls), use the ViewState (watch out for large number of rows):
ViewState["myDataSource"] = myDatasource;

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

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

Getting DropDownList values in a repeater

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

Categories

Resources