Add a query for sqldatasource/girdview

Source: Internet
Author: User
In vs2005, you can easily bind the SELECT statement to sqldatasource with the gridview, saving a lot Code .
However, it is inconvenient to add more queries.
For example, I want to display all the data in the gridview when I enter the page for the first time, and some of the following query conditions, such as name and email, enter a name point to query the filtered data in the gridview. Previously, this was done: Write query branches in sqldatasource, such
If @ op = 1
Begin
Select .....
End
Else if @ op = 2
Begin
Select ....
End
However, the problem is that you must set the query parameters in sqldatasource in advance. If I want to filter email queries without entering an email, so this is not easy.
The current method is to click query and rewrite the selectcommand of sqldatasource. However, if you want to modify the data, an error will occur, because the fixed selectcommand in sqldatasource will come out again after PostBack. In this way, I want to add a variable to record whether to filter the query or all the queries. However, the variables in Asp.net will be reinitialized at the next PostBack, unless they are set to static, however, when there are multiple users, other users can change the static value, which is equivalent to the global value. In this case, you need to use session or viewstate.
After talking about this, I am confused. If you don't talk about it, post the code to the brothers who encounter the same problem. If you have a better solution, please kindly advise. Protected   Void Page_load ( Object Sender, eventargs E)
{
Lb_tips.visible =   False ;

If ( ! Ispostback)
{
Loadinit ();
}
Dosearch ();
}

Trigger event # Region Trigger event
Private   Void Dosearch ()
{
If (( String ) Viewstate [ " Search " ] =   " 1 " )
{
String Ssql =   " Select db_oa.dbo.oa_user.fld_username, informperson. informpersonid, "
+   " Informperson. informpersonname, informperson. Email, informperson. Mobile, "
+   " Informperson. createuserid from informperson left Outer Join db_oa.dbo.oa_user "
+   " On informperson. createuserid = db_oa.dbo.oa_user.fld_userid " ;
String Swhere =   " Where 1 = 1 " ;
If (Txb_search1.text ! =   "" )
{
Swhere + =   " And informpersonname like "   + Common. quotedstr ( " % "   + Txb_search1.text +   " % " );
}
If (Txb_search2.text ! =   "" )
{
Swhere + =   " And email like "   + Common. quotedstr ( " % "   + Txb_search2.text +   " % " );
}
If (Txb_search_mobile.text ! =   "" )
{
Swhere + =   " And mobile like "   + Common. quotedstr ( " % "   + Txb_search_mobile.text +   " % " );
}

Swhere + =   " Order by informpersonid " ;
Ssql + = Swhere;
Sds_data.selectcommand = Ssql;
Gv_data_list.allowpaging =   False ;
}
Else
{
String Ssql2 =   " Select db_oa.dbo.oa_user.fld_username, informperson. informpersonid, "
+   " Informperson. informpersonname, informperson. Email, informperson. Mobile, "
+   " Informperson. createuserid from informperson left Outer Join db_oa.dbo.oa_user "
+   " On informperson. createuserid = db_oa.dbo.oa_user.fld_userid "
+   " Order by informpersonid " ;
Sds_data.selectcommand = Ssql2;
Gv_data_list.allowpaging =   True ;
}
}
/**//// <Summary>
///Query
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected   Void Btn_dosearch_click ( Object Sender, eventargs E)
{
Viewstate ["Search"]= "1";
Dosearch ();
Gv_data_list.databind ();
}
Protected   Void Btn_search_cancel_click ( Object Sender, eventargs E)
{
Gv_data_list.allowpaging =   True ;
Pn_search.visible =   False ;
Viewstate [ " Search " ] =   " 0 " ;
Dosearch ();
Gv_data_list.databind ();
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.