If then else in repeater - c#

I'm trying to control the output from a repeater.
I do have a nice working repeater but would like to further control the display of cirtain values.
So this is my repeater:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="height:27px;" >
<td style="width:20%" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
I would like to do something like this; (Trying to explain my goal in below code with a mix of spegetticode (Classic ASP) with some translation to ASP.NET C#) I hope you can read it!?
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<% If Eval("name") == "aron"
{
If Eval("group") == "1"
{ namecellcolor = "#454545" }
ElseIf Eval("group") == "2"
{ namecellcolor = "#555555" }
ElseIf Eval("group") == "3"
{ namecellcolor = "#666666" }
Else
{ namecellcolor = "#FFFFFF" }
}
ElseIf Eval("name") == "bill"
{ namecellcolor = "#000000" }
Else
{ namecellcolor = "#111111" }
%>
<% If Eval("startdate", "{0:g}") <= DateTime.Now.Date; AND Eval("group") == "1" OR Eval("startdate", "{0:g}") <= DateTime.Now.Date; AND Eval("group") == "2"
{ groupcellcolor = "#010101" }
elseif Eval("startdate", "{0:g}") <= (DateTime.Now.Date; + 2) AND Eval("group") == "1" OR Eval("startdate", "{0:g}") <= (DateTime.Now.Date; + 2) AND Eval("group") == "2"
{ groupcellcolor = "#333333" }
else
{ groupcellcolor = "#000000" }
%>
<tr style="height:27px;" >
<td style="width:20%; background-color: <%# namecellcolor %>" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%; background-color: <%# groupcellcolor %>" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Your answer:
Wow dude, crawl back under that stonedge block of yours and dont bother to come back!
Interesting set of typo's you got there champ!
I will try to actually explain a working solutions to your problem, and here goes;
So what will it be?
I really appreciate any help i can get.

Interesting set of typo's you got there champ!
But I see what you are trying to achieve. You basically need to choose a background color based on the values in the databound object.
You can in theory do all this in the aspx file but as you can see, it's very messy and you quickly lose track of what is what.
A better option is to do this in the backend code. Either just call a method in the Page to determine and return the color, or better, use the ItemDataBound event to manipulate the html after it is rendered by the repeater.
Here is how to do the name cell.
Add an ID to the <td> and make it runat="server" so the code-behind can access it.
Create an ItemDataBound event handler for the repeater.
In the event handler get your data object from e.Item.DataItem and use it to work out the background color. This should be simpler because now you are in code and just have an object (in your case a DataRow as you are using SqlDataSource) instead of all the Eval()s.
Find the <td> control and set it's BgColor.
Something like this should do it (untested but should be close enough):
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource_MyList" EnableViewState="False"
OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="0" style="margin:0px; border-collapse: collapse; border-spacing: 0px; padding:0px;" class="table table-striped table-hover">
<thead>
<tr style="height:35px; font-weight: bold;">
<th style="width:20%" class="toprowcolor">StartNr</th>
<th style="width:40%" class="toprowcolor">Name</th>
<th style="width:30%" class="toprowcolor">StartDate</th>
<th style="width:10%" class="toprowcolor">Group</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="height:27px;" >
<td runat="server" ID="nameCell" style="width:20%" class="listtext_s"><asp:Label ID="startnrLabel" runat="server" Text='<%# Eval("startnr") %>' /></td>
<td style="width:40%" class="listtext_s"><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></td>
<td style="width:30%" class="listtext_s"><asp:Label ID="startdateLabel" runat="server" Text='<%# Eval("startdate", "{0:g}") %>' /></td>
<td style="width:10%" class="listtext_s"><asp:Label ID="groupLabel" runat="server" Text='<%# Eval("group") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// get the data item bound to this row of the repeater
var dataItem = (DataRow)e.Item.DataItem;
// do the logic on the raw object instance
string namecellcolor;
// read the values from the DataRow (may need to cast to a different type, depending om your SQL query).
string itemName = (string)dataItem["name"];
int itemGroup = (int)dataItem["group"];
if(itemName == "aron")
{
if(itemGroup == 1)
namecellcolor = "#454545";
else if(itemGroup == 2)
namecellcolor = "#555555";
else if(itemGroup == 3)
namecellcolor = "#666666";
else
namecellcolor = "#FFFFFF";
}
else if(itemName == "bill")
namecellcolor = "#000000";
else
namecellcolor = "#111111";
// get the table cell and set its BgColor.
var nameCell = (System.Web.UI.HtmlControls.HtmlTableCell)e.Item.FindControl("nameCell");
nameCell.BgColor = namecellcolor;
}

