0

在我的项目中,有 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 调用代码隐藏更好的方法,请告诉我。

提前致谢

4

1 回答 1

0

好的对不起,这是我的解决方案

因为现在我使用 PageMethod 来解决这个问题,我不再需要从后面的代码中引发事件函数,所以我删除了我发布的所有代码来问我自己的问题。

首先,我在后面的 BasicDetail 代码中为 RadioButtonList 中的每个项目添加了 javascript 函数

BasicDetail - 后面的代码

Private Sub rdl_Priority_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdl_Priority.DataBound
    For Each li As ListItem In rdl_Priority.Items
        Dim slaHour As Integer? = GetSLAHour(li.Value)
        li.Attributes.Add("onclick", "return CalCheckDate(" & If(slaHour.HasValue, CStr(slaHour), "null") & ");")
    Next
End Sub

这个添加到每个按钮的“CalCheckDate”功能在EditOrder页面(用户控件的父页面)中实现

编辑订单.aspx

<script type="text/javascript">
    function CalCheckDate(hour) {
        var hid_ServId = document.getElementById('<%=hid_ServId.ClientID%>');
        var txt_reportDate = document.getElementById('<%=BasicContact.ReportDateTextName%>');
        var txt_reportTime = document.getElementById('<%=BasicContact.ReportTimeTextName%>');
        PageMethods.GetCheckDateTime(hid_ServId.value, txt_reportDate.value, txt_reportTime.value, hour, OnGetCheckDateComplete, OnGetCheckDateError);
    }
    function OnGetCheckDateComplete(result) {
        var txt_checkDate = document.getElementById('<%=ActionTime.CheckDateTextName%>');
        var txt_checkTime = document.getElementById('<%=ActionTime.CheckTimeTextName%>');
        var chkDateTime = result.split(" ");
        txt_checkDate.value = chkDateTime[0];
        txt_checkTime.value = chkDateTime[1];
    }
    function OnGetCheckDateError(e)
    {
        alert(e._message);
    }
</script>

用户控件中的子控件,例如BasicContact中的txt_ReportDate,可以通过在BasicContact中创建属性来派生,如下:

BasicContact - 代码背后

Public ReadOnly Property ReportDateTextName() As String
    Get
        Return txt_ReportDate.ClientID
    End Get
End Property

此属性在“CalCheckDate”函数中用于获取其值并将其传递给 PageMethod 函数。其他控件可以以相同的方式派生。

最后一步是在后面的 EditOrder 代码中编码 PageMethods 函数“GetCheckDateTime”

EditOrder - 代码隐藏

<System.Web.Services.WebMethod()> _
Public Shared Function GetCheckDateTime(ByVal servId As String, ByVal ReportDate As String, ByVal ReportTime As String, ByVal hour As String) As String
    Dim checkDate As String, checkTime As String
    '************************************************************************
    'Calculate check and time date from input ReportDate, ReportTime and hour
    '************************************************************************
    Return checkDate & " " & checkTime
End Function

结果返回到 javascript 中的“OnGetCheckDateComplete”函数(如果有异常,则返回“OnGetCheckDateError”)。在这里,我拆分字符串并使用它的值在文本框中设置文本。

于 2012-06-14T03:26:54.343 回答