【问题标题】:2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control2 Gridview中的Dropdownlists导致报错:Bind()等数据绑定方法只能在数据绑定控件的上下文中使用
【发布时间】:2017-01-05 11:20:37
【问题描述】:

虽然这似乎之前讨论过,但我无法在我的情况下申请给予帮助:

我在绑定到 SQLDataSource 的 Gridview 的编辑模板中有 2 个 DropDownLists(DDL1、DDL2)。 DDL2 取决于 DDL1 的选定值。

如果我将填充 DropDownLists 的 SQLDataSources 放在顶层,即使我正确地处理了它(Gridview$DDL1ControlID),也找不到 DDL2 的 ControlParameter 的 ControlID。因此,我将 SQLDataSources 放置在 Gridview 的编辑模板中。现在,如果我进入给定记录的编辑模式,两个 DropDownLists 都会正确填充;如果我更改 DDL1 的索引,则 DDL2 不会自动更新,因为 DDL2 的 AutoPostBack 设置为 false。一旦我将 AutoPostBack 设置为 true,就会抛出错误“Eval()、XPath() 和 Bind() 等数据绑定方法只能在其中使用”。

    <asp:GridView runat="server" ID="Gridview1" 
                  DataSourceID="Milestones" 
                  DataKeyNames="ID"
                  OnRowEditing="OnRowEditing"
                  OnRowDataBound="OnRowDataBoundMS"
                  OnSelectedIndexChanged="OnSelectedIndexChangedMS">
....
            <asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
                    ConnectionString="Data Source=XXXXX"    
                    SelectCommand="select function, 2 as ord
                                   from Staff
                                   where function is not null
                                   group by function
                                   union all
                                   select 'select' as function, 0 as ord
                                   union all
                                   select '-----' as function, 1 as ord
                                   order by ord, function">
                </asp:SqlDataSource>
                <asp:DropDownList runat="server" id="DDOwnerGroup" Text='<%# Bind("DefaultOwnerGroup") %>' DataSourceID="OwnerGroups" DataTextField="function" DataValueField="function" Font-Size="12px" Width="80px" AutoPostBack="true"/>
            </EditItemTemplate>
        </asp:templatefield>

        <asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient" 
                    ConnectionString="Data Source=XXXXX"    
                    SelectCommand="select Name, UserID
                                   from Staff
                                   where function = @OwnerGroup
                                   union all
                                   select Null as Name, Null as UserID
                                   order by Name">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup"  Type="String" />
                </SelectParameters>
                </asp:SqlDataSource>
                <asp:DropDownList runat="server" id="DDOwner" Text='<%# Bind("DefaultOwner") %>' Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
            </EditItemTemplate>
        </asp:templatefield>

【问题讨论】:

  • 好的,-愚蠢-我明白了...我不必绑定DropDownList中的DataField;相反,我必须在后面的代码中设置 SelectedValue。放置一个 HiddenField 可以很容易地检索该值。

标签: asp.net gridview data-binding drop-down-menu sqldatasource


【解决方案1】:

我像这样在 OnRowDataBound 事件中绑定 DropdownLists:

            <asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroupEdit" Visible="false"/>
                <asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
                    ConnectionString="Data Source=XXXXX"    
                    SelectCommand="...">
                </asp:SqlDataSource>
                <asp:DropDownList runat="server" id="DDOwnerGroup" DataSourceID="OwnerGroups" DataTextField="funcion" DataValueField="funcion" Font-Size="12px" Width="80px" AutoPostBack="true"/>
            </EditItemTemplate>
        </asp:templatefield>

        <asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwnerEdit" Visible="false"/>
                <asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient" 
                    ConnectionString="Data Source=XXXXX"    
                    SelectCommand="...."
                <SelectParameters>
                    <asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup"  Type="String" />
                </SelectParameters>
                </asp:SqlDataSource>
                <asp:DropDownList runat="server" id="DDOwner" Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
            </EditItemTemplate>
        </asp:templatefield>

protected void OnRowDataBoundMS(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList DDL1 = e.Row.FindControl("DDOwnerGroup") as DropDownList;
            DropDownList DDL2 = e.Row.FindControl("DDOwner") as DropDownList;
            Label LBL1 = e.Row.FindControl("LabelDefaultOwnerGroupEdit") as Label;
            Label LBL2 = e.Row.FindControl("LabelDefaultOwnerEdit") as Label;
            DDL1.SelectedValue = Convert.ToString(LBL1.Text);
            DDL2.DataBind();
            DDL2.SelectedValue = Convert.ToString(LBL2.Text);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 2018-06-21
    • 2012-12-10
    • 2011-02-04
    • 2012-05-02
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多