Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all.
In the below code I generate runtime textbox control.I have to store contents this textbox in datebase.But I don't know how get runtime textbox controls property.
Please help me

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        TableRow row;
        TableCell cell;
        TextBox ttt;
        TextBox tx;
        for (int i = 0; i < 10; i++)
        {
            row = new TableRow();
            cell = new TableCell();
            ttt = new TextBox();
            ttt.ID = "t" + i.ToString();
            cell = new TableCell();
            cell.Controls.Add(ttt);
            row.Cells.Add(cell);
            tx = new TextBox();
            tx.ID="tt"+ i.ToString();
            cell = new TableCell();
            cell.Controls.Add(tx);
            row.Cells.Add(cell);
            Table1.Rows.Add(row);
        }

    }
Posted
Comments
Wonde Tadesse 14-Aug-11 12:02pm    
How you able to assign ttt.ID = "t" + i.ToString();, if you don't know how to access runtime textbox controls property ?
Sergey Alexandrovich Kryukov 15-Aug-11 1:37am    
Good point. I don't think this is about "runtime control" (there is not non-runtime controls), but OP does not know what to do with local variable -- it won't be available after the exit from this method. Hard to help with this level of knowledge.
--SA
Sergey Alexandrovich Kryukov 15-Aug-11 1:46am    
So, I added my solution, please see.
--SA

Just access the properties inside ttt directly.
For e.g. ttt.Text would allow you to get/set text.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Aug-11 1:46am    
This is of course correct, my 5, but... I don't think this is about "runtime control" (there is not non-runtime controls), but OP does not know what to do with local variable -- it won't be available after the exit from this method. Hard to help with this level of knowledge.

Anyway, I tried to explain this matter -- please see my solution.
--SA
First of all, please understand that all controls are "run-time controls". There are no "non-runtime" controls. Even if a control is created with Designer, the actual instance is created during runtime.

I suspect your problem is to access to the control via its variable ttt, as it is local. Well, you need to have a form's field member for this control. You can use your local variable, but eventually you need to assign the form's field to this variable. You can do it because the Control is a reference type, so the instance of the control will be referenced by both these (or many other, if you want) variables.

I suggest you pause your UI development and learn be basics.
You need to understand perfectly:


  • Type system;
  • Reference and value types;
  • Classes, structures and instances;
  • Static and instance members of types;
  • Visibility and scope of members of types; static, instance and local (stack) variables;
  • Method parameters and methods of parameter passing for both reference and value types, their functionality;
  • Other fundamentals of programming in general, .NET in particular.


—SA
 
Share this answer
 
v2
Comments
Abhinav S 15-Aug-11 1:49am    
Yes correct. If this is what the OP means, then I'm sure this will help him out. 5.
Sergey Alexandrovich Kryukov 15-Aug-11 1:54am    
Hope so. As Wonde pointed out, OP probably knows how to access ttt members.
Thank you, Abhinav.
--SA
BobJanova 15-Aug-11 10:01am    
Good high level answer, 5 from me. I think the OP needs a good grounding in C#, not just a direct answer to the question. However, it isn't strictly accurate; you don't need to assign to a form's field because you can always look up in the Controls property or use FindByName on the control's parent.
Sergey Alexandrovich Kryukov 15-Aug-11 13:00pm    
Yes, of course, but I did not advice this because this is dirty.
Thank you for voting, Bob.
--SA
Wendelius 15-Aug-11 16:12pm    
Very good answer, my 5
The big picture:

0. you are assigning a string to a property 'ID of TextBox in your code: the WinForms TextBox does not have an 'ID property, so this code will not compile in WinForms: are you using some other TextBox that has an 'ID property ?

1. if you are going to have a real-world situation where you have many rows in your Data Object, each populated with 10 TextBoxes, you are going to have, imho, a potentially sluggish application. Is there another strategy for you to allow the user to enter/edit text in your Data Object's cells/fields ... other than putting a WinForm Control in the cell ?

Examining your code what is going on appears to be rather simple:

0. You are creating controls at run-time, rather than design-time.

1. obviously you have a DataObject, perhaps a DataGridView control, or whatever, named 'Table1

2. when you click 'Button1: you execute a loop in which you create 10 new Rows to add to your data object.

3. For each new Row you create two TextBoxes, one named 'tt, and one named 'tx. Those are inserted in Cells, and the Cells inserted in the Row being constructed.

Because the TextBoxes you create are in the scope of the code for Button1's Click event, you can't access them outside that code.

Yes, you might well be able to dynamically inspect the Rows of your DataObject, query each cell in the row to see if it has a Control, make sure the Control is a TextBox, get the Control, cast it into a TextBox, get its TextBox.Text value, and store that value in a Data Object.

But you can see how much 'work' that would be.

I believe you need to implement a strategy for saving a reference to the run-time created TextBoxes in a way that you can easily access them later, and read their .Text content value and save that in a database.

So, please think about something like:


edit #1 ... cleaned up formatting errors introduced by CP's code-block parser ... edit #1 /


List<TextBox>ListOTT = new List<TextBox>();
List<TextBox>ListOTX = new List<TextBox>();
List<List<TextBox>> RowList = new List<List<TextBox>>();
List<List<List<TextBox>>> RowsList = new List<List<List<TextBox>>>();
So, in your Button Click event:
C#
for (int i = 0; i < 10; i++)
{
    // start a fresh row
    ListOTT.Clear();
    ListOTX.Clear();
    RowList.Clear();

    row = new TableRow();
    cell = new TableCell();
    ttt = new TextBox();

    // fix this: will not compile with WinForms TextBox
    ttt.ID = "t" + i.ToString();

    ListOTT.Add(ttt);

    cell = new TableCell();
    cell.Controls.Add(ttt);
    row.Cells.Add(cell);
    tx = new TextBox();

    // fix this, too
    tx.ID="tt"+ i.ToString();

    ListOTX.Add(ttt);

    cell = new TableCell();
    cell.Controls.Add(tx);
    row.Cells.Add(cell);

    // each RowList contains
    // two Lists<textbox>
    RowList.Add(ListOTT);
    RowList.Add(ListOTX);

    // add the Row
    // to the List<row>
    RowsList.Add(RowList);

    Table1.Rows.Add(row);
}</row></textbox>
edit #2 If you see <row><textbox> just after the final curly brace above, please ignore: it's an artifact of CP's code-block parser I can't find a way to work around. edit #2 /

Now that suggestion ... a "matryoshka" Russian-nested-doll solution :) ... is only one of many ways you could implement some 'container' or data-structure strategy for storing the TextBoxes you create at run-time. Perhaps using a special Class would be better.

good luck, Bill

 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900