Access dropdownlist value which is kept outside listview? - asp.net

I have a listview and a dropdownlist ( this is kept outside listview) and one search button.
Now i need to access two literal control from listview which are kept in layout template and emptydatatemplate and assign the dropdownlist value to those literal control with few extra text.
what i tried was for layout template:
protected void lvP_LayoutCreated(object sender, EventArgs e)
{
if (dlCategory.SelectedIndex != -1)
{
(lvP.FindControl("ltHeaderText") as Literal).Text = " Products in " + dlCategory.SelectedItem.Text.ToString();
}
}
and for empty data i tried below
protected void lvPr_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.EmptyItem)
{
(lvP.FindControl("ltEmptyData") as Literal).Text = "No products in " + dlCategory.SelectedItem.Text.ToString();
}
}
aspx markup
<asp:ListView ID="lvP" runat="server" OnLayoutCreated="lvP_LayoutCreated"
OnItemCreated="lvP_ItemCreated">
<LayoutTemplate>
<table class="PWorld" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
<span>
<asp:Literal ID="ltHeaderText" runat="server"></asp:Literal>
</span>
</th>
</tr>
</thead>
<tbody>
<tr id="itemplaceHolder" runat="server">
</tr>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr valign="top">
<td>
<table class="PWorldInner" cellpadding="0" cellspacing="0">
<tr>
<td>
<img height="65px" width="175px" src='<%#Eval("ProductLogo")%>' alt="<%#Eval("pName")%>" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<asp:Literal ID="ltEmptyData" runat="server"></asp:Literal>
</EmptyDataTemplate>
</asp:ListView>

Related

How to get value from a listview Asp.Net

