Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Re: HOW: Read dynamic control on postback

Reply
 
Old 04-09-2009   #1 (permalink)
Jesse Houwing


 
 

Re: HOW: Read dynamic control on postback

Hello JP,
Quote:

> I think I finally got this thing figured out. Granted it takes a bit
> of code.
>
> Basically I'm generating the dynamic controls in the
> gridview.RowDataBound() Then in GridView.DataBound() if EVENTTARGET
> that generates the GridView is what fired the AJAX post back, I'm
> cycling though all the controls for each row in the given cell and
> adding them to a Control[row#,control object] type array so I can
> maintain all the properties that were set. I then store this array in
> a session.
>
> Then on post back, I check the page EVENTTARGET to ensure that the
> object that generated the full post back was the submit button
> otherwise I don't want to waste time cycling through the array.
>
> I make sure the session value is not null and convert it back to the
> Control[,] array. I cycling though the array OnPage_Load, I simply
> cycle though GridViewRows and do Controls.Add to the cell using the
> object stored in the array.
>
> So far seems to work. I have some other tweaks I need to make though
> but I've gotten over a major hurdle.
>
> You have been a huge help.
Instead of adding them to the session, I'd put it in the viewstate, as the
data is page specific.

It was a pleasure helping .

Jesse
Quote:

>
> "Jesse Houwing" wrote:
>
Quote:

>> Hello JP,
>>
Quote:

>>> Ok, this might pose a problem then if the dynamic controls are in a
>>> GridView.
>>>
>>> In the page life cycle Page_Init occurs before Page_Load with
>>> viewsate becoming available just before Page_Load occurs.
>>>
>>> The way I understand it is that on postback ,the dynamic controls
>>> have be added to the back to the collection in the Page_Init method.
>>> Since this method occurs before the Page_Load method, there is no
>>> GridView object to add the controls back to because the postback
>>> data wont regenerate the gridview until it executes the Page_Load
>>> Method.
>>>
>> It's always tricky to find the right spot... At some point
>> EnsureChildControls(); is called on the page. After that it should be
>> in the right state. You can override EnsureChildControls to add the
>> controls where needed at the right time. The grid control will make
>> sure it will have empty rows added, no data just yet. That's the time
>> to ensure your textboxes get in there as well.
>>
>> JEsse
>>
Quote:

>>> "Jesse Houwing" wrote:
>>>
>>>> Hello JP,
>>>>
>>>>> So what you are saying is that after the user clicks submit
>>>>> (postback), I still have to add the control back to the
>>>>> collection, before I can see what the user typed? If thats true,
>>>>> how does the view state know that the properties belong to the
>>>>> dynamic control?
>>>>>
>>>> The value has been added to the viewstate in the same tree
>>>> structure as the controls were in. The control will get the right
>>>> values from the viewstate as long as you add it as follows:
>>>>
>>>> TextBox tb = new TextBox();
>>>> tb.ID = "id";
>>>> row.Add(tb);
>>>> e.g. It is important to set the ID before adding the control to the
>>>> tree.
>>>> Anything else, you need to set after adding the control to the
>>>> control tree, that way anything which was written from ViewState
>>>> will succesfully be read and applied, before any other changes are
>>>> done to the control.
>>>>
>>>>> I assume this is where the protected override void
>>>>> OnInit(EventArgs e) comes in to play? In this particular
>>>>> situation, I do want to maintain the value for postback solely for
>>>>> the purposes of storing it in the database, but I do not want to
>>>>> re-render the page after the postback.
>>>>>
>>>> The Grid usually has a RowCreated event, which will be fired when
>>>> the control tree is being recreated. That is the moment you can
>>>> safely add the control and re-apply eventsubscriptions for teh
>>>> specific control. This will also allow any event you subscribed to
>>>> to be fired after Page_Load.
>>>>
>>>> Then later in a databinding event, or in or after the Page_Load,
>>>> you can change any value in the control or remove it altogether.
>>>>
>>>> Jesse
>>>>
>>>>> "Jesse Houwing" wrote:
>>>>>
>>>>>> Hello JP,
>>>>>>
>>>>>> Did you re-add the control to the grid before reading it's value?
>>>>>> A dynamically added crontrol will need to be added back to the
>>>>>> control tree on every subsequent request, should you want to
>>>>>> access its value...
>>>>>>
>>>>>> Jesse
>>>>>>
>>>>>>> You are absolutely correct. I mistakenly typed my example in
>>>>>>> this message. This statement is inside a foreach loop that
>>>>>>> should have read as follows:
>>>>>>>
>>>>>>> foreach (GridViewRow gvr in gridQuestions.Rows)
>>>>>>> {
>>>>>>> //txtTransID is the dynamically generated control but this
>>>>>>> always
>>>>>>> returns null or error
>>>>>>> string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
>>>>>>> }
>>>>>>> "Jesse Houwing" wrote:
>>>>>>>> Hello JP,
>>>>>>>>
>>>>>>>>> I created a page that has dynamically created TextBox or
>>>>>>>>> DropDownList controls.
>>>>>>>>>
>>>>>>>>> However once the text box is filled in and I submit the page,
>>>>>>>>> I cannot find the dynamic control. I have been all over the
>>>>>>>>> net looking for an answer. I read the article from
>>>>>>>>> 4GuysFromRolla but it still does not answer my question.
>>>>>>>>>
>>>>>>>>> How to I retrieve the values from a dynamically created
>>>>>>>>> control on postback?
>>>>>>>>>
>>>>>>>>> This was the name of the control I added to a GridView (AJAX
>>>>>>>>> call)
>>>>>>>>>
>>>>>>>>> TextBox controlTextBox = new TextBox();
>>>>>>>>> controlTextBox.EnableViewState = true;
>>>>>>>>> controlTextBox.ID = "txt" +
>>>>>>>>> drControls["controlName"].ToString();
>>>>>>>>> controlTextBox.MaxLength = 6;
>>>>>>>>> controlTextBox.Width = 150;
>>>>>>>>> controlTextBox.Enabled = true;
>>>>>>>>> controlTextBox.Visible = true;
>>>>>>>>> controlTextBox.Attributes.Add("runat", "server"); //just for
>>>>>>>>> the
>>>>>>>>> heck of it
>>>>>>>>> e.Row.Cells[2].Controls.Add(controlTextBox);
>>>>>>>>> (All is well at this point and the control is visible on the
>>>>>>>>> page)
>>>>>>>>> if(IsPostBack)
>>>>>>>>> {
>>>>>>>>> //Control never found
>>>>>>>>> string Answer=
>>>>>>>>> ((TextBox)GridView.FindControl("txtTransID")).Text;
>>>>>>>>> }
>>>>>>>>> I understand that the control itself (GUI representation)
>>>>>>>>> needs
>>>>>>>>> to
>>>>>>>>> be
>>>>>>>>> recreated, but should not the control properties be in the
>>>>>>>>> viewstate
>>>>>>>>> to access them?
>>>>>>>>> PLEASE HELP! Does anyone have a working example? I could care
>>>>>>>>> less
>>>>>>>>> that the controls get re-rendered on the postback as they get
>>>>>>>>> redirected to a different screen after the data saves, but I
>>>>>>>>> really
>>>>>>>>> need to just access the value
>>>>>>>> The control is added to a naming container (the grid), which
>>>>>>>> makes sure the control name is unique per row (each row will
>>>>>>>> contain the specified textbox).
>>>>>>>>
>>>>>>>> So you'll have to query the specific row of the grid control in
>>>>>>>> order to use FindControl.
>>>>>>>>
>>>>>>>> eg
>>>>>>>>
>>>>>>>> foreach (GridRow r in Grid.Rows)
>>>>>>>> {
>>>>>>>> r.FindControl("txtTransID");
>>>>>>>> }
>>>>>>>> should do the trick
>>>>>>>> --
>>>>>>>> Jesse Houwing
>>>>>>>> jesse.houwing at sogeti.nl
>>>>>> --
>>>>>> Jesse Houwing
>>>>>> jesse.houwing at sogeti.nl
>>>> --
>>>> Jesse Houwing
>>>> jesse.houwing at sogeti.nl
>> --
>> Jesse Houwing
>> jesse.houwing at sogeti.nl
--
Jesse Houwing
jesse.houwing at sogeti.nl



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
HOW: Read dynamic control on postback .NET General
Can't read appSettings of IE hosted control when under SSL .NET General
Read messages turn UN-read again Live Mail
READ THIS IF YOU CANNOT GET DRIVE TO READ DISK TO FIX VISTA ERROR! Vista hardware & devices
Folders/files read only/can't create new folder in read only folde Vista account administration


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46