DataList等控件嵌套绑定的一个需要注意的地方
2008-06-03 20:30
435 查看
前两天刚刚搞定了DataList的嵌套绑定,结果今天用的时候就一直出问题,每次执行都找不到指定的控件,给我一个NULL的异常.(点这里看"DataList嵌套绑定数据")
为什么找不到控件呢,使用的东西都一模一样.研究了好半天,才发现原来是一个ListItemType的问题,上次我使用的时候,就直接用的ItemTemplate,看代码,没有在ItemTemplate之前再有其他的Template
<asp:DataList ID="DataList1" runat="server"
2 Width="126px" onitemdatabound="DataList1_ItemDataBound">
3 <ItemTemplate>
4 <table style="width: 100%;">
5 <tr>
6 <td>
7 <asp:Label ID="Label1" runat="server" Text='<%# Eval("BookClassName") %>'
8 Font-Bold="True" Font-Size="16pt" ForeColor="Red"></asp:Label>
9 </td>
10 </tr>
11 <tr>
12 <td>
13 <asp:DataList ID="DataList2" runat="server" RepeatColumns="2">
14 <ItemTemplate>
15 <asp:Label ID="Label2" runat="server" Text='<%# Eval("BookClassName") %>'></asp:Label>
16 </ItemTemplate>
17 </asp:DataList>
18 </td>
19 </tr>
20 </table>
21 </ItemTemplate>
22 </asp:DataList>
所以就可以直接在代码中使用
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
<asp:DataList ID="dlistOut" runat="server" OnItemDataBound="dlistOut_ItemDataBound">
2 <HeaderTemplate>
3 <asp:Label ID="Label20" runat="server" Text="三、简答题(每题" Font-Bold="True"></asp:Label>
4 <%Response.Write(Session["QuestionScore"].ToString());%>
5 <asp:Label ID="Label21" runat="server" Text="分)" Font-Bold="True"></asp:Label>
6 </HeaderTemplate>
7 <ItemTemplate>
8 <table cellspacing="1" cellpadding="1" width="550" border="0" style="text-align: left">
9 <tr>
10 <td colspan="2">
11 <asp:Label ID="lblQNum" runat="server" Text='<%# Container.ItemIndex+1 %>' Font-Bold="True"></asp:Label>
12 <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("QTitle","、{0}") %>' Width="273px"
13 Font-Bold="False"></asp:Label>
14 <asp:Label ID="lblQID" runat="server" Text='<%# Eval("QID") %>'></asp:Label>
15 </td>
16 </tr>
17 </table>
18 </ItemTemplate>
19 </asp:DataList>
所以要在DataList的绑定事件中加一个判断,判断一下是否是ItemTemplate的绑定事件,重要的就是那个ListItemType
1 protected void dlistOut_ItemDataBound(object sender, DataListItemEventArgs e)
2void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
为什么找不到控件呢,使用的东西都一模一样.研究了好半天,才发现原来是一个ListItemType的问题,上次我使用的时候,就直接用的ItemTemplate,看代码,没有在ItemTemplate之前再有其他的Template
<asp:DataList ID="DataList1" runat="server"
2 Width="126px" onitemdatabound="DataList1_ItemDataBound">
3 <ItemTemplate>
4 <table style="width: 100%;">
5 <tr>
6 <td>
7 <asp:Label ID="Label1" runat="server" Text='<%# Eval("BookClassName") %>'
8 Font-Bold="True" Font-Size="16pt" ForeColor="Red"></asp:Label>
9 </td>
10 </tr>
11 <tr>
12 <td>
13 <asp:DataList ID="DataList2" runat="server" RepeatColumns="2">
14 <ItemTemplate>
15 <asp:Label ID="Label2" runat="server" Text='<%# Eval("BookClassName") %>'></asp:Label>
16 </ItemTemplate>
17 </asp:DataList>
18 </td>
19 </tr>
20 </table>
21 </ItemTemplate>
22 </asp:DataList>
所以就可以直接在代码中使用
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
<asp:DataList ID="dlistOut" runat="server" OnItemDataBound="dlistOut_ItemDataBound">
2 <HeaderTemplate>
3 <asp:Label ID="Label20" runat="server" Text="三、简答题(每题" Font-Bold="True"></asp:Label>
4 <%Response.Write(Session["QuestionScore"].ToString());%>
5 <asp:Label ID="Label21" runat="server" Text="分)" Font-Bold="True"></asp:Label>
6 </HeaderTemplate>
7 <ItemTemplate>
8 <table cellspacing="1" cellpadding="1" width="550" border="0" style="text-align: left">
9 <tr>
10 <td colspan="2">
11 <asp:Label ID="lblQNum" runat="server" Text='<%# Container.ItemIndex+1 %>' Font-Bold="True"></asp:Label>
12 <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("QTitle","、{0}") %>' Width="273px"
13 Font-Bold="False"></asp:Label>
14 <asp:Label ID="lblQID" runat="server" Text='<%# Eval("QID") %>'></asp:Label>
15 </td>
16 </tr>
17 </table>
18 </ItemTemplate>
19 </asp:DataList>
所以要在DataList的绑定事件中加一个判断,判断一下是否是ItemTemplate的绑定事件,重要的就是那个ListItemType
1 protected void dlistOut_ItemDataBound(object sender, DataListItemEventArgs e)
2void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
相关文章推荐
- 关于相对布局中控件摆放位置的一个需要注意的小地方
- 使用 AfxBeginThread 传递一个结构体入线程需要注意的地方
- WPF 数据绑定需要注意的地方
- 使用CustomValidator 控件时需要注意的地方
- fragment 嵌套 fragment 需要注意的地方
- oracle 默认值需要注意的一个地方
- Android多进程需要注意的一个地方
- JQuery 中 next() 方法,需要注意的一个地方
- vc编写全局钩子需要注意的一个地方
- SpriteBuilder修改CCB文件中的子CCB文件需要注意的一个地方
- 当你创建一个新项目时...有很多需要注意的地方哟
- 控件的事件委托需要注意的地方
- qt国际化与本地化中一个需要注意的地方
- 创建一个Spring+SpringMVC+MyBatis+MySql+Maven项目需要注意的地方
- JQUERY中的getJSON方法需要注意的一个地方:单引号与双引号
- Bulk collect的一个需要注意的地方
- 当ListView控件在后台绑定数据源时,用DataPager控件分页需要注意的问题
- 一个项目 接口编写需要注意的地方
- 关于Qt中QMap的一个需要注意的地方
- qt国际化与本地化中一个需要注意的地方