将方法链接到数据源

时间:2013-02-21 12:05:03

标签: asp.net sqldatasource

所以ATM我的代码工作正常。我使用SqlDataSource链接到网格数据源并显示一个表。它显示表中的列,并用作超链接以导航到其他页面。

         

<asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="false" /> 

    <asp:TemplateField>
        <ItemTemplate>
            <asp:HyperLink ID="LoadSubContractorDetails" runat="server" Text="Show Details"/>
         </ItemTemplate>
    </asp:TemplateField>
    </Columns>
     <EmptyDataTemplate>
           There are currently no items in this table.
     </EmptyDataTemplate>

</asp:GridView>
 <asp:SqlDataSource ID="GridDataSource1" runat="server"   
      ConnectionString="<%$ConnectionStrings:ClarkesTest4FromMaster1ConnectionString %>"  
     SelectCommand="SELECT id, [Company Name] FROM [Sub Contractor] ORDER BY [Company Name]" >
</asp:SqlDataSource>  

 protected void passSubContractorInfoToNewPage(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView view = (DataRowView)e.Row.DataItem;
        HyperLink LoadSubContractorDetails = (HyperLink)e.Row.FindControl("LoadSubContractorDetails");
        LoadSubContractorDetails.NavigateUrl = ResolveUrl(@"~/SubContractDetails.aspx?id=" + view["id"].ToString() + "&InvoiceId=" + this.CurrentInvoiceId.ToString());
    }
}

然而,正如我所说,代码工作正常,但它显示数据库表中的所有记录我只想显示从我拥有的另一个函数返回的子协议:

 LoadSubContractors();

我该怎么做? 请指教? 感谢

1 个答案:

答案 0 :(得分:1)

如果LoadSubContractors()函数的返回类型类似于ListDataSet,则只需在代码中设置数据源,然后手动绑定数据:

myGridView.DataSource = LoadSubContractors();
myGridView.DataBind()