简体   繁体   English

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

[英]Perform code-behind Function/Sub from javascript (VB.NET)

In my project, there are 3 user controls; 在我的项目中,有3个用户控件。 BasicContact, BasicDetail and ActionTime. BasicContact,BasicDetail和ActionTime。 They are in EditOrder.aspx page. 它们在EditOrder.aspx页中。

There is "ReportDate" property (Date type) in BasicContact, RadioButtonList "rdl_Priority" (with integer value from "prio_id" field) in BasicDetail and "CheckDate" property in ActionTime. BasicContact中具有“ ReportDate”属性(日期类型),BasicDetail中具有RadioButtonList“ rdl_Priority”(具有来自“ prio_id”字段的整数值),而ActionTime中具有“ CheckDate”属性。 The ReportDate value is derived from txt_ReportDate and txt_ReportTime and CheckDate value is derived from txt_CheckDate and txt_CheckTime. ReportDate值派生自txt_ReportDate和txt_ReportTime,而CheckDate值派生自txt_CheckDate和txt_CheckTime。

My objective is to calculate Checkdate after I add date into ReportDate and click on radiobuttonlist. 我的目标是在将日期添加到ReportDate并单击单选按钮列表后计算Checkdate。 Checkdate will be calculated from ReportDate (date) + SLAHour (hour, get from "GetSLAHour" method where input is prio_id) and then set text in txt_CheckDate and txt_CheckTime. Checkdate将从ReportDate(日期)+ SLAHour(小时,从输入为prio_id的“ GetSLAHour”方法获得)中计算得出,然后在txt_CheckDate和txt_CheckTime中设置文本。

Right now, I was be able to complete this task using Postback. 现在,我能够使用回发功能完成此任务。 I create custom event and raise it in RadioButtonList selectedIndexchanged method. 我创建自定义事件,并在RadioButtonList selectedIndexchanged方法中引发它。 After that, event is handled in code behind of EditOrder page. 之后,事件在EditOrder页面后面的代码中处理。 Here are my code. 这是我的代码。

BasicDetail - RadioButtonList 基本详细信息-RadioButtonList

<asp:RadioButtonList ID="rdl_Priority" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" />

BasicDetail - codeBehind BasicDetail-代码背后

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 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

However, I don't want the page to be refreshed (no postback). 但是,我不希望刷新页面(不回发)。 I don't know how to call the function or sub from javascript. 我不知道如何从javascript调用函数或子函数。 I have tried PageMethod but it cause error in runtime saying that the method is not supported. 我已经尝试过PageMethod,但是它在运行时导致错误,表明该方法不受支持。 Anyway, if there is a better way than calling code-behind from javascript, please let me know. 无论如何,如果有比从javascript调用代码隐藏更好的方法,请告诉我。

Thanks in advance 提前致谢

Ok Sorry, here is my solution 好的,抱歉,这是我的解决方案

Since for now I used PageMethod to solve this problem, I don't need raise event function from code behind anymore so I deleted all the codes I have posted to ask my own question. 从现在开始,我使用PageMethod解决了此问题,所以不再需要后面的代码引发事件功能,因此我删除了所有发布的代码来问自己的问题。

First, I added javascript function to each item in RadioButtonList in BasicDetail code behind 首先,我在BasicDetail代码后面的RadioButtonList中的每个项目中添加了javascript函数

BasicDetail - Code behind 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

This "CalCheckDate" function added to each button is implemented in EditOrder page (user control's parent page) 在EditOrder页面(用户控件的父页面)中实现了此添加到每个按钮的“ CalCheckDate”功能。

EditOrder.aspx 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>

Child control within user control, such as txt_ReportDate in BasicContact, can be derived by creating property in BasicContact as follows: 用户控件中的子控件(例如BasicContact中的txt_ReportDate)可以通过在BasicContact中创建属性来派生,如下所示:

BasicContact - Code Behind BasicContact-背后的代码

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

This property is used in "CalCheckDate" function to get its value and pass it to PageMethod function. 在“ CalCheckDate”函数中使用此属性,以获取其值并将其传递给PageMethod函数。 Other controls can be derived in the same way. 其他控件可以以相同的方式派生。

Last step is coding PageMethods function, "GetCheckDateTime", in EditOrder code behind 最后一步是在后面的EditOrder代码中编码PageMethods函数“ GetCheckDateTime”

EditOrder - Code Behind 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

The result is returned to "OnGetCheckDateComplete" function in javascript (or "OnGetCheckDateError" if there is an exception). 结果返回到javascript中的“ OnGetCheckDateComplete”函数(如果有异常,则返回“ OnGetCheckDateError”)。 Here, I split the String and use its value to set text in textbox. 在这里,我拆分了String并使用其值在文本框中设置文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM