How to get the total count of rows in a repeater? - asp.net

I am using a Repeater control to display data, The data source contains multiple records
which are displayed in the repeater, what i need is to get the difference of two list items and assign it to another list item, I have used the below mentioned code works fine for the first record but doesn't display all the records
aspx.cs Code
DataSet dsJobCardHistory =new DataSet();
double dcPriceIncl;
double dcPriceExcl;
double dcTax;
protected void Job_History()
{
dsJobCardHistory = objReportManager.Get_JobCard_History(strCustNo, strTranId);
dcPriceIncl = Convert.ToDouble(dsJobCardHistory.Tables[0].Rows[0][6].ToString());
dcTax = dcPriceIncl * 0.14;
dcPriceExcl = dcPriceIncl - dcTax;
}
protected void repJobCard_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
((Label)e.Item.FindControl("lblRepPrice")).Text =
Convert.ToString(dcPriceExcl);
((Label)e.Item.FindControl("lblRepTax")).Text =
Convert.ToString(dcTax);
((Label)e.Item.FindControl("lblRepTotal")).Text=
Convert.ToString(dcPriceIncl);
}
}
}
aspx code
<font style="font-size:14px"><strong><asp:Label ID="lblRepPrice" runat="server"
Text=""></asp:Label></strong></font>
</td>
<td align="center" width="15%">
<font style="font-size:14px"><strong><asp:Label ID="lblRepTax" runat="server"
Text=""></asp:Label></strong></font>
</td>
<td align="center" width="15%">
<font style="font-size:14px"><strong><asp:Label ID="lblRepTotal" runat="server"
Text=""></asp:Label></strong></font>
</td>

As u said its working for first time only so Replace Your this line
if (e.Item.ItemType == ListItemType.Item)
with....
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)

Give the [xp:repeat] an ID value e.g. idvaluestring
in SSJS uses getComponent("idvalueString").getRowCount()
The Class of the RepeatControl is:
com.ibm.xsp.component.xp.XspDataIterator
<xp:repeat var="voyageChoicesCollection"
indexVar="voyageChoicesCollectionIndex"
value="#{ProfileListRepeat}"
id="voyageChoicesCollectionID">
var vccID:com.ibm.xsp.component.xp.XspDataIterator = getComponent("voyageChoicesCollectionID");
vccID.getRowCount()

Related

Is it possible in Gridview to show different images representing different database values?

I have an fieldname called has_Amenities. This fieldname can take one, two, three, four, or five values at same time.
The values are water, electricity, pets, Full hookup, All
If has_Amenities = "electricity", then show electricity icon but also show blank icons for pets, full hookup and water tap.
If has_Amenities = "pets", show pets icon as well as blank icons for electricity, full hookup and water
If has_Amenities = "water", show water icon and also show blank icons for pets, full hookup and electricity.
If has_Amenities = "full", show full hookup icon and also show blank icons for pets, water and electricity.
If has_Amenities = "All", then it means it has electricity, water, full hookup and pets. Show all four icons.
We are using gridview and gridview control id is gridview1.
The code below works by displaying just one value - Electricity.
<asp:TemplateField HeaderText="Facility Has">
<ItemTemplate>
<asp:Image ID="ImageDetailItem" width="22" height="22" ImageUrl='<%# IIF(CONVERT.ToString(Eval("has_Amenities")) = "Electricity", "~/images/icon_amps_50.gif", Eval("location","~/images/icon_waterfront_no.gif")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
This is not the correct solution.
How do I use all the IF conditions mentioned above and how do I show all four icons in one cell?
Below is an image of how they are laid out.
You can put this logic in RowDataBound event. The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.
Here is example code that you can use for your problem.
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drv As Common.DbDataRecord = CType(e.Row.DataItem, Common.DbDataRecord)
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
Image img = e.row.Cells(7).FindControl("image1")
if drv("has_Amenities") = "Electricity" Then
img.ImageUrl = "~/images/icon_amps_50.gif"
elseif
' ////////////////////////////////
' You can place other logic here...
End If
End Sub
I did similar things in asp.net webforms using c# like this (i hope VB is not much different)
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Electricity")? "~/electricity.gif":"~/blank.gif" %>"/>
<asp:Image ID="Image1" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Pets")? "~/pets.gif":"~/blank.gif" %>"/>
<asp:Image ID="Image2" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Water")? "~/water.gif":"~/blank.gif" %>"/>
</ItemTemplate>
</asp:TemplateField>
The page class had protected method,containing all logic
protected bool ProtectedIsVisible(string[] values,string tag)
And I used ItemType property of GridView, set to strongly typed model class,something like
class Model{ public string[] HasAmenties {get;set; }
<asp:GridView ItemType="MyApplication.Model" runat="server" ID="MyGridView">
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// set formatting for the category cell
TableCell cell = e.Row.Cells[2];
// set formatting for value cells
for (int i = 2; i < e.Row.Cells.Count; i++)
{
cell = e.Row.Cells[i];
int c_val = int.Parse(e.Row.Cells[i].Text);
if (c_val == 0)
{
//cell.BackColor = System.Drawing.Color.Red;text-align: center;
cell.ForeColor = System.Drawing.Color.Red;
cell.Attributes.Add("style", "text-align: center;");
cell.Text = "<i class='fa fa-remove'></i>";
//cell.Attributes.Add("Class", "fa fa-remove");
}
else
{
cell.ForeColor = System.Drawing.Color.Green;
cell.Attributes.Add("style", "text-align: center;");
cell.Text = "<i class='fa fa-check'></i>";
//cell.Attributes.Add("Class", "fa fa-check");
}
}
}
else if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 2; i < e.Row.Cells.Count; i++)
{
string Header_NewText = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = Header_NewText.Replace(",", "<br/>");
}
}
}

