0

I have a placeholder on a page which is populated with a dynamic table which has dynamic added textboxes (which is filled with language dependent data). The placeholder is filled with data on each page.load but with different data depending on which language choice a use makes. On the initial load english is set which works, but when the user change to french language the data in the textboxes is not changed from english to french. But when I debug I can see that the french language is added to each textbox. I have tried to disable viewstate on the table but that did not help. I also use PlaceHolder.Controls.Clear() before adding controls/data again to it. None of them works, still display initial english values in each textbox. What could be causing this?

    Protected Function FindTextBoxValues(ByRef ParentControl As Control, ByRef MyList As List(Of String)) As List(Of String)
    For Each ctrl As Control In ParentControl.Controls
        If TypeOf ctrl Is TextBox Then
            ' do something
            Dim CurrCtrl As New TextBox()
            CurrCtrl = CType(ctrl, TextBox)
            MyList.Add(CurrCtrl.Text)
        ElseIf ctrl.HasControls Then
            FindTextBoxes(ctrl, MyList)
        End If
    Next
End Function
MTplus
  • 2,077
  • 4
  • 34
  • 51
  • Are you reloading the page during this or using update panels? – H.Mikhaeljan Mar 21 '18 at 07:45
  • When the user change language the page is reloaded and french is loaded. When the data is loaded each table row that is generated contains a Literal with text and a textbox, the literal change values but the textbox keeps it initial value (english)... – MTplus Mar 21 '18 at 07:50

1 Answers1

0

Made a small example.

First add the placeholder

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Then making the dropdown for language. AutoPostBack to true

<asp:DropDownList ID="ddlLanguage" runat="server" OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" AutoPostBack="true"> 
    <asp:ListItem Text="english" Value="english"></asp:ListItem>
    <asp:ListItem Text="french" Value="french"></asp:ListItem>
</asp:DropDownList>

In Page_Init

protected void Page_Init(object sender, EventArgs e)
{
    TextBox tb = new TextBox();
    tb.ID = "test1";
    if (!Page.IsPostBack)
    {
        tb.Text = "this is a English";
    }
    PlaceHolder1.Controls.Add(tb);
}

this event triggers and find the control inside the placeholder.

protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox ctrl = (TextBox)PlaceHolder1.FindControl("test1");
    DropDownList ddl = (DropDownList)sender;
    if (ddl.SelectedValue == "english")
    {
        ctrl.Text = "this is a English";
    }
    else if (ddl.SelectedValue == "french")
    {
        ctrl.Text = "this is a French";
    }
}

Tested it myself to make sure and it works. Hope it helps you further.

H.Mikhaeljan
  • 813
  • 12
  • 22
  • Thanks that worked to move the fill of data of the placeholder to the selectedindexchange, now I only have to find a way to retrieve the values from each textbox after post of page. I have tried the function above in my original post but that does not retrieve the changed values, only the values that where fetch from start.... – MTplus Mar 21 '18 at 08:49
  • Hmmm, I correct myselt. The above function (FindTextBoxValues) for retreiving the textbox values does not work when I have moved the placeholder data fill to the selectedindexchanged. It only works when its in page.load. So how can I now retrieve the dynamically added textbox values during post? – MTplus Mar 21 '18 at 10:25
  • I changed the code to your needs. You have to remake the controls in the Page_Init. After you can read values with PlaceHolder.FindControl(). – H.Mikhaeljan Mar 21 '18 at 14:01