3

这是场景。

我有一个包含 asp:PlaceHolder 的简单页面。

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="TrainingPlan.aspx.vb" Inherits="TrainingPlan" %>
<%@ Reference Control="ctlTask.ascx" %>
<%@ Reference Control="ctlTaskheader.ascx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

    <div class="centered">
        <h2><asp:Label ID="lblPlanTitle" runat="server" Text="Plan Title"></asp:Label></h2>
        <hr />
        <br />
        <asp:ImageButton ID="imgbtnSave" runat="server" ImageUrl="~/Images/save.ico" />
        <br />
        <asp:PlaceHolder ID="PlanPlaceHolder" runat="server"></asp:PlaceHolder>
    </div>
</asp:Content>

此页面上的占位符填充了几行相同的 Web 用户控件。此 Web 用户控件包含多个文本框。对于此 Web 用户控件中的每个文本框,我都有公共属性来设置和获取文本值。在 Web 用户控件的页面加载事件中,我将 onClick 属性添加到其中一些文本框中,就像这样。

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    txtTrainingStart.Attributes.Add("onClick", "txtTrainingStart_Click(" &  txtTrainingStart.ClientID & ", " & txtTask.ClientID & ");")
    txtTraineeBadgeNum.Attributes.Add("onClick", "txtTraineeBadgeNum_Click(" & txtTraineeBadgeNum.ClientID & ", " & txtTask.ClientID & ", " & txtTrainingStart.ClientID & ");")
    txtTrainerBadgeNum.Attributes.Add("onClick", "txtTrainerBadgeNum_Click(" & txtTrainerBadgeNum.ClientID & ", " & txtTrainingComplete.ClientID & ", " & txtTask.ClientID & ", " & txtTraineeBadgeNum.ClientID & ", " & Session.Item("isTrainer").ToString.ToLower & ");")
    txtDecertifyingOfficial.Attributes.Add("onClick", "txtDecertifyingOfficial_Click(" & txtDecertifyingOfficial.ClientID & ", " & txtTrainerBadgeNum.ClientID & ", " & txtTask.ClientID & ", " & Session.Item("isDecertifyingOfficial").ToString.ToLower & ");")
End Sub

对于每一个 onClick 事件,我都有相应的 javascript 函数。

<script type="text/javascript">
    function txtTrainingStart_Click(txtTrainingStart, txtTask) {
        //processing and updates to textboxes here
    }
</script>

这是问题所在。

在包含占位符的主页上,我有一个保存按钮。在保存按钮的单击事件中,我循环遍历占位符中包含的每个 Web 用户控件以处理和保存数据。

Protected Sub imgbtnSave_Click(sender As Object, e As ImageClickEventArgs) Handles imgbtnSave.Click
    For Each item As Control In PlanPlaceHolder.Controls
        Dim task As ctlTask = TryCast(item, ctlTask)

        If Not IsNothing(task) Then
            'need updated textbox values here. 
            'The following line gets the original textbox value, not the updated value that I need
            Dim test As String = task.trainingStart

        End If
    Next
End Sub

我尝试过的所有操作都只能获取页面加载时文本框中的原始值。我认为这应该很简单,我只是缺少一些基本的东西。我已经在谷歌上下搜索了解决方案,但我还没有找到一个。这是我发现的最接近的东西 -> Set Text property of asp:label in Javascript PROPER way。尽管该帖子处理的是标签而不是文本框,并且没有使用包含多个 Web 用户控件的占位符。据我了解,我需要将更新从客户端发布回服务器,但我不知道该怎么做。我已经尝试过使用该Request.Form属性,但我似乎无法做到这一点。我错过了什么?

谢谢你的帮助,瑞兰