RadioButton Selection in GridView

Here is my requirement.
I had 3 groups and having set of emplyees under each group.
So, I had placed 3 labels(To display groupname) 3 gridviews(to dispalay set of employees from each group) in a DataList. all the rows in 3 GridViews having RadioButtons.
Now problem is RadioButton is behaving like a checkbox, it is allowing multiple selections.
But, I need to select Only one RADIOBUTTON among all 3 Gridviews. How this can be acheived. Can Someone here help me?
/********* Displaying Group Name and Its set of employes **********/
/*** This loop will execute 3 times as I had 3 groups. ****/
protected void dlGraphItemDataBound(object sender, DataListItemEventArgs e)
{
MyList dataitem = (MyList)e.Item.DataItem;
if (dataitem != null)
{
Label lbl = (Label)e.Item.FindControl("lblMyLabel");
lbl.Text = dataitem.GroupName;
GridView dg = (GridView)e.Item.FindControl("gvList");
if (dg != null)
{
List< MyList > groupItem = new List< MyList >(); // List of Employees
foreach (MyList item in _empList)
{
if (item.GroupName.Equals(dataitem.GroupName))
groupItem.Add(item); // Grouping all the emps who had same groupname
}
SetupReportGrid(dg); // Method to add controls to gridview dynamically
dg.SetDataSource(groupItem); //Setting datasource for gridview
}
}
}
protected void onRadioButtonClicked(object sender, EventArgs e)
{
foreach (DataListItem dlItem in dlMyDataList.Items)
{
GridView grid = (GridView) dlItem.FindControl("gvList");
if (grid != null)
{
RadioButton selectButton = (RadioButton) sender;
GridViewRow row = (GridViewRow) selectButton.NamingContainer;
int a = row.RowIndex;
foreach (GridViewRow gridRow in grid.Rows)
{
RadioButton rd = gridRow.FindControl("rdoSelect") as RadioButton;
if (rd.Checked)
{
if (gridRow.RowIndex == a)
{
rd.Checked = true;
}
else
{
rd.Checked = false;
}
}
}
}
}
This is how i tried.... when i select first and second radiobutton in 1st gridview and after the event for 2nd radiobutton both 1st and 2nd radiobuttons got unchecked. As the loop executes for all gridviews and last grid having no radiobuttons checked. finally my code results as no radiobuttons checked
Grid View Mark up:
<asp:DataList ID="dlMyDataList" runat="server" OnItemDataBound="dlGraphItemDataBound">
<ItemTemplate>
<table cellspacing="0" width="100%" >
<tr>
<td nowrap="nowrap" width="100%" align="left" valign="middle">
<asp:Label ID="lblGroupName" runat="server" CssClass="ssrptsublabel" > </asp:Label>
</td>
</tr>
<tr>
<td width="100%" nowrap="nowrap" align="left" valign="middle">
<asp:Panel id="pnlReport" runat="server" BorderWidth="0" Width="100%" Wrap="False">
<commoncontrols:MyGridView id="gvList" runat="server" autogeneratecolumns="false" EnableViewState="True">
</commoncontrols:MyGridViewview>
</asp:Panel>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
**// Assigning controls dynamically to grid view
// Having Seperate customized class for gridview, based on that we are adding controls
//
GridViewColumnControl(ControlTypes, Control_ID, CSS Style, 0, databind,"" );**
private void SetupGrid(GridView grid)
{
IList<GridViewColumn> columns = new List<GridViewColumn>();
GridViewColumn gridColumn = new GridViewColumn(ColumnTypes.TemplateColumn, "", 500, HorizontalAlign.Left,null);
GridViewColumnControl control = new GridViewColumnControl(ControlTypes.RadioButton, "rdoSelect", "labelstyle", 0, null, "");
control.Visible = false;
control.AutoPostBack = true;
control.OnChanged += onRadioButtonClicked;
gridColumn.AddControl(control);
control = new GridViewColumnControl(ControlTypes.DropDown, "", "style", 0, null, "");
control.Visible = false;
gridColumn.AddControl(control);
grid.SetColumns(columns);
}
The RadioButton control has a property called GroupName. All radio buttons having the same GroupName belong to the same group. In each radio button group only one radio button can be seleted. So I believe that if you set this property for all radio buttons in the grid views, you will fulfill your requirement.

Managing images in asp.net repeater (add class to each picture)

This is my code:
Codebehind:
public class data
....
...
public List<dataImages> Images { get; set; }
...
var data= GarageBLL.LoadData(Convert.ToInt32(DataId), Convert.ToInt32(MemberId));
rptImages.DataSource = data.Images.Take(3);
rptImages.DataBind();
aspx:
<asp:Repeater runat="server" ID="rptImages">
<ItemTemplate>
<asp:Image runat="server" CssClass="img1" ImageUrl='<%# String.Format("/images/{0}/{1}.{2}", DataId, Eval("ImageId"), Eval("Extension")) %>' />
</ItemTemplate>
</asp:Repeater>
So, this works at the moment, but i would like to add different classes to each image.
Anyone got any idea how i can do that?
You can use repeater's ItemDataBound event to set the CssClass:
protected void rptImages_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
dataImages img = (dataImages) e.Item.DataItem;
Image img1 = (Image) e.Item.FindControl("img1");
// add your correct logic here according to the dataImages properties
img.CssClass = "YourCssClass";
// assuming you just want different classes for your three images, use ItemIndex with remainder:
string class = "img" + (e.Item.ItemIndex % 3 + 1).ToString() + "class";
img.CssClass = class;
}
}

