我想让我的listview滚动到用户选择的索引并更改其外观。我厌倦了在很多方面得到这个,但没有任何方法可以帮助我。
当我从listview中选择任何项目时,我能够获取所选项目的数据和索引,但SelectedTemplate适用于以前的索引。不知道怎么样?
例如:默认情况下我选择索引“0”,所以当我选择索引“3”时, SelectedTemplate适用于索引“0”当我选择索引“5”时, SelectedTemplate适用于索引“3”。
代码如下,希望从Stack Overflow获得一些帮助。
web窗体
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<div class="VDOContainer raw">
<div class="vdoname" id="area" runat="server">
<asp:Label ID="VDOName" runat="server" CssClass="vdofonthead" Text="Video Name"></asp:Label>
</div>
<div class="boxleft col-lg-8 col-md-8 col-sm-12">
<asp:Literal ID="lblvedio" runat="server"></asp:Literal>
<ul class="raw like-section">
<li class="col-md-6 like-btn">
<asp:Button ID="btn_Like" runat="server" Text="Like" CssClass="btn btn-outline btn-lg"></asp:Button>
<div class="btn btn-lg" style="cursor:default;">
<img class="vlike" src="img/like.png" alt="like image" />
<asp:Label ID="lblLike" runat="server"></asp:Label>
</div>
<div class="btn btn-lg" style="cursor:default;">
<img class="vlike" src="img/like.png" alt="like image" />
<asp:Label ID="lblView" runat="server"></asp:Label>
Views
</div>
</li>
</ul>
<div class="vdoname" id="Div1" runat="server">
<asp:Label ID="VDODesc" runat="server" CssClass="vdofonthead" Text="Video Description"></asp:Label>
</div>
</div>
<div class="boxright col-lg-4 col-md-4 col-sm-12">
<asp:ListView runat="server" ID="listviedo"
OnSelectedIndexChanged="listviedo_SelectedIndexChanged"
OnSelectedIndexChanging="listviedo_SelectedIndexChanging"
SelectedIndex="0" FullRowSelect="true"
DataKeyNames="VID" ClientIDMode="AutoID">
<ItemTemplate>
<asp:Panel runat="server" ID="selected_panel" >
<div class="vdofont">
<asp:LinkButton runat="server" ID="linkbtn" CommandName="Select" CommandArgument='<%# Eval("VID") %>'>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("VideoName")%>'></asp:Label>
<asp:Label ID="lblid" runat="server" Visible="false" Text='<%# Eval("VID")%>'></asp:Label>
</asp:LinkButton>
</div>
</asp:Panel>
</ItemTemplate>
<SelectedItemTemplate>
<asp:Panel runat="server" ID="selected_panel" CssClass="boxright_active" BackColor="SkyBlue">
<div class="vdofont boxright_active">
<asp:LinkButton runat="server" ID="linkbtn" CommandName="Select" CommandArgument='<%# Eval("VID") %>'>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("VideoName")%>'></asp:Label>
<asp:Label ID="lblid" runat="server" Visible="false" Text='<%# Eval("VID")%>'></asp:Label>
</asp:LinkButton>
</div>
</asp:Panel>
</SelectedItemTemplate>
</asp:ListView>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="listviedo" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="listviedo" EventName="SelectedIndexChanging" />
<asp:AsyncPostBackTrigger ControlID="listviedo" EventName="ItemCommand" />
</Triggers>
</asp:UpdatePanel>
C#背后
protected void listviedo_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Literal lblvedio = (Literal)e.Item.FindControl("lblvedio");
Label lblname = (Label)e.Item.FindControl("lblname");
if (e.CommandName == "Select")
{
int ID = Convert.ToInt32(e.CommandArgument);
Database.Tbl obj = DB.Tbl.Single(p => p.VID == ID);
if (obj.IsUrl == false)
{
string Link = "<iframe></iframe>";
}
else
{
string Link = "<iframe></iframe>";
}
}
if (e.CommandName == "Resume")
{
int ID = Convert.ToInt32(e.CommandArgument);
Database.Tbl objcar = DB.Tbl.Single(p => p.VID == ID);
}
}
protected void listviedo_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label lblid = (Label)e.Item.FindControl("lblid");
LinkButton linkresuume = (LinkButton)e.Item.FindControl("linkresuume");
int ID = Convert.ToInt32(lblid.Text);
Database.Tbl objcar = DB.Tbl.Single(p => p.VID == ID);
}
protected void listviedo_SelectedIndexChanged(object sender, EventArgs e)
{
if (Session["USER"] == null)
{
Response.Redirect("Login");
}
else
{
if (listviedo.SelectedIndex >= 0)
{
int id1 = Convert.ToInt32(listviedo.SelectedValue.ToString());
lblvedio.Text = getvalue(id1);
VDOName.Text = getName(id1);
VDODesc.Text = getDesc(id1);
lblView.Text = getView(id1).ToString();
lblLike.Text = getLike(id1).ToString();
}
}
}
protected void listviedo_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
this.listviedo.SelectedIndex = e.NewSelectedIndex;
}
CSS
.boxright {
float: left;
height: 890px;
margin: 0 auto;
overflow: auto;
padding: 10px 10px 10px 10px;
background-color: rgba(0, 0, 255, 0.12);
}
.boxright div:hover {
transform: scale(1.01);
box-shadow: 1px 1px 5px 0px #777;
border-radius:5px;
}
.boxright_active {
transform: scale(1.01);
box-shadow: 1px 1px 10px 0px #777;
border-radius:5px;
}
.boxright div:active {
transform: scale(1.01);
box-shadow: 1px 1px 10px 0px #777;
border-radius:5px;
}
.boxleft {
float: left;
margin: 0 auto;
padding: 15px;
overflow: hidden;
}