编辑
这是根据请求使用 Web 用户控件填充占位符的代码

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim DBConn As MySqlConnection
    Dim cmd As New MySqlCommand

    DBConn = New MySqlConnection(Globals.mysqlConStr)

    Try
        lblPlanTitle.Text = Request.QueryString("plan_name") & " Training Plan"

        If Session.Item("empID") = -1 Then
            empID = clsUser.getID
        Else
            empID = Session.Item("empID")
        End If

        Dim strSQL As String = "SELECT * FROM task_revs tr WHERE tr.change != 'Deleted' and tr.id_plan = " & Request.QueryString("id_plan") & " ORDER BY task_num, rev_date desc"
        Dim da As New MySqlDataAdapter(strSQL, DBConn)
        Dim dtAllRevs As New DataTable
        da.Fill(dtAllRevs)

        Dim dtCurrentRev As New DataTable
        Dim lastTaskNum As String = ""
        dtCurrentRev = dtAllRevs.Clone
        For Each row As DataRow In dtAllRevs.Rows
            If lastTaskNum <> row("task_num") Then
                dtCurrentRev.ImportRow(row)
                lastTaskNum = row("task_num")
            End If
        Next

        strSQL = "SELECT * FROM checkoffs WHERE emp_id = " & empID
        da = New MySqlDataAdapter(strSQL, DBConn)
        Dim dtCheckoffs As New DataTable
        da.Fill(dtCheckoffs)
        Dim addColor As Boolean = True
        Dim tabWidth As Integer

        Dim Header As ctlTaskHeader = LoadControl("ctlTaskHeader.ascx")
        PlanPlaceHolder.Controls.Add(Header)

        For Each row As DataRow In dtCurrentRev.Rows
            Dim newRow As ctlTask = LoadControl("ctlTask.ascx")
            tabWidth = 0

            newRow.id_Task = row("id_Task")
            newRow.taskNum = row("task_num")
            newRow.task = row("task")

            If row("is_header") = True Then
                If row("task_num").ToString.EndsWith(".0") And row("task_num").ToString.Split(".").Count = 2 Then
                    newRow.taskBold = True
                    newRow.taskItalic = True
                Else
                    newRow.taskForeColor = Drawing.Color.Blue
                    newRow.taskItalic = True
                    tabWidth += 10
                End If
            Else
                tabWidth += 10
            End If

            For i As Integer = 0 To row("task_num").ToString.Split(".").Count - 3
                tabWidth += 10
            Next
            newRow.TabSize = tabWidth

            If Not IsDBNull(row("task_level")) Then
                For i As Integer = 0 To row("task_level") - 1
                    newRow.taskLevel = newRow.taskLevel & "*"
                Next
            End If

            If addColor = True Then
                newRow.taskNumBackColor = Drawing.Color.LightGray
                newRow.taskLevelBackColor = Drawing.Color.LightGray
                newRow.taskBackColor = Drawing.Color.LightGray
                newRow.trainingStartBackColor = Drawing.Color.LightGray
                newRow.trainingCompleteBackColor = Drawing.Color.LightGray
                newRow.traineeBadgeNumBackColor = Drawing.Color.LightGray
                newRow.trainerBadgeNumBackColor = Drawing.Color.LightGray
                newRow.decertifyingOfficialBackColor = Drawing.Color.LightGray
            End If
            addColor = Not addColor

            For Each checkoff As DataRow In dtCheckoffs.Rows
                If checkoff("id_task") = row("id_task") Then
                    If Not IsDBNull(checkoff("training_start")) Then
                        newRow.trainingStart = checkoff("training_start")
                    End If
                    If Not IsDBNull(checkoff("training_complete")) Then
                        newRow.trainingComplete = checkoff("training_complete")
                    End If
                    If Not IsDBNull(checkoff("trainee_badge")) Then
                        newRow.traineeBadgeNum = checkoff("trainee_badge")
                    End If
                    If Not IsDBNull(checkoff("trainer_badge")) Then
                        newRow.trainerBadgeNum = checkoff("trainer_badge")
                    End If
                    If Not IsDBNull(checkoff("decertifying_official")) Then
                        newRow.decertifyingOfficial = checkoff("decertifying_official")
                        newRow.taskNumBackColor = Drawing.Color.LightSalmon
                        newRow.taskLevelBackColor = Drawing.Color.LightSalmon
                        newRow.taskBackColor = Drawing.Color.LightSalmon
                        newRow.trainingStartBackColor = Drawing.Color.LightSalmon
                        newRow.trainingCompleteBackColor = Drawing.Color.LightSalmon
                        newRow.traineeBadgeNumBackColor = Drawing.Color.LightSalmon
                        newRow.trainerBadgeNumBackColor = Drawing.Color.LightSalmon
                        newRow.decertifyingOfficialBackColor = Drawing.Color.LightSalmon
                    End If
                End If
            Next

            If row("is_header") = True And PlanPlaceHolder.Controls.Count > 1 Then
                Dim newLine As LiteralControl = New LiteralControl("<br/>")
                PlanPlaceHolder.Controls.Add(newLine)
            End If
            PlanPlaceHolder.Controls.Add(newRow)
        Next
    Catch ex As Exception
        clsLog.logError(ex)
    Finally
        DBConn.Close()
    End Try

