gpt4 book ai didi

asp.net - 难倒用户控件上的自定义属性

转载 作者:行者123 更新时间:2023-12-04 06:03:26 24 4
gpt4 key购买 nike

我创建了一个 ASP.NET 用户控件。当我明确提供自定义属性的值时,该值将传递给控件。但是,当我尝试使用数据源中的值时,该值不会传递给控件。

用户控件位于 FormView 中。 FormView 成功显示基础记录。我试图将 FormView 数据源中列中的值传递给控件。这有效

<asp:formview .... />
<editTemplate>
<uctrl:DateSelector ID="DateSelector1" runat="server" DateValue="5/30/2011" /><br />
<%#Eval("MilestoneDate")%>
</editTemplate>
</asp:formview>

这不...
<asp:formview .... />
<editTemplate>
<uctrl:DateSelector ID="DateSelector1" runat="server" DateValue='<%#Eval("MilestoneDate")%>' /><br />
<%#Eval("MilestoneDate")%>
</editTemplate>
</asp:formview>

我已经确认 MileStoneDate 确实有它的值(value)。无论出于何种原因,该值并未传递给控件,​​但控件确实识别出文字值。
Imports Microsoft.VisualBasic 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.ComponentModel

Partial Class content_WebUserControl
Inherits System.Web.UI.UserControl
<Bindable(True, BindingDirection.OneWay)>
Public Property DateValue() As String
Get
Return _DateValue
End Get
Set(ByVal value As String)
_DateValue = value
End Set
End Property
Private _DateValue As String
Protected Sub Page_Load() Handles Me.Load

If IsDate(Me.DateValue) Then
Dim NewDate As DateTime = CDate(Me.DateValue)
LabelSelectedDateDisplay.Text = Me.DateValue
LabelSelectedDateDisplay_DayOfWeek.Text = NewDate.ToString("dddd")
TextBoxSelectedDate.Text = Me.DateValue
Else
LabelSelectedDateDisplay.Text = ""
LabelSelectedDateDisplay_DayOfWeek.Text = ""
TextBoxSelectedDate.Text = ""
End If

End Sub


End Class

我被引用到这个 URL 上的帖子 ASP.NET User Control : can't initialize a user control property using Eval("...")

但是,根据我对这篇文章的理解,我所拥有的应该是有效的。

最佳答案

您需要使用 ViewState 在页面请求之间保留 DateValue。

Public Property DateValue() As String 
Get
IF IsNothing(ViewState("datevalue") Then
return String.Empty
End If
return ViewState("datevalue").ToString()
End Get
Set(ByVal value As String)
ViewState("datevalue")=value
End Set
End Property

或将值直接存储到控件:
 Public Property DateValue() As String 
Get
Return LabelSelectedDateDisplay.Text
End Get
Set(ByVal value As String)
If IsDate(value) Then
Dim NewDate As DateTime = CDate(value)
LabelSelectedDateDisplay.Text = NewDate
LabelSelectedDateDisplay_DayOfWeek.Text = NewDate.ToString("dddd")
TextBoxSelectedDate.Text = NewDate
Else
LabelSelectedDateDisplay.Text = ""
LabelSelectedDateDisplay_DayOfWeek.Text = ""
TextBoxSelectedDate.Text = ""
End If
End Set
End Property

关于asp.net - 难倒用户控件上的自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8668711/

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