3

我在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
4

2 回答 2

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

请参阅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 语法不好

于 2013-06-10T14:06:10.480 回答
0

我通过改变用户控件解决了这个问题:

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 事件,代码工作如下:

  1. DataList(或 ASPxDataview)加载
  2. UserControl 属性设置
  3. 用户控件加载
  4. PageIndexChange 事件
  5. 用户控件加载
  6. UserControl 属性设置

这就是未设置 on UserControl.Load 属性的原因。

于 2013-06-10T15:02:37.167 回答