Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing text field value

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
Hey,
I have a piece of code below in which I am creating multiple rows on a dynamic table, each with a number of text fields I have to gather on a button click. My question is how do I access each text field? I have named them according to a static name + the row number in which it falls into. How do I incorporate the row number in the name when I get to it in my loop?

int max_parms = 32;
int max_parm_count;
for (max_parm_count = 1; max_parm_count <= max_parms; max_parm_count++)
{
TableRow tr2 = new TableRow();

TableCell MSN = new TableCell();

TextBox _MSN = new TextBox();
_MSN.Width = 80;
_MSN.ID = "MSN" + max_parm_count.ToString();
_MSN.Text = "";
_MSN.Enabled = true;

MSN.Controls.Add(_MSN);
tr2.Cells.Add(MSN);
tblDynamic.Rows.Add(tr2);
}

TableRow row1 = new TableRow();

TableCell RunTestBtn = new TableCell();
RunTestBtn.HorizontalAlign = HorizontalAlign.Center;

Button _RunTestBtn = new Button();
_RunTestBtn.ID = "RunTestBtn";
_RunTestBtn.Text = "Run Test";
_RunTestBtn.Click += new EventHandler(_RunTestBtn_Click);
RunTestBtn.Controls.Add(_RunTestBtn);
row1.Cells.Add(RunTestBtn);

tblSaveQuitPrint.Rows.Add(row1);

private void _RunTestBtn_Click(object sender, System.EventArgs e)
{
int i = 1;
int flag = 0;

while(i < 32)
{
// This is the line I need to figure out
if(MSN+i.ToString() == "")
{
flag = 1;
break;
}

i = i + 1;
}
}

Any help would be appreciated.

Thanks
Mike
 
I am confused. Do you want to loop through and get the text that is in each of your textboxes?
 
that's exactly what I want to do. I have the ID being created, but don't know the syntax to get the value in it.

ex.
MSN1 --> value = 123
MSN2 --> value = 102
MSN3 --> value = rest

It's when I try to access then where I get confused. How do I syntactically say I want "MSN"+index in C#?
 
when I do that, the only options I get are:

CompareTo
Equals
GetHashCode
GetType
GetTypeCode
ToString

I tried ToString, but this is tha error I get:

The name 'MSN' does not exist in the class or namespace 'TestScreen.WebForm1'
 
you may need to try

if((MSN+cstr(i)).Text == "")

I do not know the CSharp code to do this

basically convert the i (int to a string) concantinate to the "MSN" and try the .text



 
I still get the same error.

I also tried setting a temp string and setting it to the name:

test = "MSN"+i.ToString();

which will give it MSN1 (ex.), but when I try to access temp.Text I get:

'string' does not contain a definition for 'Text'

 
I belive you have to loop through the conrols on the page. This is VB code, but you can convert it easily to C#

Code:
   Dim c As Control
        For Each c In parent.Controls
           If c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
              Dim tb As TextBox
              tb = CType(c, TextBox)
              If Trim(tb.Text) <> "" Then
                 response.write(tb.id)
                 response.write(tb.text)
              End If
           End If
        Next c
 
Alright, I figured it out. Since I can access the table, row, and cell fairly easy I can cast it as a textbox:

temptext = (TextBox)tblDynamic.Rows.Cells[j].Controls[0]

with temptext being added at the top as a textbox.

Thanks for all the help!!!
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top