Search code examples
asp.netsqldatasource

Link a method to a datsource


So ATM my code is working fine. I use a SqlDataSource to link to a grid data source and display a table. It displays a column from the table and is used as a hyperlink to navigate to a different page.

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

However as I said the code works fine, but it displays all the records in the DB Table I only want to display the subcontractos that are returned from another function I have:

 LoadSubContractors();

How do i do this? please advise? thanks


Solution

  • If the return type of your LoadSubContractors() function is something like a List or a DataSet you can simply set the datasource in code behind and then bind the data manually:

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