asp.net- Highlight cell in listview - asp.net

I have this piece of code which works fine and highlights the label. However, I want it to highlight the entire cell not just the label.
Any help would be appreciated!
protected void HighLight_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label TotalTimeLabel = (Label)e.Item.FindControl("TotalTime");
if (TotalTimeLabel != null)
{
Decimal Total;
if (Decimal.TryParse(TotalTimeLabel.Text, out Total) == true)
{
if (Total > (Decimal)1.5)
{
TotalTimeLabel.BackColor = System.Drawing.Color.Red;
TotalTimeLabel.ForeColor = System.Drawing.Color.Black;
}
}
}
}
}
The code for the table is below
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="HighLight_ItemDataBound" >
<LayoutTemplate>
<table cellpadding="1" class="TableCSS" runat="server" id="tblProducts">
<tr runat="server" style="background-color:lightgrey">
<th runat="server">enNotificationNoNI</th>
<th runat="server">TotalTime</th>
<th runat="server">TPTIMEIN</th>
<th runat="server">Status</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" class="TableData">
<td>
<asp:Label ID="enNotificationNoNI" runat="Server" Text='<%#Eval("enNotificationNoNI") %>' />
</td>
<td>
<asp:Label ID="TotalTime" runat="Server" Text='<%#Eval("TotalTime") %>' />
</td>
<td>
<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN") %>' />
</td>
<td>
<asp:Label ID="Status" runat="Server" Text='<%#Eval("Status") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>

Try this code
For entire row
Control ctrl = TotalTimeLabel.Parent.Parent;
HtmlTableRow tblrw = (HtmlTableRow)ctrl;
tblrw.BgColor = System.Drawing.Color.Red.Name;
For one cell
Control ctrl = TotalTimeLabel.Parent;
HtmlTableCell tblcl = (HtmlTableCell)ctrl;
tblcl.BgColor = System.Drawing.Color.Red.Name;
let me know whether this works :)

Related

Taking a textbox's text in a listview

I have a ListView, and i have an asp:TextBox in it:
<table>
<asp:ListView ID="Users" runat="server"
DataKeyNames="Email" onitemediting="Users_ItemEditing"
onitemupdating="Users_ItemUpdating">
<LayoutTemplate>
<tr>
<td style=" width:34%;border-bottom:1px solid black;">Name</td>
<td></td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</table>
when i change the text in the text box and then try to get it in the code behind like this:
protected void Users_ItemEditing(object sender, ListViewEditEventArgs e)
{
this.Users.EditIndex = e.NewEditIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string Name=((TextBox)Users.Items[e.ItemIndex].FindControl("Name")).Text;
this.Users.EditIndex = -1;
BindTheListView();
}
*BindTheListView() Binds the listview Users, it works.
it takes the original text of the TextBox, the <%# Bind("name") %> instead of what i have just changed.
The update function is a lot longer so i put only what i think is needed. i know the Name recives the text that wast before in the textbox because the updtade function didnt work(it didnt change anything in the database) so i run debugg and i saw that Name recives what was before
How can i take the text that i have entered the textbox and not what was in it?
Thanks for the help
In User.ItemUpdating get new values from e.NewValues collection instead.
EDIT:
Use DataList instead of ListView:
<table>
<asp:DataList ID="Users" runat="server" DataKeyField="Email" OnEditCommand="Users_ItemEditing"
OnUpdateCommand="Users_ItemUpdating">
<HeaderTemplate>
<tr>
<td style="width: 34%; border-bottom: 1px solid black;">
Name
</td>
<td>
</td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:DataList>
</table>
Code behind:
protected void Users_ItemEditing(object sender, DataListCommandEventArgs e)
{
this.Users.EditItemIndex = e.Item.ItemIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, DataListCommandEventArgs e)
{
string Name = ((TextBox)e.Item.FindControl("Name")).Text;
}
on Page Load use
if (!IsPostBack)
{
BindTheListView();
}

How to get values from templatefields in Gridview

I have a gridview with template fields.
I want when I press the imagebutton to get the values of the Labels inside the ItemTemplate.
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table style="border: 0px solid">
<tr>
<td style="width: 220px">
<asp:Image ID="imgEmployee" BorderStyle="Solid" Height="182px" Width="235px"
ImageUrl='<%# Eval("Photo")%>'
runat="server" />
</td>
<td style="width: 600px">
<table>
<tr>
<td>
<b>ID:</b>
</td>
<td>
<asp:Label ID="lblId"
runat="server"
Text='<%#Eval("id")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Item:</b>
</td>
<td>
<asp:Label ID="lblItem"
runat="server"
Text='<%#Eval("Item")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Price:</b>
</td>
<td>
<asp:Label ID="lblPrice"
runat="server"
Text='<%#Eval("Price")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Notes:</b>
</td>
<td>
<asp:Label ID="lblNotes"
runat="server"
Text='<%#Eval("Notes")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text ="1" ></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/buy.png" CommandName="AddToBasket" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridView1_RowCommand I have the following code
If e.CommandName = "AddToBasket" Then
Here i want to take the values of the labels
You need to attach OnRowCommand event method to the GridView first:
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
OnRowCommand ="GridView1_RowCommand"
runat="server">
You can get the GridViewRow from commandsource's NamingContainer and use FindControl method to find the Labels like below:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToBasket")
{
int id = 0;
string item = string.Empty; ;
decimal price = 0.00m;
string notes = string.Empty;
var gvRow = ((ImageButton)e.CommandSource).NamingContainer as GridViewRow;
var lblId = gvRow.FindControl("lblId") as Label;
var lblItem = gvRow.FindControl("lblId") as Label;
var lblPrice = gvRow.FindControl("lblId") as Label;
var lblNotes = gvRow.FindControl("lblId") as Label;
if (lblId != null && lblItem != null && lblPrice != null && lblNotes != null)
{
int.TryParse(lblId.Text, out id);
item = lblItem.Text;
decimal.TryParse(lblPrice.Text, out price);
notes = lblNotes.Text;
}
}
}

how to bind string[] array to only one column of listview

select | Application No.
There are two columns in my listview(generated by following code) where 1st column is select link while other is simple column with header application number (header shown above):
<asp:ListView ID="ListBox1" runat ="server" AutoPostBack="True"
onselectedindexchanged="ListBox1_SelectedIndexChanged">
<LayoutTemplate>
<table style="border: solid 2px #336699;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: #336699; color: White;">
<th>Select</th>
<th>Application No.</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
</td>
<td><%# Container.DataItem %></td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: #336699; color: White;">
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
ForeColor="White" />
</td>
<td><%# Container.DataItem %></td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
now i want to display application number from string[] array. How i can do that plz help..?
//are you using datatable as datasource?, if so
var arr = new[] { "one", "two" };
var dt = new DataTable();
dt.Columns.Add("ApplicationNo", typeof(string));
var i = 0;
foreach (DataRow dr in dt.Rows)
{
dr["ApplicationNo"] = arr[i];
dt.AcceptChanges();
i++;
}
ListBox1.DataSource=dt;
ListBox1.DataBind();
Try this,
<asp:ListView ID="mylist" runat="server"
onselectedindexchanged="mylist_SelectedIndexChanged">
<LayoutTemplate>
<table style="border: solid 2px #336699;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: #336699; color: White;">
<th>
Select
</th>
<th>
Application No.
</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton OnClick="lnkSelect_OnClick" ID="lnkSelect" Text="Select" CommandArgument='<%# Container.DataItem %>' CommandName="Select" runat="server" />
</td>
<td>
<%# Container.DataItem %>
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: #336699; color: White;">
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
ForeColor="White" />
</td>
<td>
<%# Container.DataItem %>
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
IList<string> myList = new List<string>();
myList.Add("1");
myList.Add("3");
myList.Add("2");
myList.Add("4");
mylist.DataSource = myList;
mylist.DataBind();
OR
string[] myList = new string[] { "1","2","3" };
mylist.DataSource = myList;
mylist.DataBind();
protected void lnkSelect_OnClick(object sender, EventArgs e)
{
string val = ((System.Web.UI.WebControls.LinkButton)(sender)).CommandArgument;
}

Add a new row in a gridview

