#c# #asp.net #listview
#c# #asp.net #listview
Вопрос:
У меня есть listview с именем, recent_all_albums
которое содержит imagebutton с именем Addtocart
в качестве шаблона элемента, я хочу быть невидимым только для выбранного элемента. Как это сделать?
My .aspx :
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
<div style="float: left; width: 988px;">
<table>
<tr>
<td style="width: 960px">
<div style="margin-top: 8px; border-bottom: 2px solid #DDDDDD; margin-bottom: 10px;">
<span class="style3">Featured Pictures </span>
</div>
<asp:ListView ID="Recent_Albums_All" runat="server" DataKeyNames="ID" DataSourceID="objDSPictureViewAll"
EnableViewState="false" OnSelectedIndexChanged="Recent_Albums_SelectedIndexChanged"
GroupItemCount="3" OnItemCommand="Recent_Albums_All_ItemCommand">
<EmptyItemTemplate>
<td id="Td1" runat="server" />
</EmptyItemTemplate>
<ItemTemplate>
<div class="PictureBlock1">
<div>
<asp:HyperLink ID="HyperLinkPicture" runat="server" ImageUrl='<%# Eval("ThumbnailPath") %>'
NavigateUrl='<%#"~/UI/PictureDetail.aspx?pid=" Eval("ID") %>' Text='<%# Eval("Title") %>'
Target="_parent" Height="150px">
</asp:HyperLink>
</div>
<div class="PictureBlockButton1">
<asp:ImageButton ID="ImageButtonAddToCart" runat="server" ImageUrl="~/image/shopcartadd.png"
AlternateText="AddCart" Height="40px" Width="40px" CommandName="AddToCart" CommandArgument='<%#Eval("ID") %>' />
<asp:ImageButton ID="ButtonAdded" runat="server" ImageUrl="~/image/shopcartapply.png"
Height="40px" Width="40px" Visible="false" />
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server">
<tr>
<td>
No data was returned.
</td>
</tr>
</table>
</EmptyDataTemplate>
<LayoutTemplate>
<div id="groupPlaceholderContainer" runat="server" class="PictureBlockContainer1">
<div id="groupPlaceholder" runat="server">
</div>
</div>
<div id="dvPager" class="header">
<asp:DataPager ID="DataPager1" runat="server" PageSize="21">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<GroupTemplate>
<div id="itemPlaceholderContainer" runat="server">
<div id="itemPlaceholder" runat="server">
</div>
</div>
</GroupTemplate>
<SelectedItemTemplate>
<div id="Div3" runat="server" style="background-color: #E2DED6; font-weight: bold;
color: #333333;">
</div>
</SelectedItemTemplate>
</asp:ListView>
</td>
</tr>
</table>
<asp:ObjectDataSource ID="objDSPictureViewAll" runat="server" DataObjectTypeName="PicArchieving.DAL.DAO.Picture"
SelectMethod="GetPublicPostedPictureList_All" OnSelecting="OnselectCallMethod"
TypeName="PicArchieving.BLL.PictureManager">
<SelectParameters>
<asp:Parameter Name="aUser" Type="Object" />
</SelectParameters>
<DeleteParameters>
</DeleteParameters>
</asp:ObjectDataSource>
</div>
</asp:Content>
My .cs :
namespace PicArchieving.UI
{
public partial class WebForm4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Recent_Albums_All.DataBind();
Recent_Albums_All.Dispose();
}
DataTable Cart = (DataTable)Session["selectedPictureObject1"];
}
protected void OnselectCallMethod(object sender, ObjectDataSourceSelectingEventArgs e)
{
User aUser;
if (Session["LoggedUser"] != null)
aUser = (User)Session["LoggedUser"];
else
{
aUser = new User();
aUser.UserID = null;
}
e.InputParameters["aUser"] = aUser;
}
protected void Recent_Albums_SelectedIndexChanged(object sender, EventArgs e)
{
PictureManager aManager = new PictureManager();
Picture aPicture = new Picture();
aPicture.ID = Convert.ToInt32(((ListView)sender).SelectedDataKey["ID"]);
Session["selectedPictureObject"] = aPicture;
objDSPictureViewAll.DataBind();
}
protected void Recent_Albums_All_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
DataTable Cart = Session["selectedPictureObject1"] == null ? new DataTable() : (DataTable)Session["selectedPictureObject1"];
if (Cart.Columns.Count > 0)
{
try
{
Cart.Rows.Add(e.CommandArgument.ToString());
//want to invisible ImageButtonAddToCart for that listview item
}
catch (Exception ex)
{
MessageBox.Show("This Item Already Exists In Cart");
}
}
else
{
DataColumn[] key = new DataColumn[1];
DataColumn id = new DataColumn();
Cart.Columns.Add(id);
id.ColumnName = "ID";
key[0] = id;
Cart.PrimaryKey = key;
Cart.Rows.Add(e.CommandArgument.ToString());
}
Session["selectedPictureObject1"] = Cart;
Button btn = (Button)Master.FindControl("ViewCart");
btn.CssClass = "viewCartFull";
btn.Visible = true;
}
}
}
}
Комментарии:
1. О чем вы просите? Мы должны отлаживать ваш код? Пожалуйста, прочтите это: tinyurl.com/so-hints
Ответ №1:
в .aspx :
<asp:ImageButton ID="ImageButtonAddToCart" runat="server" ImageUrl="~/image/shopcartadd.png" AlternateText="AddCart" Height="40px" Width="40px" CommandName="AddToCart" CommandArgument ='<%# ((ListViewDataItem)Container).DisplayIndex%>' />
в .cs:
protected void Recent_Albums_All_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "AddToCart")
{
((ImageButton)Recent_Albums_All.Items[index].FindControl("ImageButtonAddToCart")).Visible = false;
}
}