Related

Add the values inside (labels) together that are inside (list view)

How can I add the values inside the labels created in (List View) together?
And show the values added together in a label
<asp:Label ID="lbl_Plural" runat="server" Text="0">
You can just bind the list view, and then loop each row, and get the total.
Say I have this listview:
<h2>Fighter Prints</h2>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr style="">
<td><asp:Label ID="Fighter" runat="server" Text='<%# Eval("Fighter") %>' /></td>
<td><asp:Label ID="Engine" runat="server" Text='<%# Eval("Engine") %>' /></td>
<td><asp:Label ID="Thrust" runat="server" Text='<%# Eval("Thrust") %>' /></td>
<td><asp:Label ID="Description" runat="server" Text = '<%#Eval("Description") %>' /></td>
<td>
<asp:Image ID="iPreview" runat="server" Height="68px" Width="149px"
ImageUrl='<%# Eval("ImagePath") %>'/>
</td>
<td><asp:TextBox ID="Qty" runat="server" Text='<%# Eval("Qty") %>' Width="30px"/></td>
<td><asp:Label ID="Price" runat="server" Text='<%# string.Format("{0:c2}",Eval("Price")) %>'/></td>
<td><asp:Label ID="Total" runat="server" Text='<%# string.Format("{0:c2}",Eval("Total")) %>'/></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" style="" class="table table-hover">
<tbody>
<tr runat="server" style="" >
<th runat="server">Fighter</th>
<th runat="server">Engine</th>
<th runat="server">Thrust (lbs)</th>
<th runat="server">Description</th>
<th runat="server">Preview</th>
<th runat="server">Qty</th>
<th runat="server">Price</th>
<th runat="server">Total</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</tbody>
<tfoot>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td>Total</td>
<td><asp:Label ID="MyTotal" runat="server" Text="0"></asp:Label></td>
</tr>
</tfoot>
</table>
</LayoutTemplate>
</asp:ListView>
Now, in above, I did put in a total footer, but it could have been any control or lable on the page that I stuff the results into.
So, the code to do this?
Bind the ListView
Loop the total, and then put the value into a label, text box, or in this case a label at the footer of the list view.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters ", conn))
{
conn.Open();
DataTable rstData = new DataTable("Fighters");
rstData.Load(cmdSQL.ExecuteReader());
rstData.Columns.Add("Total", typeof(decimal),"[Qty] * [Price]");
ListView1.DataSource = rstData;
ListView1.DataBind();
decimal myTotal = 0;
foreach (ListViewItem OneRow in ListView1.Items)
{
Label MyPrice = OneRow.FindControl("Total") as Label;
myTotal += Decimal.Parse(MyPrice.Text, NumberStyles.Currency);
}
// now update the final total label
Label lblTotal = ListView1.FindControl("MyTotal") as Label;
lblTotal.Text = string.Format("{0:c2}", myTotal);
}
}
}
Results:

Accessing Label Controls from inside the Repeater

