3

So far this is what I have.

protected void CategoryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Object dataItem = e.Item.DataItem;

        // code to pull catid out of dataItem
    }

Here's an example of what dataItem contains after running in debug:

dataItem = { TextCategory = "Radiation (Release or Contamination)", catid = 4, TextResult = "Refer to emergency plan and report under code 332.54(A)" , IsReportable = True }

I want to pull the catid out of the object. I have tried casting it, but all attempts to cast it have been unsuccessful. My goal is to use this catid for a nested repeater's databind.. something like this:

using (NERAEntities entities = new NERAEntities())
{
    var Questions = (from x in entities.Questions
                   where x.CategoryID == catid
                   select new { QuestionText = x.Text }); 


    QARepeater.DataSource = Questions;
    QARepeater.DataBind();
}
4

3 回答 3

4

问题是 DataItem 本身并没有真正的类型(它是一个匿名类)。您需要改用它:

dynamic dataItem = e.Item.DataItem ;
int catId = dataItem.catid ;

显然,如果实际对象没有catId属性,这将在运行时失败。

于 2013-08-08T15:00:17.710 回答
0

假设您使用的对象的类型称为 DataItem,请尝试以下操作:

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataItem dataItem = (DataItem) e.Item.DataItem;

        // code to pull catid out of dataItem
    }
于 2013-08-08T14:57:47.020 回答
0

也许你可以尝试:

protected void CategoryRepeater_ItemDataBound(object sender, 
    RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
        int? catID = row["catid"] as int;
    }
}
于 2013-08-08T15:14:16.077 回答