Search code examples
c#.netobjectdatasourceascx

Trying to use a variation on the name of a parent page serving up a .net user control as the SelectMethod for a datasource


I am trying to build a reusable control (ascx) that can be used in multiple aspx pages. I have a datasource in the control which has a SelectMethod. Id like to use the calling page name (minus the extension) as the name of the SelectMethod - which can be looked up elsewhere.

Not sure how I would access this information from the ascx page. Was hoping something like this pseudo-code would work:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="Parent.pagename()" TypeName="BlahBlah"></asp:ObjectDataSource>

Where pagename() is a function in the .ascx.cs file returning the parent aspx page name as a string which can be looked up as the SelectMethod elsewhere in the ObjectContextFacadeManager in the BLL (its a large behemoth application - only half of which I am aware of).

Cheers.


Solution

  • You can get the value you're looking for using the AppRelativeCurrentExecutionFilePath. To output it you may have to use code-behind, though, because using inline-script, such as:

    SelectMethod="<%Request.AppRelativeCurrentExecutionFilePath%>"
    

    Won't work as the attribute value of a control with runat="server" (IIRC).

    So, on load, or some other event, you could set this programmatically:

    Page_Load(object sender, EventArgs e) {
      SelectMethod = System.IO.Path.GetFileNameWithoutExtension(
        Request.AppRelativeCurrentExecutionFilePath);
    }