I am fairly new to ASP.NET and I am trying to add the number i created in the backend to be inserted into
the label text field in the front-end but Im not being able to access the Label (lblAmountNumber) inside the Repeater i guess it is not allowing me to access the ID=lblAmountNumber
and the name of my repeater is RpMember
I have my simple code down below
FRONT END
<asp:Repeater ID="RpMember" runat="server">
<HeaderTemplate>
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th style="min-width: 100px">
<asp:Label ID="Label2" runat="server" Text="MemberInfo"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label3" runat="server" Text="Amount Number"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label5" runat="server" Text="Loan Balance"></asp:Label></th>
<th style="min-width: 100px">
<asp:Label ID="Label1" runat="server" Text="Savings Balance"></asp:Label></th>
<th style="min-width: 100px" class="ButtonView">
<asp:Label ID="Label6" runat="server"></asp:Label></th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr style="text-align: center">
<td style="width: 100px; text-align: left; text-transform: capitalize"><%#Eval("Name") %>
<br />
<%#Eval("Phone") %>
<br />
<%#Eval("Address") %>
</td>
<td style="width: 100px"><asp:Label ID="lblAmountNumber" runat="server"></asp:Label></th></td>
<td style="width: 100px"><%#Eval("LoanBalance") %></td>
<td style="width: 100px"><%#Eval("SavingsBalance") %></td>
<td style="text-align: center; width: 100px" class="ButtonView">
<asp:LinkButton ID="ReportLinkBtn" runat="server" OnClick="ReportLinkBtn_Click" CommandArgument='<%# Eval("MemberId")%>' CssClass="btn btn-success" Text="Report Details" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
BACKEND
DataTable dt2 = objdalTransactionEntry.GetLoanTakerStartAmountByLoanTakerId(Sessions.Name.UserId, MemberId);
foreach (RepeaterItem item in RpMember.Items)
{
Label lab = item.FindControl("lblHishabNumber") as Label;
string yearForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForHishabNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string yearForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).Year.ToString();
string monthForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("MM");
string dayForInvoiceNumber = Convert.ToDateTime(dt2.Rows[0]["EntryDate"].ToString()).ToString("dd");
lab.Text = "Amount Number " + monthForHishabNumber + yearForHishabNumber.Substring(yearForHishabNumber.Length - 2) + dt2.Rows[0]["UserWiseID"].ToString();
}

Add Row in Listview Itemtemplate after each date records

