Use selection property instead of "SelectMethod" in ObjectDataSource?

I use several ObjectDataSources to populate ComboBox fields in a FormView. FormView is kind of generic because its appearance differs depending on its category.

The category is defined in the URL of the web page. I would like to create a class that filters a category and exposes several properties that can be used to populate ComboBox fields.

The problem is that the ObjectDataSource object by default received the SelectMethod property for retrieving data. With this class that I would like to create, these would not be methods, but properties that will contain data.

Perhaps it is somehow possible to assign the property "SelectMethod" (or similar)? Is it better to use a different approach?

Thank.

+3
source share
2 answers

Maybe I missed something. But if you need to assign the property as SelectMethod, you must set it as get_{Property Name}.

+8
source

If you are trying to assign a dynamic SelectMethod method, you can do this:

// You say that the category comes from Url, so in the Page_Load method

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // Get your QueryString variable
            if (Request["YourVariable"] != null)
            {
             string yourVariable = Request["YourVariable"].ToString();

              if (yourVariable == "CategoryX") {

                   ObjectDataSource1.SelectMethod = "SelectMethodFromCategoryX";

                   // and if you need to set SelectParameters to your ObjectDataSource
                   ObjectDataSource1.SelectParameters["pYourParameterNameForCategoryX"].DefaultValue = this.txtTest.Text;
              }

            }
        }
    }
0
source

Source: https://habr.com/ru/post/1731352/


All Articles