我在DataList中有一个UserControl,我根据DataList数据的PrimeryKey填充了usercontrol里面的控件。最初加载程序按预期运行,但当用户更改页面索引时,所有 MessageID 都返回 0,很可能与 <%# Eval("id").toString()%> 一起出现的值丢失。我不想硬编码用户控件,因为我在项目的很多地方都使用了它,它使代码可维护。我该如何纠正这种行为?提前致谢。
我的代码如下:
<asp:DataList ID="DataList1" runat="server" Width="658px">
<ItemTemplate>
<table style="width: 100%;">
<tr>
<td style="font-weight: 700; color: #FF0000; font-size: medium; font-size: 10pt; font-family: Tahoma">
<%# Eval("hadding") +" ["+Eval("startTime")+" - "+Eval("endTime")+"]"%>
</td>
</tr>
<tr>
<td style="font-weight: 700; font-size: 10pt; font-family: Tahoma">
<%# Eval("location") %>
</td>
</tr>
<tr>
<td>
<uc1:MyUserControl ID="MyUserControl1" runat="server" MessageID='<%# Eval("id").toString()%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<table>
<tr>
<td>
<asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"
Width="43px" OnClick="btnfirst_Click" /></td>
<td>
<asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"
Width="43px" OnClick="btnprevious_Click" /></td>
<td>
<asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"
Width="43px" OnClick="btnnext_Click" /></td>
<td>
<asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"
Width="43px" OnClick="btnlast_Click" /></td>
</tr>
</table>
和我的用户控制:
Partial Class MyUserControl
Inherits System.Web.UI.UserControl
Private _messageID As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (_messageID > 0) Then
Fill(_messageID)
End If
End Sub
Public Property MessageID As Integer
Get
Return _messageID
End Get
Set(value As Integer)
_messageID = value
End Set
End Property
Protected Sub Fill(messageID As Integer)
'Some code
End Sub
End Class