I have a listview which is bind to a datatable. Datatable have records between two given dates.
I want to calculate day total date wise as show in picture , But day total Row displayafter each record.. I want to show day total of each date group.
Here is my code..
<asp:ListView ID="lvRR" OnItemDataBound="lvRR_DataBound" OnItemCommand="lvRR_ItemCommand" runat="server" ItemPlaceholderID="plchldr" >
<LayoutTemplate>
<table id='tblViewDate' class='tblViewDate' align=center border=0 cellpadding=0 cellspacing=0 width=100% >
<tr class='GridviewScrollHeader'>
<th scope="col" style='min-width:150px !important;font-weight:bold' width="5%" align="left" valign="middle">
ID
</th>
<th scope="col" style='min-width:350px !important;font-weight:bold' align="left" valign="middle" >
From
</th>
<th scope="col" style='min-width:200px !important;font-weight:bold' align="left" valign="middle">
To
</th>
<th scope="col" style='min-width:100px !important;font-weight:bold' align="left" valign="middle">
Total Return Quantity
</th>
<th scope="col" style='min-width:100px !important;font-weight:bold' align="left" valign="middle">
Total Rejection Quantity <br />(In no. of Bottles)
</th>
</tr>
<tr id="plchldr" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<asp:Panel runat="server" ID="pnlDate" Visible=true>
<tr id="Tr1" class='GridviewScrollItem GridviewScrollItem1' runat="server" ><%--visible='<%# Container.DataItemIndex.ToString()== "0" ? true : false %>'--%>
<td style='font-weight:bold;text-align:left;height:20px;font-size:12px;border-right:1px solid #686868 !important;' >
<asp:Label runat="server" ID="lblDate" Text='<%#Convert.ToDateTime(Eval("RejectDate")).ToString("dd-MMM-yyyy",System.Globalization.CultureInfo.InvariantCulture) %>'></asp:Label> </td>
<td style='font-weight:bold;text-align:left;height:20px;font-size:12px;border-right:1px solid #686868 !important;'> </td><td style='font-weight:bold;text-align:left;height:30px;font-size:12px;border-right:1px solid #686868 !important;'> </td><td style='font-weight:bold;text-align:left;height:30px;font-size:12px;border-right:1px solid #686868 !important;'> </td><td style='font-weight:bold;text-align:left;height:30px;font-size:12px;border-right:1px solid #686868 !important;'> </td>
</tr>
</asp:Panel>
<tr class='GridviewScrollItem'>
<td align="left" valign="middle" >
<asp:HiddenField ID="hidRRAutoID" runat="server" Value='<%# Eval("RRAutoID")%>' />
<asp:CheckBox runat="server" ID="chkSelect" />
<asp:Label ID="lnkRejectID" ToolTip="Click here to see details!" style="color:#2AA5BC;cursor:pointer" runat="server" Text='<%# Eval("Options").ToString()=="return" ? Eval("ReturnID") : Eval("RejectID") %>' ></asp:Label>
</td>
<td align="left" valign="middle" >
<asp:Label ID="lblFrom" runat="server" Text='<%# Eval("ReturnFrom") %>' ></asp:Label>
</td>
<td align="left" valign="middle" class="browsehhh555">
<asp:Label ID="lblTo" CssClass="link" runat="server" Text='<%# Eval("ReturnTo") %>' />
</td>
<td align="left" valign="middle" >
<asp:Label ID="lblTotalRejectQuantity" CssClass="link" runat="server" Text='<%# Eval("TotalReturnQuantity").ToString().Replace(".00","") %>' />
</td>
<td align="left" valign="middle" >
<asp:Label ID="Label1" CssClass="link" runat="server" Text='<%# Eval("TotalRejectQuantity").ToString().Replace(".00","") %>' />
</td>
</tr>
<asp:Panel runat="server" ID="pnlDayTotal" Visible=true>
<tr>
<td style="border-bottom:1px solid #AAAAAA"> </td><td style="border-bottom:1px solid #AAAAAA"> </td><td class='tdShift' style='text-align:right !important;height:20px !important;cursor:pointer;font-weight:bold;;white-space:nowrap;'>Day Total </td>
<td class='clsTotal' style='font-weight:normal;height:20px;text-align:right;width:100px !important;white-space:nowrap;'><asp:Literal runat="server" ID="ltrDayTotalReturn" Text="#"></asp:Literal></td>
<td class='clsTotal' style='font-weight:normal;height:20px;text-align:right;width:100px !important;white-space:nowrap;'><asp:Literal runat="server" ID="ltrDayTotalRejection" Text="#"></asp:Literal></td>
</tr>
</asp:Panel>
</ItemTemplate>
</asp:ListView>
Cs Part is ...
public string temDate = ""; public string temDayTotal = ""; public string temDTDate = "";
protected void lvRR_DataBound(object sender, ListViewItemEventArgs e)
{
Label lblFrom = (Label)e.Item.FindControl("lblFrom");
Label lblTo = (Label)e.Item.FindControl("lblTo");
Panel pnlDate = (Panel)e.Item.FindControl("pnlDate");
Panel pnlDayTotal = (Panel)e.Item.FindControl("pnlDayTotal");
Label lblDate = (Label)e.Item.FindControl("lblDate");
if (pnlDate != null)
{
temDTDate = lblDate.Text.ToString();
if (lblDate.Text.ToString() == temDate)
{
pnlDate.Visible = false; //pnlDayTotal.Visible = false;
}
else
{
pnlDate.Visible = true; //pnlDayTotal.Visible = true;
}
temDate = lblDate.Text.ToString();
if (temDTDate != temDate)
{
pnlDayTotal.Visible = true;
}
}
}
Please help me...
Add Following code after bind listview ..
decimal sTotalReturn =0; decimal sTotalReject =0;
for (Int32 ii = 0; ii < lvRR.Items.Count;ii++ )
{
Label lblTotalReturnQuantity = ((Label)lvRR.Items[ii].FindControl("lblTotalReturnQuantity"));
Label lblTotalRejectQuantity = ((Label)lvRR.Items[ii].FindControl("lblTotalRejectQuantity"));
Label lblDate = ((Label)lvRR.Items[ii] .FindControl("lblDate"));
Panel pnlDayTotal = ((Panel)lvRR.Items[ii].FindControl("pnlDayTotal"));
Literal ltrDayTotalReturn = ((Literal)lvRR.Items[ii].FindControl("ltrDayTotalReturn"));
Literal ltrDayTotalRejection = ((Literal)lvRR.Items[ii].FindControl("ltrDayTotalRejection"));
sTotalReturn = sTotalReturn + Convert.ToDecimal(lblTotalReturnQuantity.Text);
sTotalReject = sTotalReject + Convert.ToDecimal(lblTotalRejectQuantity.Text);
if (ii + 1 < lvRR.Items.Count)
{
Label lblDateNext = ((Label)lvRR.Items[ii + 1].FindControl("lblDate"));
if (lblDate.Text.ToString().Trim() != lblDateNext.Text.ToString().Trim())
{
pnlDayTotal.Visible = true;
ltrDayTotalReturn.Text = sTotalReturn.ToString();
ltrDayTotalRejection.Text = sTotalReject.ToString(); sTotalReturn = 0; sTotalReject = 0;
}
}
else
{
pnlDayTotal.Visible = true;
ltrDayTotalReturn.Text = sTotalReturn.ToString();
ltrDayTotalRejection.Text = sTotalReject.ToString();
sTotalReturn = 0; sTotalReject = 0;
}
}