I have a listview in asp.net like this:
<asp:ListView ID="lvLoads" DataKeyNames="BookingId" OnItemCommand="lvLoads_OnItemCommand" runat="server">
<EmptyDataTemplate>
<table>
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<GroupTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</GroupTemplate>
<ItemTemplate>
<fieldset>
<table class="table table-striped table-responsive">
<thead>
<tr class="hidden-lg">
<th width="15%">Type</th>
<th width="31%">Details</th>
<th width="25%">Distance/Duration</th>
<th width="21%"> </th>
</tr>
</thead>
<tbody>
<tr>
<td width="15%">
<asp:Label CssClass="hide" ID="lblBokingId" runat="server"><%# Eval("BookingId") %></asp:Label>
<asp:Label ID="lblLoadTypeName" runat="server"> <%# Eval("LoadTypeName") %></asp:Label>
<br>
</td>
<td width="21%">
<asp:LinkButton ID="btnViewMore" CssClass="btn btn-warning" CommandName="ViewMore" runat="server">View More</asp:LinkButton>
</td>
</tr>
</tbody>
</table>
</fieldset>
</ItemTemplate>
</asp:ListView>
Now on click of the button ViewMore i wrote this code to extract listviewItem's value:
var id = ((Label)e.Item.FindControl("lblBookingId")).Text;
var name = ((Label)e.Item.FindControl("lblLoadTypeName")).Text;
But e.Item.FindControl("lblBookingId") always comes null. I checked the page's html by using inspect element of chrome, and i was shocked to see that my id somehow got changed to ContentSection_ctl00_lblLoadTypeName_0
I am really stuck at this. Please help me. Thanx in Advance
I had done some work with your list view and one which i had created to resemble the problem.
Default2.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
WORKING EXAMPLE
<asp:ListView ID="lvLoads" runat="server">
<LayoutTemplate>
<table border="0" cellpadding="1">
<tr style="background-color: #E5E5FE">
<th align="left">EmployeeId
</th>
<th align="left">LastName
</th>
<th align="left"></th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("BookingId") %>'></asp:Label>
</td>
<td id="Td2" runat="server">
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LoadTypeName") %>'></asp:Label>
</td>
<td id="Td3" runat="server">
<asp:LinkButton ID="lnkSelect" runat="server" OnClick="btnViewMore_Click" Text="Select" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
<div>
ERROR
<asp:ListView ID="ListView1" DataKeyNames="BookingId" OnItemCommand="lvLoads_OnItemCommand" runat="server">
<EmptyDataTemplate>
<table>
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<GroupTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</GroupTemplate>
<ItemTemplate>
<fieldset>
<table class="table table-striped table-responsive">
<thead>
<tr class="hidden-lg">
<th width="15%">Type</th>
<th width="31%">Details</th>
<th width="25%">Distance/Duration</th>
<th width="21%"> </th>
</tr>
</thead>
<tbody>
<tr>
<td width="15%">
<asp:Label CssClass="hide" ID="lblBokingId" runat="server"><%# Eval("BookingId") %></asp:Label>
<asp:Label ID="lblLoadTypeName" runat="server"> <%# Eval("LoadTypeName") %></asp:Label>
<br>
</td>
<td width="21%">
<asp:LinkButton ID="btnViewMore" CssClass="btn btn-warning" OnClick="btnViewMore_Click1" runat="server">View More</asp:LinkButton>
</td>
</tr>
</tbody>
</table>
</fieldset>
</ItemTemplate>
</asp:ListView>
</div>
</div>
</form>
Default2.aspx.cs:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 obj = new Class1();
lvLoads.DataSource = obj.bind();
lvLoads.DataBind();
ListView1.DataSource = obj.bind();
ListView1.DataBind();
}
protected void lvLoads_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
}
protected void btnViewMore_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
Label lblStatus = (Label)item.FindControl("lblLastName");
string message = lblStatus.Text;
}
protected void btnViewMore_Click1(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
Label lblStatus = (Label)item.FindControl("lblLoadTypeName");
string message = lblStatus.Text;
}
}
Class1:
public class Class1
{
public int BookingId { get; set; }
public string LoadTypeName { get; set; }
public Class1()
{
}
public List<Class1> bind()
{
List<Class1> data = new List<Class1>();
Class1 obj = new Class1();
obj.BookingId = 1;
obj.LoadTypeName = "Type name1";
data.Add(obj);
obj = new Class1();
obj.BookingId = 2;
obj.LoadTypeName = "Type name2";
data.Add(obj);
obj = new Class1();
obj.BookingId = 3;
obj.LoadTypeName = "Type name3";
data.Add(obj);
obj = new Class1();
obj.BookingId = 4;
obj.LoadTypeName = "Type name4";
data.Add(obj);
return data;
}
}
As you can see in both list view there is only problem related with design which you are using in your list view. Please change design as per your requirement.
You are placing control in further other controls that's why id will change at run time.
hope it will help you.

how to pass a variable to a user control to be used as selectcommand of sqldatasource

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 ...

how to create dynamic textbox,dropdownlist in asp.net

