How to display variable data in dynamic textbox - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to display variable data in dynamic textbox

vishalonnevishalonne Posts: 6Member
Hi

How can add display data from 4 variables in a dynamically created textbox which is inside dynamically created table, row and cell here is the code which is working fine and adding text also at the time of textbox creation but I want to add different variable values in these textbox.

I have only 1 button on my page and on click event following code executed -[code]
private int numOfRows = 1;
[b]int a = 1; int b = 2; int c = 3; int d = 4;[/b]
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
private void GenerateTable(int rowsCount)
{
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);

//The number of Columns to be generated
const int colsCount = 4;//change the value based on you requirements

// Now iterate through the table and add your controls

for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j; // Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
string cellid = tb.ID;

// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow

row.Cells.Add(cell);
}

// And finally, add the TableRow to the Table
[b]table.Rows.Add(row);
for (int k = i; k <= i; k++)
{
a++; b++; c++; d++;
for (int m = 0; m < 4; m++)
{
textboxid="TextBoxRow_" + k + "Col_" + m;
((TextBox)this.FindControl(textboxid)).Text = a.ToString();
}
}[/b]
}

//Set Previous Data on PostBacks
SetPreviousData(rowsCount, colsCount);

//Sore the current Rows Count in ViewState
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}

private void SetPreviousData(int rowsCount, int colsCount)
{
Table table = (Table)Page.FindControl("Table1");
if (table != null)
{
for (int i = 0; i < rowsCount; i++)
{
for (int j = 0; j < colsCount; j++)
{
//Extracting the Dynamic Controls from the Table
TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
//Use Request objects for getting the previous data of the dynamic textbox
//tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
}
}
}
}
[/code]
I am able to view the value of variable "a" only in all 4 textbox in a single row like this on click of button -

cell1 cell2 cell3 cell4
Row1 2 2 2 2
Row2 3 3 3 3
Row3 4 4 4 4

......

I just want to view the values of all 4 variables "a", "b", "c" and "d" in
cell1 cell2 cell3 cell4
Row1 2 3 4 5 Value of a,b,c,d
Row2 3 4 5 6
Row3 4 5 6 7

......

Hope some body guide me.

Regards
Sign In or Register to comment.