Why I can't click on the ImageButton in ListView?

I want to use the listview control in my asp.net application and I use in the itemtemplate imagebuttons. I want if I click on this Button than I can make a div block visible = true and fill the controls with the data from the listview row.
my listview:
<asp:ListView runat="server" ID="myListView" OnItemCommand="myListView_ItemCommand"
OnSelectedIndexChanging="myListView_SelectedIndexChanging">
<LayoutTemplate>
<table id="UserList" border="1" cellpadding="0" cellspacing="0">
<tr style="background-color:#ccdaeb">
<th>
<span class="spanpading"><asp:Label ID="lblNameHeader" runat="server" Text="_Name"></asp:Label></span> </th>
<th>
<span class="spanpading"><asp:Label ID="lblCompanyHeader" runat="server" Text="_Firma"></asp:Label></span></th>
<th >
<span class="spanpading"><asp:Label ID="lblVonHeader" runat="server" Text="_gültig ab"></asp:Label></span></th>
<th >
<span class="spanpading"><asp:Label ID="lblBisHeader" runat="server" Text="_gültig bis"></asp:Label></span></th>
<th >
<span class="spanpading"><asp:Label ID="lblErstellerHeader" runat="server" Text="_erstellt von"></asp:Label></span> </th>
<th align="left">
</th>
<th align="left">
</th>
<th align="left">
</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr onmouseover="this.style.backgroundColor='#87CEFA'"
onmouseout="this.style.backgroundColor='#ffffff'">
<td align="left"><span class="spanpading"><asp:Label ID="lblname" Text='<%# Eval("NAME") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblcompany" Text='<%# Eval("COMPANY") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblVon" Text='<%# Eval("TIMEFROM") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblBis" Text='<%# Eval("TIMETO") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblErsteller" Text='<%# Eval("CREATOR") %>' runat="server" /></span></td>
<td align="left"><asp:ImageButton ID="imgDelete" runat="server" ToolTip="löschen" ImageUrl="images/delete.gif" /></td>
<td align="left"><asp:ImageButton ID="imgUpdate" runat="server" ToolTip="ändern" ImageUrl="images/edit.gif" CommandName="update" CommandArgument='<%# Container.DataItemIndex %>' /></td>
<td align="left"><asp:ImageButton ID="imgEmail" runat="server" ToolTip="Zugangsdaten senden" ImageUrl="images/send.gif" /></td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<h4>Es wurden keine Einträge gefunden</h4><br/>
</EmptyDataTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #E5EDF5;" onmouseover="this.style.backgroundColor='#87CEFA'"
onmouseout="this.style.backgroundColor='#E5EDF5'">
<td align="left"><span class="spanpading"><asp:Label ID="lblname" Text='<%# Eval("NAME") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblcompany" Text='<%# Eval("COMPANY") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblVon" Text='<%# Eval("TIMEFROM") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblBis" Text='<%# Eval("TIMETO") %>' runat="server" /></span></td>
<td align="left"><span class="spanpading"><asp:Label ID="lblErsteller" Text='<%# Eval("CREATOR") %>' runat="server" /></span></td>
<td align="left"><asp:ImageButton ID="imgDelete" runat="server" ToolTip="löschen" ImageUrl="images/delete.gif" /></td>
<td align="left"><asp:ImageButton ID="imgUpdate" runat="server" ToolTip="ändern" ImageUrl="images/edit.gif" CommandName="update" CommandArgument='<%# Container.DataItemIndex %>' /></td>
<td align="left"><asp:ImageButton ID="imgEmail" runat="server" ToolTip="Zugangsdaten senden" ImageUrl="images/send.gif" /></td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
Here is m< code for the commandItem:
protected void myListView_SelectedIndexChanging(object sender, EventArgs e)
{
//
}
protected void myListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "update")
{
int index = Convert.ToInt32(e.CommandArgument);
Label lbName = (Label)myListView.Items[index].FindControl("lblname");
Label lbFirma = (Label)myListView.Items[index].FindControl("lblcompany");
Label lbVon = (Label)myListView.Items[index].FindControl("lblVon");
Label lbBis = (Label)myListView.Items[index].FindControl("lblBis");
//Übergabe an die Div Update Box
draggablebox.Visible = true;
}
}
and here is the errormessage:

