简体   繁体   中英

How to pass a URL parameter to a ObjectDataSource SelectMethod?

I have a small example application with articles and comments. Users can view a specific Article by passing the article ID value in URL:

http://localhost:56079/viewArticle.aspx?id=123456

I want the Article id to be used to find corresponding comments and populate a gridview with them in the same form.

viewArticle.aspx:

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataSourceID="ObjectDataSource1">
                <Columns>
                    <dx:GridViewDataTextColumn  FieldName="field1"   Caption="Field #1"      VisibleIndex="0" />
                    <dx:GridViewDataTextColumn  FieldName="field2"   Caption="Field #2" VisibleIndex="1" />
                </Columns>
            </dx:ASPxGridView>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
                SelectMethod="searchComments"
                TypeName="App.CommentManager">
                <SelectParameters>
                </SelectParameters>
            </asp:ObjectDataSource>

If id is "hard-coded" in the searchComments method, gridview is populated with correct entries.

My only problem is passing the article id to searchComments method.

  • I was thinking about a <%# %>" style databinding approach, but it would be extremely dirty, and it still doesn't work.

     SelectMethod="searchComments(<%# Request.QueryString["id"] %>)" 
  • Another approach I tried was setting the selectmethod in codebehind like this:

     ObjectDataSource1.SelectMethod = "searchComments('123456')"; 

    That results in error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'searchComments('123456')' that has no parameters.

Pass a QueryStringParameter directly into SelectParameters - ie:

            <SelectParameters>
                <asp:QueryStringParameter  QueryStringField="id" />
            </SelectParameters>

You can use QueryStringParameter in SelectParameters section

    <SelectParameters>
            <asp:QueryStringParameter QueryStringField="id" Name="id"/>
    </SelectParameters>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM