0

In an ASPxPageControl i have added some labels and textboxs dynamically created. Initially the Text property of the text box is empty until the end user fills the text box according to the information necessary.

The next step is to press the Save Button and when this occurs, in the button_ClickEvent function I get all the controls correctly but the text property always is empty even though the user have filled in the text box

Am I missing something in my code?

Here's my code:

    PropertyInfo cntrlProperty;
    foreach (System.Web.UI.Control cntrl in pControl.TabPages[1].Controls)
    {
          Type ControlType = testAssembly.GetType(typeof(ASPxTextBox));

          if (!cntrl.GetType().Equals(typeof(ASPxLabel)) && cntrl.GetType().Equals(ControlType))
          {


             cntrlProperty = cntrl.GetType().GetProperty("Text");

             var value = cntrlProperty.GetValue(cntrl);

              VALUES += String.Format("'{0}'" + ",", value);
          }


    }

Also I tried to set a default string when creating my controls dynamically and this woks fine because this default string is in the text box when the controls are rendered.

let's say the default string = "just a string";

Till this point in my code above the variable value = just a string and this is OK.

Then what I did was to append(I typed "in the textbox") some text to the "default string" in the runtime NOW default string looks like

default string = "just a string in the textbox"

and then I pressed Save Button and it turns out that the variable value it still stores value = just a string

Any idea why is this happenig?

Nahum Fabian
  • 180
  • 1
  • 2
  • 15

1 Answers1

0

OK, I don't think you need reflection at all. You already have knowledge of the type you are targeting. You commonly use reflection when you don't know the type or you could be dealing with multiple types and trying to fit them into a pattern.

Consider this:

    foreach (System.Web.UI.Control cntrl in pControl.TabPages[1].Controls)
    {
        if (cntrl is ASPxTextBox)
        {
           VALUES += string.Format("'{0}' + ",", (cntrl as ASPxTextBox).Text);
        }
    }

If when doing this you still don't get the text property value, there might be a problem with the internals of the control and is not capturing the entered text in the browser when the page goes through the LoadPostData stage.

Also something you need to be aware of is that for this LoadPostData to be successful when adding controls using ASP.NET AJAX is that you need to load this control on the page for every postback after you create them to be able to get the values back.

Hope this helps.

Arturo Martinez
  • 3,737
  • 1
  • 22
  • 35
  • I tried your code and I still cant get the text value back. I think the problem could be that thing you mentioned(LoadPostData) or something with Page life cycle(I have read it) I would like you provide me some usefull links about LoadPostData, I've been searching and I havent found nothing helpful. Do you think this problem could be also by the **RegisterRequiresPostBack()** function? I've been reading this but i still cant get it. – Nahum Fabian Apr 22 '13 at 17:33
  • I think i need to use Callbacks instead of PostBacks, what do you think @Arturo Martinez? – Nahum Fabian Apr 22 '13 at 17:38
  • First, did you develop the custom control? or is this a third party control. If this is a third party control try looking in their forums for a possible solution because the control internally needs to handle the LoadPostData method. – Arturo Martinez Apr 22 '13 at 18:27
  • One more thing you can do is to study the values posted to the server from all the HTML form fields. This would confirm if the value is actually being posted. You can access this from `Page.Request.Form[""]`. If the expected value is found using this approach, then you can confirm is a control internal issue or somehow your page logic is overwriting the posted value. – Arturo Martinez Apr 22 '13 at 18:30