从javascript(VB.NET)执行代码隐藏功能/子

时间:2012-06-11 07:11:29

标签: javascript asp.net vb.net radiobuttonlist raiseevent

在我的项目中,有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,然后单击radiobuttonlist。 Checkdate将从ReportDate(日期)+ SLAHour(小时,来自“GetSLAHour”方法,其中输入为prio_id)计算,然后在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 - codeBehind

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

提前致谢

1 个答案:

答案 0 :(得分: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页面(用户控件的父页面)中实现

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函数。其他控件可以以相同的方式派生。

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

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”)。在这里,我拆分String并使用其值在文本框中设置文本。