I recently switched a ComboBox in my repeater to a SuggestComboBox (found: here) because it is a contains search rather than a starts-with search. Previously, I was using NamingContainer of that ComboBox to find nearby elements. My new SuggestComboBox does not have this value but in my research on this problem, it looks like all children of a repeater should have this already?
"the NamingContainer property is available in code to any instance of that class or of a derived class." (found: here)
What am I missing?
Here's my repeater:
<asp:Repeater ID="repHW" runat="server" OnItemCommand="rep_ItemCommand">
<HeaderTemplate>
<table style="width:100%; padding-bottom:10px" id="HWtable">
<tr style="font-weight: bold"><td>Product</td><td>Part Number</td><td>Cost</td><td>Unit Price</td><td>Quantity</td><td>Price</td><td>Delete</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<asp:HiddenField ID="Category" Value="Hardware" runat="server"/>
<td><asp:Label ID="Product" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Name") %>' /></td> <!--TODO: make this clickable to edit -->
<td><asp:Label ID="PartNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.PartNumber") %>' /></td>
<td><asp:Label ID="PartCost" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Cost") %>' /></td>
<td><asp:Label ID="UnitPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Price") %>' /></td>
<td><asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>' /></td>
<td><asp:Label ID="Price" runat="server" Text='<%# Eval("Total") %>' /></td>
<td><asp:Button class="btn btn-danger" ID="DeleteHardware" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Container.ItemIndex %>'/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<asp:HiddenField ID="AddCategory" Value="Hardware" runat="server"/>
<td><SuggestComboBox runat="server" ID="AddProduct" AutoCompleteMode="SuggestAppend" AutoPostBack="true" OnSelectedIndexChanged="ProductSelected" OnDataBinding="LoadHardwareProducts"/></td>
<td><asp:TextBox runat="server" ID="AddPartNumber" ClientIDMode="static"/></td>
<td><asp:TextBox runat="server" ID="AddPartCost" ClientIDMode="static"/></td>
<td><asp:TextBox runat="server" ID="AddUnitPrice" ClientIDMode="static"/></td>
<td><asp:TextBox runat="server" ID="AddQuantity" ClientIDMode="static"/></td>
<td><asp:Button class="btn btn-success" ID="AddHardware" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Container.ItemIndex %>' onClientClick="return EmptyFieldCheck('Hardware');"/></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
and here's my function I am trying to access the other elements in:
protected void ProductSelected(Object source, EventArgs e)
{
SuggestComboBox temp = (SuggestComboBox)source;
List<Product> results = session.Query<Product>()
.Where(x => x.Name == temp.Text)
.ToList();
if(results.Count > 0)
{
Product p = results[0];
var repParent = temp.NamingContainer; //this broke
TextBox partNum = (TextBox)repParent.FindControl("AddPartNumber");
TextBox partCost = (TextBox)repParent.FindControl("AddPartCost");
TextBox unitPrice = (TextBox)repParent.FindControl("AddUnitPrice");
TextBox quantity = (TextBox)repParent.FindControl("AddQuantity");
partNum.Text = p.PartNumber;
partCost.Text = p.Cost.ToString();
unitPrice.Text = p.Price.ToString();
quantity.Text = p.DefaultQuantity.ToString();
}
}
Related
I have a product catalog page. When you click on the "Add Product" button, page with a cart shows up. This page has a table with an ItemTemplate inside it. Is there any way to get the value from the textBox located inside that ItemTemplate and change the value in the column Total cost in the table by clicking the button? The main problem is that I cannot access the textBox since it's in the . Thank you.
Catalog page:
Cart page:
CartView.aspx
MasterPageFile="~/Page/Store.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodyContent" runat="server">
<div id="content" style="margin-left: 7%;">
<style>
#import url("/css/tableCart.css");
#import url("/css/ButtonsCart.css");
</style>
<h2 style="padding: 14px; color:Highlight;">Ваша корзина</h2>
<h3 style="padding: 14px; color:Highlight;">Товары, которые вы добавили в корзину, представлены здесь</h3>
<table id="Table1" V class ="simple-little-table">
<thead>
<tr>
<th></th>
<th>Название</th>
<th>Цвет</th>
<th>Глубина</th>
<th>Ширина</th>
<th>Цена</th>
<th>Количество</th>
<th>Итого</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" ItemType="Line.Models.CartLine"
SelectMethod="GetCartLines" runat="server" EnableViewState="false">
<ItemTemplate>
<tr>
<td><asp:Image ID="Image1" runat="server" style="height:45px; " ImageUrl=<%# Item.Product.Img %> /></td>
<td> <%# Item.Product.NameProduct %> <%# Item.Product.TypeProducts %></td>
<td><%# Item.Product.Colors %></td>
<td>
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<asp:Button ID="cmdUpdate" OnClick="cmdUpdate_Click1" runat="server" Text="Update" CommandName="MyUpdate" CommandArgument = '<%# Container.ItemIndex %>'/>
</td>
<td><%# Item.Size.Depth%></td>
<td><%# Item.Product.Price%></td>
</td>
<td>
<td>
<asp:Label ID="Label2" runat="server" Text="<%# ((Item.Quantity *
Item.Product.Price))%>"></asp:Label>
</td>
<td>
<asp:Label ID="txtAmount" runat="server" Text=""></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
<tr>
<td colspan="3">Итого:</td>
<td colspan="2" ><%= CartTotal.ToString("c") %></td>
</tr>
</tfoot>
</table>
</div>
</asp:Content>
CartView.aspx.cs
using Line.Helpers;
using Line.Models;
using Line.Models.Repository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Line.Page
{
public partial class CartView : System.Web.UI.Page
{
protected void Page_Load(object sender, RepeaterCommandEventArgs e)
{
}
public IEnumerable<CartLine> GetCartLines()
{
return SessionHelper.GetCart(Session).Lines;
}
public decimal CartTotal
{
get
{
return SessionHelper.GetCart(Session).ComputeTotalValue();
}
}
public string CheckoutUrl
{
get
{
return RouteTable.Routes.GetVirtualPath(null, "checkout",
null).VirtualPath;
}
}
public string ReturnUrl
{
get
{
return SessionHelper.Get<string>(Session, SessionKey.RETURN_URL);
}
}
protected void cmdUpdate_Click1(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items[Convert.ToInt32(e.CommandArgument)];
TextBox tQty = (TextBox)rRow.FindControl("txtQty");
Label tAmount = (Label)rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text;
}
}
}
}
Ok, so you can set the index of the button, and pick this up in the Repeater "item command"
So, for your button, you can/want say this:
I have qty, price, and amount in the repeater. and button.
So, the markup can look like this:
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
So, you are now free to enter Qty, amount in any of the repeated items.
I have this:
Now, my repeater is going accross - and I think you should be using a listview since that better supports a grid + columnar layout - but it really don't mater (listview, gridview, repeater - they all work the same).
So, note how in our button we have both command Name, and command argument. In command argument I pass the row of the repeater.
So, the code looks like this:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// update button in repeater clicked
// update amount based on qty, and price
if (e.CommandName == "MyUpdate")
{
RepeaterItem rRow = Repeater1.Items(e.CommandArgument);
TextBox tQty = rRow.FindControl("txtQty");
TextBox tPrice = rRow.FindControl("txtPrice");
TextBox tAmount = rRow.FindControl("txtAmount");
tAmount.Text = tQty.Text * tPrice.Text;
}
}
So, just pass the "index" of the repeater row as per above. We used this expression as command argument:
<asp:Button ID="cmdUpdate" runat="server" Text="Update"
CommandName="MyUpdate"
CommandArgument = '<%# Container.ItemIndex %>'/>
So, once you have the Item Index (row index), then you can run code against the one row and change that row as per above.
Edit:
So the full markup I have is this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div style="border-style:solid;color:black;width:250px;float:left">
<div style="padding:5px;text-align:right">
Hotel Name: <asp:TextBox ID="txtHotelName" runat="server" Text ='<%# Eval("HotelName") %>' Width="130px" />
<br />
First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' Width="130px" />
<br />
Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>' Width="130px" />
<br />
Qty: <asp:TextBox ID="txtQty" runat="server" Width="130px" />
<br />
Price: <asp:TextBox ID="txtPrice" runat="server" Width="130px" />
<br />
Amount: <asp:TextBox ID="txtAmount" runat="server" Width="130px" />
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update" CommandName="MyUpdate" CommandArgument = '<%# Container.ItemIndex %>' />
<div>
City : <asp:DropDownList ID="cboCity" runat="server" DataTextField="City"
DataValueField="City" Width="110px">
</asp:DropDownList>
<div style="float:right;text-align:center;margin-left:4px;">
<asp:Button ID="cmdAddCity" runat="server" Text="+" OnClick="cmdAddCity_Click" Height="16px" Width="12px" Font-Size="XX-Small" cssclass="btnPad" OnClientClick="AddCity();return false"/>
</div>
</div>
Active: <asp:CheckBox ID="chkActive" runat="server" Checked = '<%# Eval("Active") %>'/>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
The code to load up this repeater is this:
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack == false)
LoadGrid();
}
public void LoadGrid()
{
cmdSQL.Connection.Open();
strSQL = "SELECT ID, FirstName, LastName, HotelName, City, Active from tblHotels ORDER BY HotelName";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, new SqlConnection(My.Settings.TEST3)))
{
cmdSQL.Connection.Open();
rstTable.Load(cmdSQL.ExecuteReader);
Repeater1.DataSource = rstTable;
Repeater1.DataBind();
}
}
so the above fills out the repeater - as noted, for a grid like layout, I would use a listview. Drag a list view into a web page.
use the wizards to connect to the database. Now blow out all the templates, only leave the itemtemplate.
Your code to load up the grid - same as above.
now, in place of the repeater as per previous screen shots?
You get this:
So your multiple lines that your building? They should be based on and around a listview. And each row can thus be grabbed, and addressed just as I did per above.
Do a google for listview examples - there is a like a billion examples.
Once you have this setup, then each row is a "thing" that repeats for you automatic based on the data from the table.
The markup for the list view - it again quite much follows the same ideas and concepts as a repeator. the listview for above looks like this:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr style="">
<td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
<td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Eval("Active") %>' Enabled="false" /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">ID</th>
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Active</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
I have a list of objects called dashboard which holds a list of Announcements with their AnnouncementId. My DataListAnnouncements.DataSource is equal to the dashboard but I can't figure out how to get the selectedItem equal to the item that I am selecting in the client side.
What Currently is happening is when I select an unread announcement, it displays the message with some css, but it does that to the wrong announcement at the moment.
protected void DataListAnnouncements_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Select")
{
IList<DashBoardView> dashboard = new List<DashBoardView>();
dashboard = (IList<DataObjects.DashBoardView>)ListOfObjects;
UnreadAnnouncement unread = UnreadAnnouncements;
if (e.CommandArgument != null)
{
if (unread != null)
{
unread.WasRead = true;
UpdateUnreadAnnouncement(unread);
att = unread.AnnouncementId;
}
}
dashboard[e.Item.ItemIndex].WasRead = unread.WasRead;
DataListAnnouncements.DataSource = Session["dashboard"];
DataListAnnouncements.SelectedItem = dashboard[e.Item.ItemIndex]
(DataListAnnouncements.SelectedItem = att <---- Something Like this is what I would like to implement?)
Session["dashboard"] = dashboard;
bindDataList();
}
}
as you see now, I have it using the dashboard[e.Item.ItemIndex].WasRead = unread.WasRead line, but this proves to not work since the index is not always what I want. So I want to use an attribute. I looked around a bit but I am still fuzzy on how to implement it on the C# and asp side.
and this is my ASP.
<ItemTemplate>
<table width="880px">
<tr>
<td class="leftCol">
<asp:LinkButton Text='<%# Eval("title") %>' CssClass="bold" runat="server" CommandName="Select" CommandArgument='<%# Eval("UnreadAnnouncementId") %>' ID="titleLabel" /></span>
</td>
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="one-long-line">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table width="880px" cellpadding="10px">
<tr>
<td class="leftCol">
<asp:Label Text='<%# Eval("title") %>' CssClass="bold" runat="server" ID="titleLabel" />
<asp:Label runat="server" ID="wasRead" Text='<%# Eval("wasRead") %>' Visible="false" Enabled="false" />
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="deailsTD" colspan="2">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
<tr>
</tr>
</table>
</SelectedItemTemplate>
I want to be able to click on a item in the datagrid, and it will expand that item and show me the details.
You have to use `SelectedIndex', not SelectedItem'. Source: How to: Allow Users to Select Items in DataList Web Server Controls.
It could be like this:
DataListAnnouncements.SelectedIndex = e.Item.ItemIndex;
From your code it is not clear what is the relation between dashboard and att. If there's any way to find the att in dashboard and find it's position, you can do like this:
DataListAnnouncements.SelectedIndex = attposition;
EDIT : There's an easy way to control background color of DataLists Items. We can parse each item in DataList's ItemBound and set BackColor like below:
protected void DataListAnnouncements_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem||
e.Item.ItemType == ListItemType.SelectedItem
)
{
// Here BackColor - Grey: WasRead; Yellow: Unread
var wasRead = ((DashBoardView)e.Item.DataItem).WasRead;
e.Item.BackColor = wasRead? System.Drawing.Color.Gray: System.Drawing.Color.Yellow;
}
}
You can even expand it more by splitting the if condition into two:
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem){}
and
if ( e.Item.ItemType == ListItemType.SelectedItem){}
I want open a Dialog with Controls and it must open if I click on a LinkButton in a ListView. I want make it with a CommandName and OnCommand Argumen.
My Application:
In My Application can a User search other Users about a textBox and list the users in a ListView. Than he can display more Informations about one User if the user click on the LinkButton.
If I make it about a normal Button, the Dialog open but if I make it about the LinkButton with CommandName it Don't work.
My Code:
<form id="form1" runat="server">
<asp:HiddenField ID="hidForModel" runat="server" />
<asp:scriptmanager id="ScriptManager1" runat="server">
</asp:scriptmanager>
<asp:modalpopupextender id="ModalPopupExtender1"
runat="server" cancelcontrolid="btnCancel"
okcontrolid="btnOkay" targetcontrolid="hidForModel"
popupcontrolid="Panel1" popupdraghandlecontrolid="PopupHeader"
drag="true" backgroundcssclass="ModalPopupBG">
</asp:modalpopupextender>
<div class="popupConfirmation" id="Panel1" >
<asp:Panel runat="server" ID="PopupHeader" CssClass="modalPopup">Benutzerinformationen</asp:Panel>
<div class="Body">
Hallo
<asp:Button id="btnOkay" runat="server" text="Speichern" />
<asp:Button id="btnCancel" runat="server" text="Abbrechen" />
</div>
</div>
<div class="header">
<table id="SuchTabelle" runat="server" border="0">
<tr>
<th><asp:Label ID="id_SearchUser" runat="server" Text="lblSearchUser"></asp:Label></th>
<th><asp:TextBox ID="txtBenutzer" runat="server" Width="250px"></asp:TextBox></th>
</tr>
<tr>
<th><asp:Label ID="id_location" runat="server" Text="lblLocation"></asp:Label></th>
<th><asp:DropDownList ID="dropWerk" runat="server" Width="250px" /></th>
<th><asp:Button ID="Button2" runat="server" Text="Suchen"
onclick="btnBenutzerSuchen_Click" Width="219px" /></th>
</tr>
</table>
<div id="bild">
<asp:Image runat="server" ImageUrl="~/App_Theme/lw_logo.jpg" Height="58px"
Width="277px" />
</div>
</div>
<div class="bodyList">
<asp:ListView runat="server" ID="myListView">
<LayoutTemplate>
<table id="UserTable" runat="server" border="0" width="800" cellpadding="0" cellspacing="0">
<tr style="background-color:#E5E5FE" class="TableClass">
<th id="th1" runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" >id_Benutzer</asp:LinkButton></th>
<th id="th2" runat="server"><asp:LinkButton ID="lnkemail" runat="server" >id_Email</asp:LinkButton></th>
<th id="th3" runat="server"><asp:LinkButton ID="lnkVorname" runat="server" >id_Vorname</asp:LinkButton></th>
<th id="th4" runat="server"><asp:LinkButton ID="lnkNachname" runat="server" >id_Nachname</asp:LinkButton></th>
<th id="th5" runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" >id_Telefon</asp:LinkButton></th>
</tr>
<tr runat="server" id="ItemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="TableClass">
<td align="left" ><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="TableClass">
<td align="left" ><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:LinkButton CssClass="MyLink" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
<br />
<br />
</div>
</form>
My C# Code:
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Select")
{
ModalPopupExtender1.Show(); // error :(
}
}
My Errortext:
System.InvalidOperationException: The ListView "myListView" has one SelectedIndexChanging-Event execute, which was not treated.
I need help :(
The Update:
protected void myListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
txtVorname.Text = "test";
PopUpDialog.Show(); //<-- Here is the error
}
}
I make a breakpoint and see ...
tarasov
Try this:
Handle "ItemCommand" Event of "ListView":
protected void myListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Select"))
{
ModalPopupExtender1.Show();
//your code
}
}
I have a ASP.NET Application and I use a ListView. If I create a ListItem (a line) I want use a LinkButton. I want that this LinkButton have the CSS Properties..
color:Black;
text-decoration:none;
But if I start the Application. I get the linkButtons as Blue and with underline :(
here my Code:
ASPX:
...
<asp:ListView runat="server" ID="myListView">
<LayoutTemplate>
<table id="UserTable" runat="server" border="0">
<tr id="Tr1" style="background-color:#E5E5FE">
<th runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" >id_Benutzer</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkemail" runat="server" >id_Email</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkVorname" runat="server" >id_Vorname</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkNachname" runat="server" >id_Nachname</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" >id_Telefon</asp:LinkButton></th>
</tr>
<tr runat="server" id="ItemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left" ><asp:LinkButton ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#EFEFEF">
<td align="left" ><asp:LinkButton ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
...
My CSS File:
...
#Label1
{
color:Black;
text-decoration:none;
}
...
What is wrong ?
tarasov
#Label1 means that only the control with an id="Label1" in the final HTML will get formatted by the CSS.
When you create a control within ASP.NET (whether in the page, user-control, repeater, etc) it will not be just called Label1 in the HTML, but something like ctl100_Label1.
What you need to do is create the CSS as a class and then use that class on your controls using CssClass.
.MyLabel {
color: Black;
text-decoration: none;
}
<asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" CssClass="MyLabel" />
Another option (rather than putting the CssClass into every single asp:Label) is to either use the ID or CssClass of one of the parent controls, and use that instead. For instance...
<table id="UserTable" runat="server" border="0" class="UserTableClass">
.UserTableClass span {
color: Black;
text-decoration: none;
}
The Solution
....
<td align="left" ><asp:LinkButton ForeColor="Black" Font-Underline="false" ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
...
The ASP ID works differently by default and isn't the same as HTML id, you can tell it to if you want using ClientIDMode.
Alternatively you can set the CssClass of an asp:LinkButton.
<asp:LinkButton .... CssClass="Label1" ... />
My Program:
I' ve a ListView, that get Data from the Active Directory. The User input a String (Lastname or a part of this) in a TextBox. Than the ListView list all AD Users with the same string from the TextBox. Every Line (Row) get a Button "Anzeigen" to get more Informations about the User.
ASPX:
<asp:ListView runat="server" ID="myListView">
<LayoutTemplate>
<table id="UserTable" runat="server" border="0" cellspacing="10" cellpadding="2">
<tr runat="server" id="Tr1" style="background-color:#E5E5FE">
<th runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" CommandName="Sort" CommandArgument="Benutzer" onsorting="ListView1_Sorting">Benutzer</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkemail" runat="server" CommandName="Sort" CommandArgument="eMail" onsorting="ListView1_Sorting" >eMail</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkVorname" runat="server" CommandName="Sort" CommandArgument="Vorname" onsorting="ListView1_Sorting" >Vorname</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkNachname" runat="server" CommandName="Sort" CommandArgument="Nachname" onsorting="ListView1_Sorting" >Nachname</asp:LinkButton></th>
<th runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" CommandName="Sort" CommandArgument="Telefon" onsorting="ListView1_Sorting" >Telefon</asp:LinkButton></th>
</tr>
<tr runat="server" id="ItemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="MyArgument" runat="server" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#EFEFEF">
<td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
<td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Anzeigen" CommandArgument="MyArgument" runat="server" /></td>
</tr>
</AlternatingItemTemplate>
CS File:
...
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
{
//here???
}
...
What I want:
If the User click on LinkButton "Benutzer", the list must be sort by alphabet :P I look on the Site http://www.codeproject.com/Articles/24570/Complete-ListView-in-ASP-NET-3-5 but in the Example and in the Downloadfile doesn't exist this Sort Method :(
now I don't know how I can sort the list.
So I fill my List with Data:
protected void btnBenutzerSuchen_Click(object sender, EventArgs e)
{
DirectoryEntry Entry = new DirectoryEntry("LDAP://" + "Domain");
string filter = "(&(objectClass=user)(objectCategory=person)(cn=" + txtBenutzer.Text + "*))";
DirectorySearcher Searcher = new DirectorySearcher(Entry, filter);
var q = from s in Searcher.FindAll().OfType<SearchResult>()
select new
{
Benutzer = GetProperty(s, "sAMAccountName"),
eMail = GetProperty(s, "mail"),
Vorname = GetProperty(s, "givenName"),
Nachname = GetProperty(s, "sn"),
Telefon = GetProperty(s, "telephoneNumber")
};
this.myListView.DataSource = q;
this.myListView.DataBind();
}
tarasov
Here you want to sort your data according to dataname.
For that ,you have to pass datakey to rowdataBound event.
Like' :
protected void gvEmployeeList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "name")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = gvEmployeeList.Rows[index];
string deptID = gridview1.DataKeys[index].Value.ToString().Trim();
DataTable dtEmplist = new DataTable();
dtEmplist = getFilterEmployeeList(deptID);
if (dtEmplist.Rows.Count > 0)
{
gridview1.DataSource = dtEmplist;
gridview1.DataBind();
}
else
{
lblMsg.Text = "No Data Available";
}
}
}
Affter getting datakey
Sort it by (GridViewSortEventArgs e)
e.sortExpression property.
It will help you my frnd....