上次的问题:http://community.csdn.net/Expert/topic/3410/3410805.xml?temp=.4355127
提到了FindControl,反复验证过后,发现FindControl只会查看容器里面是否有控件,如果负值给一个变量,那么结果将会是相应控件的类型。5555555555555555然后又看了foreach方法,发现竟然是C#的。
55555555555555555555555555555555555555
好痛苦,有没有人解决一下呀。
4天了。这次  100 分,对不懂的问题决不吝啬。问了跟清楚一点,我再阐明一下意图:
“我有几个TextBox,如ID为TextBox1、TextBox2 ……,在Button事件里面需要一个循环去查看他们是否输入了值,如果没有将执行数据库写入。最好是VB代码,因为C#的很多方法再VB里不能用。”再次麻烦大家了。

解决方案 »

  1.   

    Dim c As Control
    For Each c In Page.Controls(1).Controls
    If c.GetType().ToString() = "System.Web.UI.WebControls.TextBox" Then
    (CType(c, System.Web.UI.WebControls.TextBox)).Text = "这个不是你要的?"
    End If
    Next
      

  2.   


    dim i as integer
    dim sName as string
    dim txt as TextBox
    dim sValue as stringfor i = 1 to 12
     sName = "TextBox" & i
     txt = CType(Page.FindControl(sName), TextBox)
     if not txt is nothing then
           sValue = txt.Text.Trim()
           if sValue.Length > 0 then
                 'save to the database
           end if 
     end if
    next
      

  3.   

    dim i as integerfor i=0 to 5
     try  
       if cType(page.findcontrol("Textbox" & i),TextBox).text.trim.length>0 then
         ''您的相關操作
       end if
     catch
         ''textbox為空的處理
     end try
    next
      

  4.   

    private void Del_Click()
    {
    int checkNum=0;
    for (int i=0;i<gridmain.Items.Count;i++)
    {
    CheckBox chkTemp=(CheckBox)gridmain.Items[i].FindControl("chkSel");
    TextBox btnTemp=(TextBox )gridmain.Items[i].FindControl("btnSel"); if(chkTemp.Checked)
    {
    checkNum+=1;
    btnTemp.Text = "修改" ;
    }
    }
    }
      

  5.   

    Protected Sub saveNumberNull()
            Dim obj As New Object
            For Each obj In Page.Controls(1).Controls
                If TypeOf obj Is TextBox Then
                    If obj.Text = "" Then
                        obj.Text = "-1"
                    End If
                    If Not IsNumeric(obj.Text) Then
                        obj.Text = "-1"
                    End If
                End If
            Next
        End Sub这个是我现在用的 没问题的 就是判断如果textbox为空就负值-1