next code shows a table filled (a gridview) with 5 columns and several rows. It's running well
while (drCLientes.Read())
{
GridView gv = new GridView();
gv.ID = "myGridID";
cong.Open();
da = new OleDbDataAdapter(sql, cong);
ds = new DataSet("Clientes");
da.Fill(ds, "Clientes");
cong.Close();
gv.DataSource = ds.Tables["Clientes"];
gv.DataBind();
}
After this code, the result is this:
Table that I have
Now I need to add a new row, in the first position. The first column empty. The second and third one columns must be combined. The same with the 4th and 5th ones. Here is an image with the table that I need:
Table that I need
Regards
You need to use Repeater instead of GridView.
<asp:Repeater ID="repList" runat="server" OnItemCommand="repList_ItemCommand">
<HeaderTemplate>
<table cellpadding="3" cellspacing="0" border="1" style="border-collapse: collapse;"
width="100%">
<thead style="text-align: center;" class="Gridheader">
<tr>
<td rowspan="2">Sequence</td>
<td colspan="4">Student</td>
<td colspan="3">Lesson</td>
<td rowspan="2">Point</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>Age</td>
<td>Class</td>
<td>Name</td>
<td>Teacher</td>
<td>Time</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:Label ID="lblSequence" runat="server" >
</asp:Label>
</td>
<td>
<asp:Label ID="lblStudentName" runat="server" >
</td>
<td>
<asp:Label ID="lblStudentSurname" runat="server" >
</td>
<td>
<asp:Label ID="lblStudentAge" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonClass" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonName" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonTeacher" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonTime" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonPoint" runat="server" >
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
and Code Behind;
public void rptr_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Data.Common.DbDataRecord objRow = (System.Data.Common.DbDataRecord)e.Item.DataItem;
Label lblAnswer = e.Item.FindControl("lblSequence") as Label;
lblAnswer.Text = objRow["Sequence"].ToString();
Label lblStudentName = e.Item.FindControl("lblStudentName") as Label;
lblStudentName.Text = objRow["StudentName"].ToString();
...
..
.
}
}

How to group one row values in Listview

I have a dynamic Listview which is bind to three different tables with one to many relations i.e. one table row may contain many rows in other table. When i run my application i get this output.
But i want to get Listview in this format although this image has been edited using Photshop.
Here is Listview HTML.
<asp:ListView runat="server" ID="LV_ViewQuestion" DataKeyNames="UID, Question_ID">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No Surveys.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="SURVEY_TYPELabel" runat="server" Text='<%# Eval("Survey_Type")%>' />
</td>
<td>
<asp:Label ID="SURVEY_TITLELabel" runat="server" Text='<%# Eval("Survey_Title") %>' />
</td>
<td>
<asp:Label ID="Question_TextLabel" runat="server" Text='<%# Eval("Question_Text")%>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Option_Text")%>' />
</td>
<td>
<asp:LinkButton runat="server" ID="lb_DelQuestion" Text="Delete" CommandArgument='<%# Eval("Question_ID")%>' CommandName="XDelQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_AddMoreQuest" Text="Add Question" CommandArgument='<%# Eval("UID")%>' CommandName="XAddAnotQuestion" CssClass="GeneralInput" />
<asp:LinkButton runat="server" ID="lb_Publish" Text="Publish" CommandArgument='<%# Eval("UID")%>' CommandName="XPublishSurvey" CssClass="GeneralInput" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" class="nobordered" style="width: 580px;">
<tr id="Tr2" runat="server" style="">
<th id="Th1" runat="server">Type</th>
<th id="Th2" runat="server">Title</th>
<th id="Th6" runat="server">Question</th>
<th id="Th4" runat="server">Options</th>
<th id="Th3" runat="server" style="width: 200px;">Actions</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" ButtonCssClass="GeneralButton" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
You can accomplish this hiding the cells in the ItemDataBound event of the ListView. Your code should look like this:
first add three global properties in your page
string type = string.Empty;
string title = string.Empty;
string question = string.Empty;
Then add the OnItemDataBound event to your list view
protected void LV_ViewQuestion_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label SURVEY_TYPELabel = (Label)e.Item.FindControl("SURVEY_TYPELabel");
Label SURVEY_TITLELabel = (Label)e.Item.FindControl("SURVEY_TITLELabel");
Label Question_TextLabel = (Label)e.Item.FindControl("Question_TextLabel");
if (SURVEY_TYPELabel.Text == type && SURVEY_TITLELabel == title &&
Question_TextLabel == question)
{
SURVEY_TYPELabel.Visible = false;
SURVEY_TITLELabel.Visible = false;
Question_TextLabel.Visible = false;
// Do the same for all the other control in cells you need to hide
}
else
{
type = SURVEY_TYPELabel.Text;
title = SURVEY_TITLELabel.Text;
question = Question_TextLabel.Text;
}
}
}

Resources