gpt4 book ai didi

javascript - 获取asp :TextBox text property in codebehind from a web user control after a javascript update

转载 作者:行者123 更新时间:2023-12-02 18:51:11 25 4
gpt4 key购买 nike

这是场景。

我有一个包含 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 用户控件中的每个文本框,我都有公共(public)属性来设置和获取文本值。在 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";

}

最佳答案

编辑3:

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

<小时/>

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

这是我使用的简化版本:

<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

关于javascript - 获取asp :TextBox text property in codebehind from a web user control after a javascript update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com