asp.net listview和findcontrol

时间:2011-07-01 11:43:51

标签: asp.net listview

我有listview,我需要将列表视图中的下拉列表绑定到ListItemCollection,它将使用函数BindPages()构建。 当我点击AddNew Link时,我无法绑定下拉列表。

<asp:ListView DataKeyNames="Menuid" OnItemCommand="lvParentMenus_ItemCommand" OnSorting="lvParentMenus_Sorting"
                        OnDataBound="lvParentMenus_DataBound" DataSourceID="SqlDataSource1" ID="lvParentMenus"
                        runat="server">
                        <LayoutTemplate>
                            <table border="0" id="listview" width="100%" class="grid" cellpadding="0" cellspacing="0">
                                <thead>
                                    <tr class="listingheader ">
                                        <td width="10%" style="text-align: center; !important">
                                            <input type="checkbox" name="checkbox" id="headercheck" />
                                        </td>
                                        <td id="thsno" runat="server">
                                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Sort" CommandArgument="Sno"
                                                Text="Sno" />
                                        </td>
                                        <td id="thmenutext" runat="server">
                                            <asp:LinkButton runat="server" ID="LinkButton2" Text="Menu Text" CommandName="Sort"
                                                CommandArgument="MenuText" />
                                        </td>
                                        <td id="thmenuurl" runat="server">
                                            <asp:LinkButton runat="server" ID="LinkButton3" Text="Menu Url" CommandName="Sort"
                                                CommandArgument="MenuUrl" />
                                        </td>
                                        <td id="thlevel" runat="server">
                                            <asp:LinkButton runat="server" ID="LinkButton4" Text="Level of Display" CommandName="Sort"
                                                CommandArgument="level" />
                                        </td>
                                        <td>
                                            Action
                                        </td>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr runat="server" id="itemPlaceholder">
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="3" align="center">
                                            <asp:Label ID="lblMessage" Text="dfdfdfd" runat="server"></asp:Label>
                                        </td>
                                        <td align="right">
                                            <asp:LinkButton Text="Add New" ID="lnkNew" CommandName="FillDropDown" runat="server"
                                                Font-Bold="true" OnClick="AddNew"></asp:LinkButton>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
<ItemTemplate>
                            <tr class='<%# Container.DataItemIndex % 2 == 0 ? "lrow1" : "lrow1 altrow" %>'>
                                <td class="col1" align="center">
                                    <asp:CheckBox runat="server" ID="chkitem"></asp:CheckBox>
                                </td>
                                <td class="lrow1">
                                    <%# Eval("Sno")%>
                                    <asp:HiddenField ID="hdnStoreID" runat="server" Value='<%# Eval("MenuId") %>' />
                                </td>
                                <td>
                                    <%# Eval("MenuText")%>
                                </td>
                                <td>
                                    <asp:DropDownList ID="ddlPagesList" runat="server" DataSource='<%#BindPages()%>'>
                                    </asp:DropDownList>
                                </td>
                                <td>
                                    <asp:DropDownList ID="ddlLevel" runat="server" DataSource='<%#BindLevel(6)%>' SelectedValue='<%# Eval("level")%>'>
                                    </asp:DropDownList>
                                </td>
                                <td nowrap="nowrap">
                                    <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                                    |
                                    <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" OnClientClick="javascript:return confirm('Are you sure to delete the current item');">Delete</asp:LinkButton>
                                </td>
                            </tr>
                        </ItemTemplate>

<InsertItemTemplate>
                            <tr class="lrow1">
                                <td class="col1" align="center">
                                    &nbsp;
                                </td>
                                <td class="lrow1">
                                    &nbsp;
                                </td>
                                <td class="lrow1">
                                    <asp:TextBox ID="txtMenuText" runat="server" Width="80px" Text='<%# Eval("MenuText")%>'
                                        CssClass="inputbox" ValidationGroup="InsertFields" />
                                    <asp:RequiredFieldValidator ID="reqValidCity" ControlToValidate="txtMenuText" runat="server"
                                        ErrorMessage="City Name is required." Display="Dynamic" ValidationGroup="InsertFields">*</asp:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="regValidCity" runat="server" ErrorMessage="Please Enter Alphabets only."
                                        Display="Dynamic" ValidationGroup="g1" ControlToValidate="txtMenuText" ValidationExpression="^[a-zA-Z0-9\s]{2,1000}"></asp:RegularExpressionValidator>
                                </td>
                                <td>
                                    <asp:DropDownList ID="ddlPagesList" runat="server" DataSource='<%#BindPages()%>'>
                                    </asp:DropDownList>
                                </td>
                                <td>
                                    <asp:DropDownList ID="ddlLevel" runat="server" DataSourceID="sdsLevel" DataValueField="level"
                                        DataTextField="level">
                                    </asp:DropDownList>
                                </td>
                                <td nowrap="nowrap">
                                    <asp:LinkButton ID="lnkinsert" runat="server" OnClick="lnkinsert_Click" ValidationGroup="InsertFields"> Insert</asp:LinkButton>
                                </td>
                            </tr>
                        </InsertItemTemplate>

</asp:ListView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="usp_getParentMenus"
                        SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="ddlRoles" Name="intRoleid" PropertyName="Text" DefaultValue="1"
                                ConvertEmptyStringToNull="true" Direction="Input" />
                        </SelectParameters>
                    </asp:SqlDataSource>

这是方法BindPAges()

 protected ListItemCollection BindPages()
{
    string sDir = Request.PhysicalApplicationPath;

    if (FirstCount == 0)
        DirSearch(sDir);

    return collection;
}

当我试图在AddNew()方法中找到ddlPageList时,它会抛出错误“Object referenc not set”

AddNEw()方法: `protected void AddNew(object sender,EventArgs e)     {

    lvParentMenus.InsertItemPosition = InsertItemPosition.FirstItem;
    lvParentMenus.FindControl("lnkNew").Visible = false;
    lvParentMenus.EditIndex = -1;

    sdsLevel.ConnectionString = DBConnectionString.ConnectionString;
    Parameter a = new Parameter("intRoleid", DbType.Int32);
    a.DefaultValue = ddlRoles.SelectedValue.ToString();
    sdsLevel.SelectParameters.Add(a);
    sdsLevel.SelectCommand = "usp_getParentMenus";

    DropDownList ddlpages = (DropDownList)lvParentMenus.FindControl("ddlPagesList");
    string sDir = Request.PhysicalApplicationPath;

    DirSearch(sDir);
    ddlpages.DataSource = collection;

ddlpages.DataBind();     }

In the image after clicking on the new link button I need to bind the dropdown to the list of pages in the dirctory.But it is not coming.

迫切需要。 感谢。

2 个答案:

答案 0 :(得分:1)

请尝试

DropDownList ddlpages = (DropDownList)lvParentMenus.Items[0].FindControl("ddlPagesList");

答案 1 :(得分:0)

您是否尝试在Listview中的TABLE中使用runat = server属性?