列表视图方法无效

时间:2017-02-02 07:44:24

标签: c# listview .net-4.5 listviewitem

我在ItemDataBound的帮助下使用lisview和绑定数据。

问题:当我点击标签时,它会显示与之相关的数据,但是当我点击其他数据时,它会显示与第1和第2项相关的数据,并持续到结束。

我尝试使用 SelectedIndexChanging 方法中的listviedo.Items.Clear();清除列表,但这不起作用。

帮我解决这个问题。

aspx.cs代码

public string getvalue(int ID)
{
Tbl_Multiple_Video_Master obj = DB.Tbl_Multiple_Video_Master.Single(p => p.VID == ID);
if (obj.IsUrl == false)
{
string Link = "<iframe id=\"paidVideo\" src=\"https://de122v0opjemw.cloudfront.net/utils/embedplayer.php?enablejsapi=1&video=" + obj.Name+"\" height=\"300\" width=\"500\"></iframe>";
return Link;
}
else
{
return "<iframe id=\"cartoonVideo\" width=\"560\" height=\"315\"  src=\"" + obj.Name + "\" frameborder=\"0\" allowfullscreen ></iframe>";
}
}
protected void listviedo_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Literal lblvedio = (Literal)e.Item.FindControl("lblvedio");
if (e.CommandName == "Select")
{
int ID = Convert.ToInt32(e.CommandArgument);
Database.Tbl_Multiple_Video_Master obj = DB.Tbl_Multiple_Video_Master.Single(p => p.VID == ID);
if (obj.IsUrl == false)
{
lblvedio.Visible = true;
string Link = "<iframe id=\"paidVideo\" src=\"https://de122v0opjemw.cloudfront.net/utils/embedplayer.php?enablejsapi=1&video=" + obj.Name + "\" height=\"300\" width=\"500\"></iframe>";
}
else
{
lblvedio.Visible = true;
string Link = "<iframe id=\"cartoonVideo\" width=\"560\" height=\"315\"  src=\"" + obj.Name + "\" frameborder=\"0\" allowfullscreen ></iframe>";
}
}
}
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_Multiple_Video_Master objcar = DB.Tbl_Multiple_Video_Master.Single(p => p.VID == ID);
}
protected void listviedo_SelectedIndexChanged(object sender, EventArgs e)
{
if (listviedo.SelectedIndex > -1)
{
SelectedVideo.Text = "Selected Video is : " + listviedo.SelectedValue.ToString();
}
else
{
SelectedVideo.Text = "Please Select a Video" + listviedo.SelectedIndex.ToString();
}
}
protected void listviedo_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
listviedo.Items.Clear();
}

protected void listviedo_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Literal lblvedio = (Literal)e.Item.FindControl("lblvedio");
if (e.CommandName == "Select")
{
int ID = Convert.ToInt32(e.CommandArgument);
Database.Tbl_Multiple_Video_Master obj = DB.Tbl_Multiple_Video_Master.Single(p => p.VID == ID);
 if (obj.IsUrl == false)
 {
lblvedio.Visible = true;
 string Link = "<iframe id=\"paidVideo\" src=\"https://de122v0opjemw.cloudfront.net/utils/embedplayer.php?enablejsapi=1&video=" + obj.Name + "\" height=\"300\" width=\"500\"></iframe>";
}
else
{
lblvedio.Visible = true;
string Link = "<iframe id=\"cartoonVideo\" width=\"560\" height=\"315\"  src=\"" + obj.Name + "\" frameborder=\"0\" allowfullscreen ></iframe>";
}
}
}
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_Multiple_Video_Master objcar = DB.Tbl_Multiple_Video_Master.Single(p => p.VID == ID);
}
protected void listviedo_SelectedIndexChanged(object sender, EventArgs e)
{
if (listviedo.SelectedIndex > -1)
{
SelectedVideo.Text = "Selected Video is : " + listviedo.SelectedValue.ToString();
}
else
{
SelectedVideo.Text = "Please Select a Video" + listviedo.SelectedIndex.ToString();
}
}
protected void listviedo_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
listviedo.Items.Clear();
}

aspx代码

<asp:ListView ID="listviedo" runat="server"
                                DataKeyNames="VideoName" 
                                OnItemCommand="listviedo_ItemCommand" 
                                OnItemDataBound="listviedo_ItemDataBound"
                                OnSelectedIndexChanged="listviedo_SelectedIndexChanged"
                                OnSelectedIndexChanging="listviedo_SelectedIndexChanging" >

                                <ItemTemplate>

                                    <div>
                                        <table>
                                            <tr style="border:solid;">
                                                <td style="border:solid;">
                                                    <asp:Literal ID="lblvedio"  runat="server" Visible="false" Text='<%#   getvalue(Convert.ToInt32(Eval("VID")))%>'></asp:Literal>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="border:solid;">
                                                    <span>
                                                        <asp:LinkButton runat="server" ID="lnkname" 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>
                                                    </span>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>

                                </ItemTemplate>

                  </asp:ListView>

1 个答案:

答案 0 :(得分:0)

试试这个

DataSource = null;
DataBind();

根据

C# Clear all items in ListView