编辑 2
我认为展示我如何更新我的 javascript 函数中的文本框可能也很重要

    function txtTrainingStart_Click(txtTrainingStart, txtTask) {

                var currentdate = new Date();
                var datetime = (currentdate.getMonth() + 1) + "/" + currentdate.getDate() + "/" + currentdate.getFullYear() + " " + getTimeAMPM();
                txtTrainingStart.value = datetime;
                txtTrainingStart.style.backgroundColor = "yellow";

    }
4

2 回答 2

0

编辑3:

下面的代码适用于静态控件,但动态创建的控件不会在回发时保留。您将需要存储更新的值,以便可以在回发时以某种方式检索它们(例如,会话变量或 HiddenField)。如果可能的话,我会静态创建控件并将它们填充到page_load,隐藏没有数据的控件 - 这将允许您在回发时获取值,如下所示。


嗯...我为您的代码设置了一个超级简化的版本并几乎按原样运行它(没有数据库功能),它似乎工作正常。

这是我使用的简化版本:

<asp:Content id="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ImageButton ID="imgbtnSave" runat="server"></asp:ImageButton>
    <asp:PlaceHolder ID="PlanPlaceHolder" runat="server">
        <asp:TextBox runat="server" ID="txtTrainingStart" value="before" ></asp:TextBox>
    </asp:PlaceHolder>
</asp:Content>

Javascript:

function txtTrainingStart_Click(txtTrainingStart, txtTask) {

    var currentdate = new Date();
    var test = "after";
    txtTrainingStart.value = test;
    txtTrainingStart.style.backgroundColor = "yellow";
}

VB:

Protected Sub imgbtnSave_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs Handles imgbtnSave.Click
    For Each item As TextBox In PlanPlaceHolder.Controls
        'need updated textbox values here.'
        Debug.WriteLine(item.ID)
        Debug.WriteLine(item.Text)
    Next
End Sub

我正在获取文本框的更新值,因此您应该能够将该值传递给函数并执行您需要的任何操作。

如果我遗漏了什么,请告诉我。

编辑 2:我只是有一个想法——你能page_load在点击保存按钮后检查是否被调用吗?我想知道它是否会再次用原始值覆盖您的更新值。

如果是这样,我会page_load使用回发检查来包装数据库功能:

If Not IsPostBack
    ' connect to database'
    ' populate placeholder'
End If
于 2013-08-06T15:30:17.577 回答
0

我已经想通了。我在正确的轨道上使用Request.Form我只是还不完全了解如何使用它。

我的保存按钮的新代码

Protected Sub imgbtnSave_Click(sender As Object, e As ImageClickEventArgs) Handles imgbtnSave.Click
    Dim coll As NameValueCollection
    coll = Request.Form

    For x = 0 To coll.Count - 1
        Response.Write("Key = " & coll.GetKey(x) & " Value = " & coll(x) & "<br />")
    Next
End Sub

循环从回发中输出所有控制键和更新的值,就像这样。

Key = ctl00$MainContent$ctl01$txtTrainingStart Value = 8/1/13
Key = ctl00$MainContent$ctl01$txtTrainingComplete Value = 8/2/13
Key = ctl00$MainContent$ctl02$txtTrainingStart Value = 8/3/13
Key = ctl00$MainContent$ctl02$txtTrainingComplete Value = 8/4/13

我使用隐藏字段来存储每个动态创建的 Web 用户控件的数据库行 ID。从那时起,很容易将更新的值插入我的数据库。

于 2013-08-08T20:28:11.303 回答