在我的项目中,有 3 个用户控件;BasicContact、BasicDetail 和 ActionTime。它们在 EditOrder.aspx 页面中。
BasicContact 中有“ReportDate”属性(日期类型),BasicDetail 中有 RadioButtonList“rdl_Priority”(具有“prio_id”字段中的整数值),ActionTime 中有“CheckDate”属性。ReportDate 值派生自 txt_ReportDate 和 txt_ReportTime,而 CheckDate 值派生自 txt_CheckDate 和 txt_CheckTime。
我的目标是在将日期添加到 ReportDate 并单击单选按钮列表后计算 Checkdate。Checkdate 将根据 ReportDate(日期)+ SLAHour(小时,从输入为 prio_id 的“GetSLAHour”方法获取)计算,然后在 txt_CheckDate 和 txt_CheckTime 中设置文本。
现在,我能够使用 Postback 完成这项任务。我创建自定义事件并在 RadioButtonList selectedIndexchanged 方法中引发它。之后,事件在 EditOrder 页面后面的代码中处理。这是我的代码。
BasicDetail - RadioButtonList
<asp:RadioButtonList ID="rdl_Priority" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" />
BasicDetail - codeBehind
Public Event priorityClicked As System.EventHandler
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not IsPostBack Then SetupList()
End Sub
Private Sub SetupList()
Dim ctx As New StsDataContext
With rdl_Priority
Dim Result = (From r In ctx.Priorities Order By r.display_order)
If Result.Count > 0 Then
.DataTextField = "prio_name"
.DataValueField = "prio_id"
.DataSource = Result
.DataBind()
Else
lbl_Priority.Visible = False
rdl_Priority.Visible = False
End If
End With
End Sub
Protected Sub rdl_Priority_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdl_Priority.SelectedIndexChanged
RaiseEvent priorityClicked(sender, e)
End Sub
EditOrder - 代码隐藏
Private Sub BasicDetail_priorityClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles BasicDetail.priorityClicked
Dim reportDate As Date? = BasicContact.ReportDate
Dim SLAHour As Integer? = GetSLAHour(BasicDetail.PriorityId)
If reportDate.HasValue AndAlso SLAHour.HasValue Then
ActionTime.CheckDate = CDate(reportDate).AddHours(CDbl(SLAHour))
End If
End Sub
但是,我不希望刷新页面(没有回发)。我不知道如何从 javascript 调用函数或子函数。我已经尝试过 PageMethod 但它在运行时导致错误,说该方法不受支持。无论如何,如果有比从 javascript 调用代码隐藏更好的方法,请告诉我。
提前致谢