Gridview FindControl 找到日历,但找不到 DropDownList

发布于 2024-10-15 11:01:33 字数 2480 浏览 1 评论 0原文

我在 GridView EditItemTemplate 中有一个 DropDownList。 ddl 将在运行时填充;每行中的选项可能有所不同。

GridView 正在按预期工作。在非编辑模式下,它绑定到数据。在编辑模式下,将呈现 EditItemTemplate 中指定的控件。

问题:为什么我无法在此 DropDownList 上 FindControl ??? 请注意,我可以在同样位于 EditItemTemplate 中的 Calendar 控件上查找控件。

下面是 ASPX 和C# 代码。

谢谢!!

<asp:UpdatePanel ID="UpdatePanelSelections" runat="server">
<ContentTemplate>
    <asp:GridView ID="GridViewSelections" runat="server" 
        ...

        <Columns>

            <asp:TemplateField HeaderText="Options" >
                <ItemTemplate>
                    <asp:Label ID="CurrentOption" runat="server" 
                        Text='<%# Eval("Options.OptionName") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:DropDownList ID="ddlOptions" runat="server" >
                    </asp:DropDownList>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Date" >
                <ItemTemplate>
                    <asp:Label ID="CurrentlySelectedDate" runat="server" 
                        Text='<%# Eval("SomeDate") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:Calendar ID="calNewDate" runat="server" SelectedDate='<%# Bind("SomeDate") %>' VisibleDate='<%# Bind("SomeDate") %>' />
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>

        ...


    protected void GridViewSelections_RowEditing(object sender, GridViewEditEventArgs e)
   {
    // FindControl calNewDate works in GridViewSelections_RowUpdating but not in GridViewSelections_RowEditing
    Calendar calNewDate = GridViewSelectionss.Rows[e.NewEditIndex].FindControl("calNewDate") as Calendar;

    // FindControl ddlOptions doesn't work anywhere!
    DropDownList ddlOptions = GridViewSelections.Rows[e.NewEditIndex].FindControl("ddlOptions") as DropDownList;

// Looking in Cells doesn't work either
DropDownList ddlOptions2 = GridViewSelections.Rows[e.NewEditIndex].Cells[2].FindControl("ddlOptions") as DropDownList;

I have a DropDownList in a GridView EditItemTemplate. The ddl is to be populated at runtime; options in each row may vary.

The GridView is working as desired. In non-edit mode, it binds to data. In edit mode the controls specified in the EditItemTemplate are rendered.

Question: Why can't I FindControl on this DropDownList???
Note that I can FindControl on a Calendar control that's also in an EditItemTemplate.

Below is the ASPX & C# code.

Thanks!!

<asp:UpdatePanel ID="UpdatePanelSelections" runat="server">
<ContentTemplate>
    <asp:GridView ID="GridViewSelections" runat="server" 
        ...

        <Columns>

            <asp:TemplateField HeaderText="Options" >
                <ItemTemplate>
                    <asp:Label ID="CurrentOption" runat="server" 
                        Text='<%# Eval("Options.OptionName") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:DropDownList ID="ddlOptions" runat="server" >
                    </asp:DropDownList>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Date" >
                <ItemTemplate>
                    <asp:Label ID="CurrentlySelectedDate" runat="server" 
                        Text='<%# Eval("SomeDate") %>'>
                    </asp:Label>
                </ItemTemplate>

                <EditItemTemplate>
                    <asp:Calendar ID="calNewDate" runat="server" SelectedDate='<%# Bind("SomeDate") %>' VisibleDate='<%# Bind("SomeDate") %>' />
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>

        ...


    protected void GridViewSelections_RowEditing(object sender, GridViewEditEventArgs e)
   {
    // FindControl calNewDate works in GridViewSelections_RowUpdating but not in GridViewSelections_RowEditing
    Calendar calNewDate = GridViewSelectionss.Rows[e.NewEditIndex].FindControl("calNewDate") as Calendar;

    // FindControl ddlOptions doesn't work anywhere!
    DropDownList ddlOptions = GridViewSelections.Rows[e.NewEditIndex].FindControl("ddlOptions") as DropDownList;

// Looking in Cells doesn't work either
DropDownList ddlOptions2 = GridViewSelections.Rows[e.NewEditIndex].Cells[2].FindControl("ddlOptions") as DropDownList;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-10-22 11:01:33

解决方案是检查 DataItem 是否不为空。仅检查 RowType 和 Edit 标志是不够的。

protected void GridViewSelections_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState.HasFlag(DataControlRowState.Edit) && (e.Row.DataItem != null)))
    {
        DropDownList ddlOptions = e.Row.FindControl("ddlOptions") as DropDownList;
        ddlOptions.Items.Add(new ListItem("aaa", "1"));
        ddlOptions.Items.Add(new ListItem("bbb", "2"));
        ddlOptions.Items.Add(new ListItem("ccc", "3"));

The solution turned out to be checking that the DataItem isn't null. Just checking for RowType and Edit flag wasn't enough.

protected void GridViewSelections_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState.HasFlag(DataControlRowState.Edit) && (e.Row.DataItem != null)))
    {
        DropDownList ddlOptions = e.Row.FindControl("ddlOptions") as DropDownList;
        ddlOptions.Items.Add(new ListItem("aaa", "1"));
        ddlOptions.Items.Add(new ListItem("bbb", "2"));
        ddlOptions.Items.Add(new ListItem("ccc", "3"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文