日期:2014-05-18  浏览次数:20459 次

关于datagrid的问题,急救!!!!!!!
我在datagrid里面写如下代码显示数据:
<ItemTemplate>
<asp:DropDownList   id= "DropDownList2 "   runat= "server ">
<asp:ListItem> <%#DataBinder.Eval(Container.DataItem, "stuanswer ")%> </asp:ListItem>
</asp:DropDownList>
</Itemtemplate>
但就是读不出来,急,汗呀!

帮帮忙,为什么读不出来????

分析器错误信息:   此上下文中不支持代码块

该如何修改才可以读出来?

------解决方案--------------------
你这里既然是 <ItemTemplate>

绑定当然是
If e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingItem Then
...

------解决方案--------------------
老兄你用的是VB.NET呀,有点看不明白你的后台代码.


<asp:DropDownList id= "DropDownList2 " runat= "server ">
<asp:ListItem> <%#DataBinder.Eval(Container.DataItem, "stuanswer ")%> </asp:ListItem>
</asp:DropDownList>
放在DataGrid 的外面,

在后台写

DropDownList2.DataSource=dr;
DropDownList2.DataTextField= "stuanswer ";
DropDownList2.DataValueField= "stuanswer ";
DropDownList2.DataBind();
这是C#的你可以参考一下.
------解决方案--------------------
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim ddll As DropDownList = CType(e.Item.FindControl( "DropDownList2 "), DropDownList)
ddll.DataSource = Dtemp.Tables(PageId)
ddll.DataTextField = "stuanswer " 'name
ddll.DataValueField = "Id " 'id
ddll.DataBind()
ddll.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "stuanswer "))).Selected = True
End If
End Sub

-------------
方向对了,但是,

If e.Item.ItemType = ListItemType.EditItem Then

> > > >

If e.Item.ItemType = ListItemType.Item OR e.Item.ItemType = ListItemType.AlternatingItem OR e.Item.ItemType = ListItemType.EditItem