how find and give css class to a control in side layout template of listview?

I have a listview control and in layout template, i have linkbuttons. Now what i want to do. i have a span with each linkbutton. i want to give css class while we click on linkbutton. my html code is as follow:
<asp:ListView ID="lst_CallType" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_CallType_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left" width="500px">
<asp:LinkButton ID="lnk_Name" runat="server" ValidationGroup="vgSearch" OnClientClick="changeSortState();"
CommandArgument="tblCallTypenew.CallType" OnClick="lnk_Sort">Name</asp:LinkButton>
<span id="imgSortPosition" class="sortNotSelected"></span>
</td>
<td align="left" width="80px">
<asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblCallTypenew.isactive"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
<span id="Span1" class="sortNotSelected"></span>
</td>
<td align="left" width="200px">
<asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblCallTypenew.CreatedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
<span id="Span2" class="sortNotSelected"></span>
</td>
<td align="left" width="200px">
<asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblCallTypenew.ModifiedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
<span id="Span3" class="sortNotSelected"></span>
</td>
<td align="left" width="200px">
<asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="tblUserNew.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created By</asp:LinkButton>
<span id="Span4" class="sortNotSelected"></span>
</td>
<td align="left" width="200px">
<%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
<asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Modified By</asp:LinkButton>
<span id="Span5" class="sortNotSelected"></span>
</td>
<td align="left" style="border-right: 1px solid #6398cc">
Activity
<div style="width: 50px; float: right;">
</div>
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
<td align="left">
<asp:Label ID="lblDeptId" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
<%# Eval("Calltype")%>
</td>
<td align="left">
<asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
<asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("id") %>'
OnClick="imgbtnStatus_Onclick" />
</td>
<td align="left">
<%# Eval("CreatedDate")%>
</td>
<td align="left">
<%# Eval("ModifiedDate") %>
</td>
<td align="left">
<%# Eval("CreatedBy")%>
</td>
<td align="left">
<%# Eval("ModifiedBy")%>
</td>
<td>
<asp:Label ID="lblCallType" runat="server" Style="display: none;" Text='<%# Eval("Calltype")%>'></asp:Label>
<asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
ToolTip="Edit Details" CommandArgument='<%# Eval("ID") %>' OnClick="imgbtnEdit_OnClick" />
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
ToolTip="Delete" Style="display: none;" CommandArgument='<%# Eval("id") %>' OnClientClick="return confirm('Are you sure you want to delete the Call type?');"
OnClick="imgbtnDelete_OnClick" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Actually i want to show sort images while user click on linkbuttons.And i want to do it by code behind.
My .cs code is as follow:
protected void lnk_Sort(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string arg = lnk.CommandArgument.ToString();
ViewState["sortCol"] = arg;
GetSortDirection();
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
}
private void GetSortDirection()
{
if (Convert.ToString(ViewState["sortDir"]) == "Desc")
{
ViewState["sortDir"] = "asc";
}
else
{
ViewState["sortDir"] = "Desc";
}
}
You can do a findcontrol in Listview_sorting event
i have done it with a silly trick
my html code is:
<asp:ListView ID="lst_CallType" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_CallType_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td width="35px" align="left">
S.No
</td>
<td align="left" width="300px">
<asp:LinkButton ID="lnk_Name" runat="server" ValidationGroup="vgSearch" CommandArgument="tblCallTypenew.CallType"
OnClick="lnk_Sort">Name</asp:LinkButton>
<asp:Image ID="img_lnk_Name" Visible="false" runat="server" />
</td>
<td align="left" width="150px">
<asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="tblUserNew.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created By</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblCallTypenew.CreatedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedOn" Visible="false" runat="server" />
</td>
<td align="left" width="150px">
<%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
<asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified By</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblCallTypenew.ModifiedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedOn" Visible="false" runat="server" />
</td>
<td align="center" width="55px">
<asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblCallTypenew.isactive"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
<asp:Image ID="img_lnk_Status" Visible="false" runat="server" />
</td>
<td align="center" width="50px" style="border-right: 1px solid #6398cc">
Activity
<%-- <div style="width: 50px; float: right;">
</div>--%>
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
<td align="left" valign="middle">
<%# Container.DataItemIndex+1 %>.
</td>
<td align="left">
<asp:Label ID="lblDeptId" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
<%# Eval("Calltype")%>
</td>
<td align="left">
<%# Eval("CreatedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM dd, yyyy")%>
</td>
<td align="left">
<%# Eval("ModifiedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("ModifiedDate")).ToString("MMM dd, yyyy")%>
</td>
<td align="center">
<asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
<asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("id") %>'
OnClick="imgbtnStatus_Onclick" />
</td>
<td class="last" align="center">
<asp:Label ID="lblCallType" runat="server" Style="display: none;" Text='<%# Eval("Calltype")%>'></asp:Label>
<asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
ToolTip="Edit Details" CommandArgument='<%# Eval("ID") %>' OnClick="imgbtnEdit_OnClick" />
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
ToolTip="Delete" Style="display: none;" CommandArgument='<%# Eval("id") %>' OnClientClick="return confirm('Are you sure you want to delete the Call type?');"
OnClick="imgbtnDelete_OnClick" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
My code behind code is:
protected void lnk_Sort(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string arg = lnk.CommandArgument.ToString();
ViewState["sortCol"] = arg;
GetSortDirection();
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
string name = lnk.ID;
Image img = (Image)(lst_CallType.FindControl("img_" + name));
if (img != null)
{
SetSortOrderImage(img, ViewState["sortDir"].ToString());
}
}
private void SetSortOrderImage(Image image, String sortorder)
{
if (sortorder == "asc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
}
else if (sortorder == "Desc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";
}
}

Categories

Resources