有关DatasourceId和DataSource的查询

时间:2012-07-04 09:58:30

标签: c# visual-studio-2010 linq-to-sql dynamic-data

有两张表

EmpInf
    EmpId,
    EmpName,
    Salary,
    DepartNumber.

Dept
    DeptNo,
    Deptname,

我有一个linqdatasource control,例如

<asp:LinqDataSource ID="LinqDataSource1" runat="server" EntityTypeName="" 
    ContextTypeName="FilterControl.DataClasses1DataContext" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" OrderBy="DeptName"  
    TableName="Dept1s" Select="new (DeptNo, DeptName)">
</asp:LinqDataSource>

listview

<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1"
   style="z-index: 1; left: 10px; top: 34px; position: absolute; height: 63px; width: 293px" 
    >
    <AlternatingItemTemplate>
        <tr style="">
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="ReadOnly" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="ReadOnly" />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Cancel" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="Edit" />
            </td>
        </tr>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" style="">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" 
                    ValidationGroup="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="Insert" 
                    ValidationGroup="Insert" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="Insert" 
                    ValidationGroup="Insert" />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="">
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="ReadOnly" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="ReadOnly" />
            </td>
        </tr>
    </ItemTemplate>

并尝试实施此listview动态数据并使用dynamic data manager control

<asp:DynamicDataManager ID="DynamicDataManager1" runat="server" >
<DataControls>
<asp:DataControlReference ControlID="ListView1" />
</DataControls>
</asp:DynamicDataManager>

我已经使用dropdownlist,它绑定到EmpInf.Empname,当我从下拉列表中选择部分员工姓名时,我想绑定部门名称。

在后端

FilterControl.DataClasses1DataContext obj = new DataClasses1DataContext();
        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {



            var a = from r in obj.EmpInfs
                    join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
                    where r.EmpName ==  DropDownList1.SelectedValue  
                    select s.DeptName;



            ListView1.DataSource = a;
            ListView1.DataBind();



        }

在运行时期间,它会抛出一个异常,它会显示使用DataSourceID="LinqDataSource1" or use the datasource a the back end. remove one of them.,同时在此过程中不会返回任何内容。我该如何进行这项运动,需要建议。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

解决这个问题的一种方法是从ListView控件中删除DataSourceID并在page_load中绑定数据

protected void Page_Load(object sender, EventArgs e)
{

   if(!Page.IsPostBack)
   {
      //Bind datasource to ListView here
   }
}

现在一切都应该有效

答案 1 :(得分:0)

FilterControl.DataClasses1DataContext obj = new DataClasses1DataContext();         protected void DropDownList1_SelectedIndexChanged1(object sender,EventArgs e)         {

        var a = from r in obj.EmpInfs
                join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
                where r.EmpName ==  DropDownList1.SelectedValue  
                select s;


        listview1.DatasourceId=null;
        ListView1.DataSource = a;
        ListView1.DataBind();



    }

也可以通过在上面的代码

中编写listview.DatasourceId=null来解决