当前位置: 代码迷 >> C# >> C#中TextBox控件跟数据库查询之间的连接
  详细解决方案

C#中TextBox控件跟数据库查询之间的连接

热度:21   发布时间:2016-05-05 04:18:08.0
C#中TextBox控件和数据库查询之间的连接
循环遍历页面内的所有TextBox控件,将TextBox文本框的内容作为查询条件,跳过空值的TextBox。后台代码该怎么写。
不要单个的判断,万一页面内有100个TextBox控件,单个写的话岂不是太麻烦了,最好运用类似 foreach (Control c in this.Controls)这种循环方法。
附上页面设计图,哪位大虾能帮忙写个示例出来。注意编程语言是C#

------解决思路----------------------
引用:
在线等啊,大虾们在哪里啊

foreach (Control c in this.Controls)

-》
这不是你都知道  怎么写么?if判断不就行了。
string  filter ="";
if((c is TextBox) && (c is TextBox).Text!=""){
    TextBox txt = c as TextBox;
     filter +=  txt.Text;
}
可能有错,手动打的。
------解决思路----------------------


string strWhere = string.Empty;
        foreach (Control cur in Controls)
        {
            if (cur is TextBox )
            {
                TextBox tb = (TextBox)cur;
                if(!string.IsNullOrEmpty(tb.Text.Trim())){
                    strWhere += tb.ID + "='" + tb.Text + "' and ";
                }
            }
        }
        strWhere += " 1 = 1";

你试试看,我没测过 ...
  相关解决方案