Create a custom server control to accept user input

I am trying to create a server control that, depending on the display of "QuestionTypeId", displays either a text box, a date picker, or a Yes / No radio button.

I have my control showing how I want it, but when the form button is clicked on the form, the textbox, date picker, or radio buttons that were generated in the RenderContents method are null.

I tried to keep the generated controls in view state which stopped them at zero, but user inputs were not saved.

I'll post code if needed. Just ask.

0


source to share


5 answers


I think you need to create (and add) controls to CreateChildControls . This would mean that you need to store the QuestionTypeId value in the ViewState or ControlState (I would argue that ControlState is applicable in this case, since your control cannot function without this value).



+1


source


When you add controls dynamically, you need to make sure they are recreated before restoring the view state.

I haven't done this for a while, but from memory I think you should recreate your controls in the OnInit method. This happens before the postback data is loaded and before the controls have their values ​​set from the viewstate.



It might be worth doing some reading on the asp.net page lifecycle:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

0


source


You can create a custom control and use server controls for text fields, datepicker, radio buttons.

If you are creating a cusom control server, you need to add the published data to your control properties. You can do this under your control. OnInit event:

MyProperty = Request.Form("myControl");

      

0


source


A simple technique would be to create all the controls at design time and make the controls invisible based on your requirements.

Sample code:

protected void Page_Load(object sender, EventArgs e)
{
    txtBox.Visible = QuestionTypeID == 1;
    chkBox.Visible = QuestionTypeID == 2;
}

      

If you are using dynamic controls, you should do as David pointed out, store the QuestionTypeID value in a ViewState or ControlState, and then create the control you want based on that value.

(the controls need to be instantiated every time the page is loaded even a column back, and they cannot be instantiated later in the page lifecycle, then the Page_Load method if you want the ViewState to persist and restore)

Sample code:

protected void Page_Load(object sender, EventArgs e)
{
    var questionId = ViewState["QuestionTypeID"];

    if(questionId == /* Value to create TextBox */) {
        var txt = new TextBox { ID = "txt" };
        placeHolder.Controls.Add(txt);
    } else if(questionId == /* Value to create Calender */) {
        var cal = new Calender { ID = "cal" };
        placeHolder.Controls.Add(cal);
    }

    /* Once the controls are added they will be populated with the posted values */
}

      

PS
It's always a good idea with dynamic controls to specify the ID.
You can save the added controls to member variables and use them elsewhere (after they have been assigned)
You can subscribe to their events and if the user has posted a new value, your method will be called

0


source


I followed your advice and did the following:

1) The question type is stored in the view state in my server.

2) on CreateChildControls now creates a new instance of my control and adds it to the place owner on the page.

Now my problem is that things seem to fire in a bit of odd order:

1) On initial page load, the child controls are created and my server's RenderContents method runs.

2) Clicked button to load new contact, triggers create child controls and RenderContents.

3) data is entered and the save is saved, this causes the child controls to be created, but the RenderContents does not start and a NullReferenceException is thrown while trying to access my control to get the value. (If I skip the code accessing my controls, RenderContents gets called and renders.

At the same time, another problem is that when I try to set the value (onDataBind), I try to access the textbox that was generated in my server control and get another NullReferanceExeption

Thoughts?

0


source







All Articles