Embedded code in repeater

I am using a databound repeater component with some click-sensitive panel inside.
<ItemTemplate>
<asp:Panel ID="PanelContent" runat="server">
<asp:Panel ID="PanelMenuTitle" runat="server"
ondblclick="EditMenu(<%# Eval("ID") %>)">
As you can see I want to pass the ID of the current data item to a javascript function called EditMenu().
However, this code breaks due to "The server tag is not well formed.". I also tried everything I can think of: Using <%= instead of <%#, Bind() instead of Eval(), ' instead of " without success.
Use single quotes around the ondblclick function. That should fix it. See below.
ondblclick='EditMenu(<%# Eval("ID") %>)'
My second suggestion would be to use another control, like a ListView or a DataList, so that you can assign data keys to hold the ID. Then you can assign the ondblclick event in the ItemDataBound event like this.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Panel pnlCtrl = (Panel)e.Item.FindControl("Panel1");
if (pnlCtrl != null)
{
pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu({0})", ListView1.DataKeys[((ListViewDataItem)e.Item).DisplayIndex]["ID"]);
}
}
}
Alright, I got it working. Not the original approach, but in the end it's what I wanted:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel pnlCtrl = (Panel)e.Item.FindControl("PanelMenuTitle");
if (pnlCtrl != null)
{
myMenu menu = (e.Item.DataItem as myMenu);
pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu('{0}')", menu.ID);
}
}

Adding <tr> from repeater's ItemDataBound Event

My repeater's templates generate a table, where each item is a table row.
When a very very specific condition is met (itemdata), I want to add an additional row to the table from this event.
How can I do that?
protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
bool tmp = bool.Parse(DataBinder.Eval(e.Item.DataItem, "somedata").ToString());
if (!tmp && e.Item.ItemIndex != 0)
{
//Add row after this item
}
}
}
I can use e.Item.Controls.Add() and add TableRow but for that I need to locate a table right?
How can I solve that?
I would put in the control in the repeater item template, set its visibility to hidden & only in that case, I will show it...
Like:
<asp:Repeater ...>
..
<ItemTemplate>
<tr>
<td>...</td>
</tr>
<tr runat="server" id="tr_condition" Visible="false">
<td> Show only in specific condition </td>
</tr>
Then in the ItemDataBound event:
var tr_condition = e.Item.FindControl("tr_condition") as HtmlTableRow;
tr_condition.Visible = myCondition;
where myCondition is a flag which gets sets on the specific condition
HTH
One way to do this is to include the row in the template code, and set the Visible property based on your 'tmp' variable. The you don't need to 'add' it - it is already there.
Edit: another idea - insert your extra row using a <%# %> data binding block. To do this, you need to generate the <tr><td>.. code as a atring in your ItemDataBound event. Use a protected string variable - like this:
protected string _extraRowHtml;
in ItemDataBound:
_extraRowHtml = tmp ? "<tr><td> -- generated content -- </td></tr>" : string.Empty;
in ItemTemplate:
<tr><td> -- regular row content --</td></tr>
<%# _extraRowHtml %>

Resources