嵌套列表视图

时间:2011-02-24 06:39:12

标签: c# asp.net listview nested

我有列表视图,在里面的lisetview我有另一个列表,如嵌套列表视图 lv1 - > lv2和里面lv2我有按钮,当我点击按钮而不是插入模板显示但如何可以精细控制lv2 ....?有我的代码
Lv1正在工作,但lv2正在制造问题..?

protected void lv1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "NewRecord")
    {

       lv1.InsertItemPosition = InsertItemPosition.FirstItem;

    }
}

protected void lv2_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "NewRecord")
    {
       //ListView lv2 = (ListView)e.Item.FindControl("lv2");
       //lv2.InsertItemPosition = InsertItemPosition.FirstItem;
    }
}

2 个答案:

答案 0 :(得分:4)

实际上,您可以通过转换sender参数:

轻松访问您的lv2控件
protected void lv2_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "NewRecord")
    {
       ListView lv2 = (ListView)sender;
       lv2.InsertItemPosition = InsertItemPosition.FirstItem;
    }
}

答案 1 :(得分:1)

嘿哈瑞我觉得我非常了解waqa waqa !! 首先,你确定项目模板中的第二个列表视图??? 或者它是在select,edite或插入模板???

如果它在项目模板中,那么:

要查找第二个列表视图,您应该使用此代码: 在vb.net中:

dim Lv2 as listvew = lv.Item.FindControl("listview2")

在c#中:

listvew Lv2 = lv.Item.FindControl("listview2");

然后你必须在lv2

中找到你的按钮
button newbtn= lv2.Item.FindControl("UrBtnName");

然后你可以

看到这是vb.net中的代码

Sub buttons(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles LVCategories.ItemCommand
        Try
            Select Case e.CommandName

                Case "Delete"
                    'this to take a value from any control
                    Dim Idlabel As Label = e.Item.FindControl("CatIDLabel")
                    Session("ID") = Idlabel.Text()
                Case "new"
                    'Show the insert template
                    LVCategories.InsertItemPosition = InsertItemPosition.FirstItem
                Case "Cancel"
                    'Hide code
                    LVCategories.InsertItemPosition = InsertItemPosition.None

                Case "Edit"
                    'Hide code
                    LVCategories.InsertItemPosition = InsertItemPosition.None

                Case "Update"
                    Dim PictureIDlbl As Label = LVCategories.EditItem.FindControl("ImageIDLabel")
                    '
                    Dim fu As FileUpload = LVCategories.EditItem.FindControl("FileUpload")
                    If fu.HasFile Then

                        Dim PictureID As String = PictureIDlbl.Text()
                        Session("ImageID") = PictureID.ToString

                        Dim filepath As String = Path.Combine(Server.MapPath("~/ADMIN/ImageUpload/Categories/"), PictureID + ".jpg")
                        fu.SaveAs(filepath)
                    End If
                Case "Insert"
                    'Uploader Code
                    Dim fu As FileUpload = LVCategories.InsertItem.FindControl("FileUpload1")
                    Dim ad As New Images()
                    Dim dt As Images.ImagesDataTable
                    ad.DML("1", Nothing, "Categories", "Category Image")
                    dt = ad.Read("3", Nothing, Nothing)
                    Dim DR As DataRow = dt.Rows(0)
                    Dim Imgid As String = DR.Item("ImageID")
                    Session("ImageID") = Imgid.ToString
                    If fu.HasFile Then
                        Dim filepath As String = Path.Combine(Server.MapPath("~/ADMIN/ImageUpload/Categories/"), Imgid + ".jpg")
                        fu.SaveAs(filepath)
                    End If
                    'Hiding the insert template
                    LVCategories.InsertItemPosition = InsertItemPosition.None
            End Select

        Catch ex As Exception

        End Try


    End Sub

你可以在C#中看到这段代码:

public void buttons(object sender, ListViewCommandEventArgs e)
{
try {
    switch (e.CommandName) {

        case "Delete":
            //this to take a value from any control
            Label Idlabel = e.Item.FindControl("CatIDLabel");
            Session("ID") = Idlabel.Text();
            break;
        case "new":
            //Show the insert template
            LVCategories.InsertItemPosition = InsertItemPosition.FirstItem;
            break;
        case "Cancel":
            //Hide code
            LVCategories.InsertItemPosition = InsertItemPosition.None;

            break;
        case "Edit":
            //Hide code
            LVCategories.InsertItemPosition = InsertItemPosition.None;

            break;
        case "Update":
            Label PictureIDlbl = LVCategories.EditItem.FindControl("ImageIDLabel");
            //
            FileUpload fu = LVCategories.EditItem.FindControl("FileUpload");

            if (fu.HasFile) {
                string PictureID = PictureIDlbl.Text();
                Session("ImageID") = PictureID.ToString();

                string filepath = Path.Combine(Server.MapPath("~/ADMIN/ImageUpload/Categories/"), PictureID + ".jpg");
                fu.SaveAs(filepath);
            }
            break;
        case "Insert":
            //Uploader Code
            FileUpload fu = LVCategories.InsertItem.FindControl("FileUpload1");
            Images ad = new Images();
            Images.ImagesDataTable dt = default(Images.ImagesDataTable);
            ad.DML("1", null, "Categories", "Category Image");
            dt = ad.Read("3", null, null);
            DataRow DR = dt.Rows(0);
            string Imgid = DR["ImageID"];
            Session("ImageID") = Imgid.ToString();
            if (fu.HasFile) {
                string filepath = Path.Combine(Server.MapPath("~/ADMIN/ImageUpload/Categories/"), Imgid + ".jpg");
                fu.SaveAs(filepath);
            }
            //Hiding the insert template
            LVCategories.InsertItemPosition = InsertItemPosition.None;
            break;
    }


} catch (Exception ex) {
}
}

此示例中有很多示例可以了解如何在列表视图中查找控件

谢谢,祝你好运。