将SqlDataSource数据绑定到GridView时出错

时间:2012-02-16 20:11:37

标签: asp.net data-binding gridview sqldatasource

我收到此错误:

  

数据绑定方法,如Eval(),XPath()和Bind()只能是   在数据绑定控件的上下文中使用。

我正在尝试将SqlDataSource的结果绑定到GridView

这是代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="grid_getbranch" Height="111px" Width="91px">
    <Columns>
        <asp:TemplateField HeaderText="branch" SortExpression="branch">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("branch") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("branch") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="name" SortExpression="name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="grid_getbranch" runat="server" 
    ConnectionString="<%$ ConnectionStrings:svpolyConnectionString %>" 
    SelectCommand="SELECT branch.branch, course.name FROM branch INNER JOIN course ON branch.c_id = course.id WHERE (branch.c_id = @c_id)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList2" Name="c_id" 
            PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

您可以尝试将SelectCommand查询更改为:

SELECT branch.branch AS branch, course.name AS name FROM branch INNER JOIN course ON branch.c_id = course.id WHERE (branch.c_id = @c_id)

我不确定这是否会导致您的错误,但这是我会尝试的第一件事。

答案 1 :(得分:0)

删除绑定到标签字段。

另请参阅此问题How to 'bind' Text property of a label in markup