I want to create dynamic 2 textbox and 2 dropdownlist below each textbox and dropdownlist in button click. This is the code i am using which creates dynamic textbox and dropdownlist but its creating control in horizontal way.I want creation of dynamic controls in a vertical manner which will be coming under textbox and dropdown.
<div id="dvContainer" runat="server">
<table align="center" cellpadding="0" cellspacing="0" width="90%">
<tr>
<td>
<table width="100%">
<tr>
<td align="center">Date</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="150px"> </asp:TextBox>
</td>
<td align="center">Time</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">Godown</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><table width="100%">
<tr>
<td align="center">Item</td>
<td align="center">QTY</td>
<td align="center">Unit</td>
<td align="center">Available</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">
<asp:DropDownList ID="DropDownList2" runat="server" Width="150px">
</asp:DropDownList>
</td>
<td align="center">
<asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">
<asp:DropDownList ID="DropDownList3" runat="server" Width="150px">
</asp:DropDownList>
</td>
<td align="center">
<asp:TextBox ID="TextBox4" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
</table></td>
</tr>
</table>
</div>
This is the code i am using in cs page
public partial class StockEntry : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_Item;
protected System.Web.UI.WebControls.TextBox Txt_Quantity;
protected System.Web.UI.WebControls.DropDownList Ddl_Unit;
protected System.Web.UI.WebControls.TextBox Txt_Available;
protected void Page_Load(object sender, EventArgs e)
{
}
int countTimes = 0;
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["countTimes"] == null)
{
countTimes = 1;
}
else
{
countTimes = Convert.ToInt32(ViewState["countTimes"]);
}
for (int i = 0; i < countTimes; i++)
{
Ddl_Item = new DropDownList();
Ddl_Item.ID = "Ddl_Item" + i;
Ddl_Item.Width = 180 + i;
Txt_Quantity = new TextBox();
Txt_Quantity.ID = "Txt_Quantity" + i;
Txt_Quantity.Width = 180 + i;
Ddl_Unit = new DropDownList();
Ddl_Unit.ID = "Ddl_Unit" + i;
Ddl_Unit.Width = 180 + i;
Txt_Available = new TextBox();
Txt_Available.ID = "Txt_Available" + i;
Txt_Available.Width = 180 + i;
dvContainer.Controls.Add(Ddl_Item);
dvContainer.Controls.Add(Txt_Quantity);
dvContainer.Controls.Add(Ddl_Unit);
dvContainer.Controls.Add(Txt_Available);
}
countTimes = countTimes + 1;
ViewState.Add("countTimes", countTimes);
}
}
You are adding your controls in the div, not in the table and next to each other. To add some html structure around controls, you can create literal object with your html in its text, and add them before/after your controls (for example you can add <br/> )
I think what you want is HtmlGenericControl http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
This will allow you to add the table that you require to set your layout, you could of course wrap each control in a div and control your layout using CSS.

asp.net repeater control scrolling with ajax

I want to make repeater data scrollable with ajax scrolling just like facebook do in news feeds here is my code
please help me I want all these fetched with page scroll.
ASPX page
<form id="form1" runat="server">
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Comments</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("PostedDate") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</ItemTemplate>
</table>
</asp:Repeater>
code behind file
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindRepeaterData();
}
}
//Bind Data to Repeater Control
protected void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Repeater_Table Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
con.Close();
}
please help me I want all these fetched with page scroll.
I think what you are looking for is typically called "infinite scroll". Try doing a Google search for some examples. Here's one that might be a good start:
http://weedallaboutit.wordpress.com/2011/10/13/infinite-scrolling-with-an-asp-net-webform-jquery-and-a-little-bit-of-code/
Working example:
http://www.aspsnippets.com/Articles/Infinite-scroll-images-like-Bing-and-Google-in-ASPNet.aspx
There is also download option in this page.

How to highlight the row in listview according to condition?

I want to change background color of the row in particular condition.My code is
<asp:ListView ID="lst_SentItems" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_SentItems_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left" width="180px">
EmpName
</td>
<td align="left" width="180px">
Salary
</td>
<td align="left" width="180px">
Address
</td>
<td align="left" width="180px">
Department
</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="lbl3" runat="server" text='<%# Eval("EmpName")%>' />
</td>
<td align="left">
' />
</td>
<td align="left">
<asp:label id="lbl1" runat="server" text='<%# Eval("Address")%>' />
</td>
<td align="left">
<asp:label id="lbl" runat="server" text='<%# Eval("Department")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Now i want to give color to particular department like if person is from Account dept then the row's background color should be red, if person belongs to IT dept then Back ground color should be green.
I have tried it some code in ItemdataBound but that code changes only the back ground of labels.i want to change the row back ground.That code is
protected void lst_SentItems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label lblCount = (Label)e.Item.FindControl("lbl");
if (lblCount != null)
{
if (lblCount.Text == IT2")
{
lblCount.BackColor = System.Drawing.Color.Red;
}
}
}
}
Vision check this :It is just an idea and edit it according to your requirement.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
int policyid = (int)DataBinder.Eval(dataitem.DataItem, "EmpID");
if (policyid == 3)
{
HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("MainTableRow");
cell.Style.Add("background-color", "Red");
}
}
}

Resources