我在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
答案 0 :(得分:1)
如果不查看整个页面生命周期,很难回答,但您可能会尝试将MessageId
存储在控件的ViewState
中,如下所示:
Public Property MessageID As Integer
Get
If (Me.ViewState("VSMessageID") IsNot Nothing) Then
return CType(Me.ViewState("VSMessageID "), Integer)
else
return 0 // arbitrary value
End Get
Set(value As Integer)
Me.ViewState.Add("VSMessageID", value)
End Set
End Property
然后:
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
对不起,如果VB.Net语法错误
答案 1 :(得分:0)
我通过更改usercontrol解决了这个问题:
Partial Class MyUserControl
Inherits System.Web.UI.UserControl
Public Property MessageID As Integer
Get
Return 0
End Get
Set(value As Integer)
Fill(value)
End Set
End Property
Protected Sub Fill(messageID As Integer)
'Some code
End Sub
End Class
正如jbl所说,这是因为页面生命周期。 ASPxDataview的OnPageIndexChange事件或asp.net DataList的自定义PageIndexChange事件,代码工作如下:
这就是为什么没有设置UserControl.Load属性。