gpt4 book ai didi

asp.net - DataList 内的用户控件

转载 作者:行者123 更新时间:2023-12-02 15:14:08 27 4
gpt4 key购买 nike

我在DataList中有一个UserControl,我根据DataList数据的PrimeryKey填充用户控件内的控件。第一次加载程序的行为符合预期,但当用户更改页面索引时,所有 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

最佳答案

如果不查看整个页面生命周期就很难回答,但您可以尝试将 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

参见http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

抱歉,VB.Net 语法错误

关于asp.net - DataList 内的用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17024902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com