2 Gridview中的Dropdownlist导致错误:数据绑定方法(如Bind())只能用于数据绑定控件的上下文中 [英] 2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control

查看:217
本文介绍了2 Gridview中的Dropdownlist导致错误:数据绑定方法(如Bind())只能用于数据绑定控件的上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在编辑模板中有2个DropDownLists(DDL1,DDL2)一个GridView的绑定到一个SQLDataSource。 DDL2依赖于DDL1的选定值。

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

< asp:GridView runat =serverID =Gridview1
DataSourceID =里程碑
DataKeyNames =ID
OnRowEditing = OnRowEditing
OnRowDataBound =OnRowDataBoundMS
OnSelectedIndexChanged =OnSelectedIndexChangedMS>
....
< asp:templatefield HeaderText =默认所有者组ItemStyle-Horizo​​ntalAlign =center>
< ItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwnerGroup)%>'id =LabelDefaultOwnerGroupFont-Size =12pxWidth =50px/> ;
< / ItemTemplate>
< EditItemTemplate>
< asp:SqlDataSource runat =serverid =OwnerGroupsProviderName =System.Data.SqlClient
ConnectionString =Data Source = XXXXX
SelectCommand =select function, 2 as ord
from Staff
其中函数不为空
按函数分组
union all
选择'select'as function,0 as ord
union所有
选择'-----'作为函数,1作为ord
按ord排序,函数>
< / asp:SqlDataSource>
< asp:DropDownList runat =serverid =DDOwnerGroupText ='<%#Bind(DefaultOwnerGroup)%>'DataSourceID =OwnerGroupsDataTextField =functionDataValueField =function Font-Size =12pxWidth =80pxAutoPostBack =true/>
< / EditItemTemplate>
< / asp:templatefield>

< asp:templatefield HeaderText =默认所有者ItemStyle-Horizo​​ntalAlign =center>
< ItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwner)%>'id =LabelDefaultOwnerFont-Size =12pxWidth =50px/> ;
< / ItemTemplate>
< EditItemTemplate>
ConnectionString =Data Source = XXXXX
SelectCommand =select Name, UserID
from Staff
其中function = @OwnerGroup
union all
选择Null作为Name,Null作为UserID
按名称排序>
< SelectParameters>
< asp:ControlParameter ControlID =DDOwnerGroupPropertyName =SelectedValueName =OwnerGroupType =String/>
< / SelectParameters>
< / asp:SqlDataSource>
< asp:DropDownList runat =serverid =DDOwnerText ='<%#Bind(DefaultOwner)%>'Width =100pxDataSourceID =OwnerDataTextField =Name DataValueField =UserIDFont-Size =12px/>
< / EditItemTemplate>
< / asp:templatefield>


解决方案

我在OnRowDataBound事件中绑定DropdownLists,如下所示:

 < asp:templatefield HeaderText =Default Owner GroupItemStyle-Horizo​​ntalAlign =center> 
< ItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwnerGroup)%>'id =LabelDefaultOwnerGroupFont-Size =12pxWidth =50px/> ;
< / ItemTemplate>
< EditItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwnerGroup)%>'id =LabelDefaultOwnerGroupEditVisible =false/>
< asp:SqlDataSource runat =serverid =OwnerGroupsProviderName =System.Data.SqlClient
ConnectionString =Data Source = XXXXX
SelectCommand =... >
< / asp:SqlDataSource>
< asp:DropDownList runat =serverid =DDOwnerGroupDataSourceID =OwnerGroupsDataTextField =funcionDataValueField =funcionFont-Size =12pxWidth =80pxAutoPostBack =true />
< / EditItemTemplate>
< / asp:templatefield>

< asp:templatefield HeaderText =默认所有者ItemStyle-Horizo​​ntalAlign =center>
< ItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwner)%>'id =LabelDefaultOwnerFont-Size =12pxWidth =50px/> ;
< / ItemTemplate>
< EditItemTemplate>
< asp:Label runat =serverText ='<%#Bind(DefaultOwner)%>'id =LabelDefaultOwnerEditVisible =false/>
< asp:SqlDataSource runat =serverid =OwnerProviderName =System.Data.SqlClient
ConnectionString =Data Source = XXXXX
SelectCommand =... 。
< SelectParameters>
< asp:ControlParameter ControlID =DDOwnerGroupPropertyName =SelectedValueName =OwnerGroupType =String/>
< / SelectParameters>
< / asp:SqlDataSource>
< / EditItemTemplate>
< / asp:templatefield>

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


Though this seems to be discussed before, I can't apply given help in my case:

I have 2 DropDownLists (DDL1, DDL2) inside the Edit Template of a Gridview, which is bound to a SQLDataSource. DDL2 depends on the selected value of DDL1.

If I place the SQLDataSources that populate the DropDownLists on the top level, the ControlID of DDL2's ControlParameter isn't found even though I address it correctly (Gridview$DDL1ControlID). Therefore I placed the SQLDataSources inside the Edit Template of the Gridview. Now both DropDownLists are populated correctly if I enter in Edit Mode for a given Record; if I change the Index for DDL1, DDL2 is not updated automatically, as DDL2 has AutoPostBack set to false. Once I set AutoPostBack to true, the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the " is thrown.

    <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>

解决方案

I bind the DropdownLists in the OnRowDataBound Event like this:

            <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);
        }
    }
}

这篇关于2 Gridview中的Dropdownlist导致错误:数据绑定方法(如Bind())只能用于数据绑定控件的上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