I am trying to loop over a ListView with a foreach statement, but I can't seem to get the Subitems of item. No success with a For statement either.
IntelliSense doesn't propose it on both ways.
Code Behind:
protected void btnNext_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in ListView1.Items)
{
item. *(here a should get the Subitems)*
}
}
ASPX
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
<LayoutTemplate>
<table>
<tr>
<th>Customer</th>
<th>Item No</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("CustomerName") %>
</td>
<td>
<%# Eval("Item") %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
you have to change your aspx page as below
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
<LayoutTemplate>
<table>
<tr>
<th>Customer</th>
<th>Item No</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerName" Text='<%# Eval("CustomerName") %>' runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblItem" Text='<%# Eval("Item") %>' runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Now you have to use for each loop as below in code behind file
string strProductNames = string.Empty;
foreach (ListViewItem item in ListView1.Items)
{
Label lblCustomerName= (Label)item.FindControl("lblCustomerName");
// strProductNames = strProductNames + lblCustomerName.Text + "<br/>";
// you can get values in lblCustomerName.Text. use this value as per your requirement
}
Hope this will helps you..happy coding
you should use loop throug listview.items
for (int j = 0; j < this.listView1.Items.Count; j++)
{
ListViewItem item =
(ListViewItem)this.listView1.ItemContainerGenerator.ContainerFromIndex(j);
}
Get data being bound to ListView on DataBound event
you would treat the code inside of your loop the same way that it would be handled inside of the listView1_ItemDataBound event.
Related
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 :)
On my form,I am having one drop down list.I have filled drop down list on page load event.Even i have check if not is post back.My drop down list's auto post back property is true.But still the event is not firing.I have to show some value in text box on selected index changed of drop down list.
my design code is as below:
enter code here
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<table class="tblContentcss">
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserRole" Text="Role"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtUserRole" Text="User" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserID" Text="User ID"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:DropDownList ID="ddlUserID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlUserID_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserName" Text="User Name"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtUserName" AutoPostBack="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserPassword" Text="Password"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="Label1" Text="Password"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</asp:Content>
and in.cs on page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillUserID();
}
}
protected void ddlUserID_SelectedIndexChanged(object sender, EventArgs e)
{
fillUserID();
SqlConnection con = dal.GetConnection();
dsUserName = new DataSet();
query = "SELECT CUSTID,(FNAME+' '+MNAME+' '+LNAME) AS USERNAME FROM CUSTOMER where CUSTID=" + Convert.ToInt32(ddlUserID.SelectedValue) + "";
dsUserName = dal.ReturnDataset(query, con);
if (dsUserName.Tables.Count > 0 && dsUserName.Tables[0].Rows.Count > 0)
{
txtUserName.Text = dsUserName.Tables[0].Rows[0]["USERNAME"].ToString();
}
}
what is the problem,i am not getting.
Suggest me any solution
When the autopostback is set True, after the page is rendered, a javascript is added on the onchange event of the dropdown (SELECT in HTML) to trigger the postback,
View the source after the page is rendered and check whether the javascript is getting added to the onchange event of the dropdown.
If you are adding some javascript on the onchange event of the dropdown in the code-behind, that might also stop the page to postback.
I get that from this link: https://www.codeproject.com/Questions/138183/Drop-down-selected-index-change-event-not-working
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();
}
I don't understand why a property ParentScheduleItemId in my usercontrol is always zero. All I want to do is pass a value to my user control so that it can be used as a selectparameter of a selectcommand in a SqlDataSource. I have the following code below, which seems to cause ParentScheduleItemId == 0, but it actually prints the expected non-zero value that I passed it in the ascx file right beside the sentence No data was returned:
namespace CCApplication.Cms
{
public partial class schedule_sub_item : System.Web.UI.UserControl
{
public int ParentScheduleItemId {
get;
set; }
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", ParentScheduleItemId.ToString());
//SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", "12");
}
}
}
I have the following code in the ascx file
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NewConferencesApplicationConnectionString %>"
SelectCommand="
SELECT [ScheduleItemId],[ParentScheduleItemId],[Name],[Type],[Location],[MaximumUsers],[CompanionAllowed],[StartTime],[EndTime]
FROM [Conferences_ScheduleItem]
WHERE ParentScheduleItemId = #ParentScheduleItemId
ORDER BY StartTime;
"
DeleteCommand="DELETE FROM [Conferences_ScheduleItem] WHERE [ScheduleItemId] = #ScheduleItemId"
>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ScheduleItemId"
DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>
No data was returned.<% Response.Write(ParentScheduleItemId.ToString()); %></td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="ScheduleItemIdLabel" runat="server"
Text='<%# Eval("ScheduleItemId") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
So all I want to do is do something like <usercontrol:mylistview ParentScheduleItemId=24> and then for 24 to be used as part of my SelectCommand. How do I achieve this?
Note - you'll notice that I commented out the line SqlDataSource1.SelectParameters.Add("ParentScheduleItemId", "12"); . If i uncomment it, it actually behaves as expected, and gives the number 12 to my select parameters and the sqldatasource gives me the values I want. I just don't understand why the line above it has a ParentScheduleItemId == 0. Does this have something to do with the page load cycle? Anyway, i just want to pass the right parentscheduleitemid to my select command.
Ok that was frustrating. I found the solution after 3 hours of banging my head against the wall. Here's the working code:
// codebehind
namespace CCApplication.Cms
{
public partial class schedule_sub_item : System.Web.UI.UserControl
{
public string _parentScheduleItemId;
public string ParentScheduleItemId {
get
{
return _parentScheduleItemId;
}
set
{
_parentScheduleItemId = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UpdateSqlParametersEventHandler(object sender, SqlDataSourceSelectingEventArgs e)
{
System.Data.Common.DbParameter param = e.Command.CreateParameter();
param.ParameterName = "#ParentScheduleItemId";
param.Value = this._parentScheduleItemId;
e.Command.Parameters.Add(param);
}
}
}
//frontend code
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NewConferencesApplicationConnectionString %>"
OnSelecting="UpdateSqlParametersEventHandler"
SelectCommand="
SELECT [ScheduleItemId],[ParentScheduleItemId],[Name],[Type],[Location],[MaximumUsers],[CompanionAllowed],[StartTime],[EndTime]
FROM [Conferences_ScheduleItem]
WHERE ParentScheduleItemId = #ParentScheduleItemId
ORDER BY StartTime;
"
DeleteCommand="DELETE FROM [Conferences_ScheduleItem] WHERE [ScheduleItemId] = #ScheduleItemId"
>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ScheduleItemId"
DataSourceID="SqlDataSource1">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>
No data was returned.<% Response.Write(ParentScheduleItemId.ToString()); %></td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="ScheduleItemIdLabel" runat="server"
Text='<%# Eval("ScheduleItemId") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
So basically I needed to add a OnSelecting="UpdateSqlParametersEventHandler", then add that function in the code behind to set the parameter.
The reason i was having problems before was because the page load cycle was causing the sqldatasource to run before the ParentScheduleItemId was even being set ...
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;
}
}
}