Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting asp:TextBox text property in codebehind from a web user control after a javascript update
    text
    copied!<p>Here is the scenario.</p> <p>I have a simple page containing an asp:PlaceHolder.</p> <pre><code>&lt;%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="TrainingPlan.aspx.vb" Inherits="TrainingPlan" %&gt; &lt;%@ Reference Control="ctlTask.ascx" %&gt; &lt;%@ Reference Control="ctlTaskheader.ascx" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"&gt; &lt;div class="centered"&gt; &lt;h2&gt;&lt;asp:Label ID="lblPlanTitle" runat="server" Text="Plan Title"&gt;&lt;/asp:Label&gt;&lt;/h2&gt; &lt;hr /&gt; &lt;br /&gt; &lt;asp:ImageButton ID="imgbtnSave" runat="server" ImageUrl="~/Images/save.ico" /&gt; &lt;br /&gt; &lt;asp:PlaceHolder ID="PlanPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt; &lt;/div&gt; &lt;/asp:Content&gt; </code></pre> <p>The placeholder on this page is populated with several rows of the same web user control. This web user control contains several textboxes. For each textbox in this web user control I have public properties to set and get the text value. In the page load event of the web user control I am adding onClick attributes to some of those textboxes like so.</p> <pre><code>Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load txtTrainingStart.Attributes.Add("onClick", "txtTrainingStart_Click(" &amp; txtTrainingStart.ClientID &amp; ", " &amp; txtTask.ClientID &amp; ");") txtTraineeBadgeNum.Attributes.Add("onClick", "txtTraineeBadgeNum_Click(" &amp; txtTraineeBadgeNum.ClientID &amp; ", " &amp; txtTask.ClientID &amp; ", " &amp; txtTrainingStart.ClientID &amp; ");") txtTrainerBadgeNum.Attributes.Add("onClick", "txtTrainerBadgeNum_Click(" &amp; txtTrainerBadgeNum.ClientID &amp; ", " &amp; txtTrainingComplete.ClientID &amp; ", " &amp; txtTask.ClientID &amp; ", " &amp; txtTraineeBadgeNum.ClientID &amp; ", " &amp; Session.Item("isTrainer").ToString.ToLower &amp; ");") txtDecertifyingOfficial.Attributes.Add("onClick", "txtDecertifyingOfficial_Click(" &amp; txtDecertifyingOfficial.ClientID &amp; ", " &amp; txtTrainerBadgeNum.ClientID &amp; ", " &amp; txtTask.ClientID &amp; ", " &amp; Session.Item("isDecertifyingOfficial").ToString.ToLower &amp; ");") End Sub </code></pre> <p>For each of those onClick events I have corresponding javascript functions.</p> <pre><code>&lt;script type="text/javascript"&gt; function txtTrainingStart_Click(txtTrainingStart, txtTask) { //processing and updates to textboxes here } &lt;/script&gt; </code></pre> <p>Here is the problem.</p> <p>On the main page containing the placeholder I have a save button. In the click event of the save button I am looping through each of the web user controls contained in the placeholder to process and save the data. </p> <pre><code>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 </code></pre> <p>Everything I have tried I can only get the original value that was in the textbox when the page loaded. I would think that this should be simple and I am just missing something basic. I've searched google high and low for the solution but I have yet to find one. This was the closest thing I found -> <a href="https://stackoverflow.com/questions/10128530/set-text-property-of-asplabel-in-javascript-proper-way">Set Text property of asp:label in Javascript PROPER way</a>. Although that post deals with a label rather than a textbox and isn't using a placeholder containing several web user controls. From what I understand I need to POST the updates back to the server from the client but I don't know how to do this. I've tried using the <code>Request.Form</code> property but I couldn't seem to make that work. What am I missing?</p> <p>Thank you for any help, Rylan</p> <p><strong>EDIT</strong> <br /> Here is the code that populates the placeholder with the web user controls as requested</p> <pre><code>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") &amp; " 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 = " &amp; Request.QueryString("id_plan") &amp; " 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 &lt;&gt; row("task_num") Then dtCurrentRev.ImportRow(row) lastTaskNum = row("task_num") End If Next strSQL = "SELECT * FROM checkoffs WHERE emp_id = " &amp; 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 &amp; "*" 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 &gt; 1 Then Dim newLine As LiteralControl = New LiteralControl("&lt;br/&gt;") PlanPlaceHolder.Controls.Add(newLine) End If PlanPlaceHolder.Controls.Add(newRow) Next Catch ex As Exception clsLog.logError(ex) Finally DBConn.Close() End Try </code></pre> <p><strong>EDIT 2</strong><br /> I thought it might also be important to show how I am making updates to the textboxes in my javascript functions</p> <pre><code> 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"; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload