Need a few lines of code help, will pay!

  • Thread starter Thread starter Robert Johnson
  • Start date Start date
R

Robert Johnson

I need to figure out how to go through all of the WebControls on a form,
find all the ones that are named with a "txt" prefix (ie txtFirstName), and
then retrieve the WebControl ID, Text (value), and Tab Index and put them
into a collection or array.

I have spent all day working on this and I just can't quite figure it out. I
would appreciate any help you can give. I will happily pay $50 to the first
person who can come up with a solution that works!!!

Thanks a bunch!

Robert Johnson
 
Unfortunately, that doesn't quite work. Tab Index is not a property of
LiteralControl or ResourceBasedLiteralControl. As a result, I get the
following error: Public member 'TabIndex' on type
'ResourceBasedLiteralControl' not found.

It needs to use the WebControl instead of Control.

I am still still looking for a solution and am happy to pay for it! If you
(or anyone else) has a fix, I really need one!

Thanks,

Robert
 
Hi,

Here the code. I prefer to use types then text. Keep the mony ... :-)

1) Class to hold data :
class MyDat
{
public string Text;
public int TabIndex;
public MyDat(string inText,int inTabIndex)
{
Text = inText;
TabIndex = inTabIndex;
}
}

2) Form Load :
private void Page_Load(object sender, System.EventArgs e)
{

// Put user code to initialize the page here
System.Collections.Hashtable oT = new
System.Collections.Hashtable();
HtmlForm theForm = (HtmlForm)this.FindControl("WebForm3");
GetTextBoxes(theForm,oT);

}

3) recursiv function :
private void GetTextBoxes(Control Father,System.Collections.Hashtable
oT)
{
for (int i = 0 ; i< Father.Controls.Count; i++)
{
if (Father.Controls is TextBox)
{
MyDat oData = new MyDat(
((TextBox)Father.Controls).Text,((TextBox)Father.Controls).TabInde
x);
oT.Add(((TextBox)Father.Controls).ID,oData);
}
if (Father.Controls.Count > 0)
{
GetTextBoxes(Father.Controls,oT);
}
}
}

Natty Gur, CTO
Dao2Com Ltd.
34th Elkalay st. Raanana
Israel , 43000
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
You have to check for the type of the control.
Here's a snippet that help. You will have to refine it
depending on what you are trying to do.

Dim ctl As Control
Dim ctlname As String
Dim tb As TextBox
Dim tbvalue As String

For Each ctl In Page.FindControl
("form1").Controls
ctlname = ctl.ID
'At this point you can also check if name starts with txt
If Ctl.GetType.ToString=
"System.Web.UI.WebControls.TextBox" Then
tb = ctl
tb.TabIndex = 1 'or whatever
tbvalue = tb.Text
End If
Next

Ofcourse if you put this code in the page_load section
the value of the text box will be nothing.
 
Back
Top