I'm trying to get the item count from a datalist that sits inside a datalist. I thought this is how I would do it but its returning null. (aspx code condensed for readability)
<asp:DataList id="searchResultsProductDataList" runat="server" >
<asp:DataList ID="productDataList" runat="server">
</asp:DataList>
</asp:DataList>
Here is the code-behind
DataList resultnumberDL = (DataList)e.Item.FindControl("productDataList");
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
I've also tried
DataList resultnumberDL = ((DataList)FindControl("productDataList"));
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
This is how I would go about doing this right?
This can be done like this in DataList1_ItemDataBound
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");
Label SalePrice = (Label)e.Item.FindControl("SalePrice");
//
// Do you calculations here ..
//
SalePrice.Text = "Your Final Value";
}
}
Maybe double-check your syntax...
If your ASP.NET controls are structured like this:
<asp:DataList ID="dl1" runat="server" onitemdatabound="dl1_ItemDataBound">
<ItemTemplate>
...
<asp:DataList ID="dl2" runat="server" Enabled="true">
<ItemTemplate>
...
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
and your C# code-behind for the nested DataList like this:
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataList dl2 = (DataList)e.Item.FindControl("dl2");
... // load DataTable
dl2.DataSource = dt;
dl2.DataBind();
}
in this case e.Item.FindControl("[id]") will find your nested DataList
Related
I have a repeater that in it has one dropdown list and one linkbutton.
I want to get the selected value of the dropdown list by CommandArgument in linkbutton, but it just knows default value of dropdown list.
My code :
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Page_Load2"OnItemCommand="list_ItemCommand" >
<ItemTemplate>
<asp:DropDownList ID="dlPricelist" CssClass="width100darsad dropdownlist" runat="server" AutoPostBack="true" ViewStateMode="Enabled" >
</asp:DropDownList>
<asp:LinkButton ID="btnAddToCart" runat="server" class="btn btn-success btnAddtoCardSinglepage" CommandArgument='<%#Eval("id") %>' CommandName="addtocard">اضافه به سبد خرید</asp:LinkButton>
<asp:Label ID="xxxxxx" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Page_Load2(object sender, RepeaterItemEventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"].ToString();
DataSet dsselectcategory = BLLTour.left3join(id.Trim().ToString());
var dlPricelist = (DropDownList)e.Item.FindControl("dlPricelist");
dlPricelist.DataSource = dsselectcategory.Tables[0];
dlPricelist.DataTextField = "pricelistPrice";
dlPricelist.DataValueField = "priceid";
dlPricelist.DataBind();
}
}
protected void list_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addtocard")
{
foreach (RepeaterItem dataItem in Repeater1.Items)
{
Label xxxxxx = (Label)e.Item.FindControl("xxxxxx");
LinkButton btnAddToCart = (LinkButton)e.Item.FindControl("btnAddToCart");
xxxxxx.Text = ((DropDownList)dataItem.FindControl("dlPricelist")).SelectedItem.Text; //No error
}
}
}
I don't know how I should fix it.
You are using very old technology to show data that's not appropriate.
But if you are serious to use it, you must use FindControll method in ItemTemplate of your repeater control. You should find dropdownlist first, and then cast it to a object to be able to use it's value.
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
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;
}
I want to add a dropdownlist to every entry in a gridview.
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:DropDownList ID="DropDown"
AutoPostBack="true" runat="server" DataTextField="Name" DataValueField="Name"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
At the back end i have the following code in order to bind a datatable to that dropdown list.
DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList();
DropDown.DataSource = reader;
DropDown.DataTextField = "NAME";
DropDown.DataValueField = "NAME";
DropDown.DataBind();
My problem is that the drop down list created at the grid view (DropDown) is not found at the back end as if it doesn't exist..
What can I do?
The DropDownList will be created for every single item in the GridView, so there can't be one field for the dropdownlists. Nevertheless, you can retrieve the DropDownList for a single row (e.g. in RowDataBound or RowCreated event)
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(r.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dropdown = e.Row.FindControl("DropDown") as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
}
Or you can use an event of the DropDownList itself and access the sender parameter.
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
you can find dropdown into grid databound event by grid.findcontrol.
I have a list of files (stored in db which I get using Linq), I want to put them in ASP ListView (or something better for this case if you can suggest).
This files are attachments to policies, so one policy may have them more than one.
ListView will be placed in DataGrid's field, but I couldn't find a way to put it there and this is my question. I also want to be able to download these files, but I don't have elements that I could check if they were selected.
Additional info:
I bind data with DataGrid by
myDataGrid.DataSource = linqQuery.ToList();
myDataGrid.DataKeyField = "IdPolicy";
myDataGrid.DataBind();
In aspx I have Panel -> DataGrid -> Columns -> BoundColumns (with DataFields).
Please help.
You can loop through each item of DataGrid and populate ListView.
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false"
OnItemDataBound="DataGrid1_DataBound">
<Columns>
<asp:BoundColumn DataField="IdPolicy" HeaderText="Policy" ReadOnly="True" />
<asp:TemplateColumn HeaderText="Files">
<ItemTemplate>
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="Panel1">
<LayoutTemplate>
<asp:Panel ID="Panel1" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<%# Container.DataItem %><br />
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
public partial class WebForm1 : System.Web.UI.Page
{
public class Policy
{
public int IdPolicy { get; set; }
public List<string> Files { get; set; }
}
private List<Policy> _policies;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_policies = new List<Policy>();
_policies.Add(new Policy
{
IdPolicy = 1,
Files = new List<string> {"One.jpg", "Two.jpg"}
});
_policies.Add(new Policy
{
IdPolicy = 2,
Files = new List<string> {"TwentyOne.jpg", "TwentyTwo.jpg"}
});
DataGrid1.DataSource = _policies;
DataGrid1.DataBind();
}
}
protected void DataGrid1_DataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var policy = e.Item.DataItem as Policy;
var listView1 = e.Item.FindControl("ListView1") as ListView;
listView1.DataSource = policy.Files;
listView1.DataBind();
}
}
}
Use a GridTemplateColumn, and add the ListView to the ItemTemplate. The ListView will be nested in the DataGrid, so you will need to bind it programatically in the ItemDataBound event (or elsewhere).
((ListView)e.Item.FindControl("ListView1")).DataSource = ListViewDataSource;