Nested Repeaters and SqlDataSource Parameters

后端 未结 5 1258
耶瑟儿~
耶瑟儿~ 2020-12-07 02:18

I am using nested repeaters to build a table for reasons I won\'t discuss here, but what I\'m looking to do is have two datasources, one for the top level repeater that will

5条回答
  •  独厮守ぢ
    2020-12-07 02:59

    I think the best way would be to handle the ItemDataBound event of the Outer Repeater, Find the inner DataSource control and set a SelectParameter for it.

        void MyOuterRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
        {
        // Find the Inner DataSource control in this Row.
        SqlDataSource s = (SqlDataSource)e.Item.FindControl("InnerDataSource");
    
        // Set the SelectParameter for this DataSource control
        // by re-evaluating the field that is to be passed.
        s.SelectParameters["MyParam"].DefaultValue = DataBinder.Eval(e.Item.DataItem, "MyFieldValueToPass").ToString();
        }
    

    For an example using the DataList, check out the ASP.NET quickstarts here

    P.S.: Please see Tony's reply below for an important correction to the above presented snippet. Notably, it would be essential to check the ItemType of the current RepeaterItem. Alternatively, it's an excellent practice to always check for nulls on every object.

提交回复
热议问题