How to Use variables to assign values to data source control parameters, dynamically increase or decrease data source control parameters, and support Paging

Source: Internet
Author: User

I. How to assign values to Data Source Control Parameters Using Variables
In vs2005, the data source control has six parameter types: , , , , , In actual programming, variables are often used to assign values to parameters of the data source control. How can we assign values to variables?
The method is as follows:
1. Write a query statement with parameters in the front-end code:
<Asp: sqldatasource id = "sqldatasourcedown" runat = "server" connectionstring = "<% $ connectionstrings: articleconnectionstring %>"
Selectcommand = "select top (15) ID, name, type from kejian where (isfabu = 1) and (kemuid = @ kemuid) and (lianjiid = @ lianjiid) order by downn DESC ">
<Selectparameters>
<Asp: parameter name = "kemuid"/>
<Asp: parameter name = "lianjiid"/>
</Selectparameters>
</ASP: sqldatasource>
2. Add the following statement to the background code:
Int kemuid = 0;
Int lianjiid = 0;
Protected void page_load (Object sender, eventargs E)
{
Int id = convert. toint32 (request. querystring ["ID"]);
String SQL = "select kemuid, lianjiid from kejian where id =" + ID;
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["articleconnectionstring"]. connectionstring ))
{
Using (sqlcommand command = new sqlcommand (SQL, connection ))
{
Connection. open ();
Using (sqldatareader reader = command. executereader ())
{
If (reader. Read ())
{
Kemuid = (INT) Reader ["kemuid"];
Lianjiid = (INT) Reader ["lianjiid"];
}
}
}
}
This. sqlperformancedown. selectparameters. Clear ();
This. sqldatasourcedown. selectparameters. Add ("kemuid", system. typecode. int32, kemuid. tostring ());
This. sqldatasourcedown. selectparameters. Add ("lianjiid", system. typecode. int32, lianjiid. tostring ());
}
Pay attention to the red part in the front and back-end code. The parameter names in the front and back-end code must be consistent. You can assign values to parameters using variables.
Is it very simple and convenient?
2. How to dynamically add or remove data source control parameters and support Paging
Suitable for environment:
The search. ASPX page accepts two query variables: Kemu and name. When Kemu is set to 0, the system queries all subjects. When Kemu is set to 0, the system queries all subjects! If this parameter is set to 0, you must query the results of the previous record by page.
Implementation Method:
Add the following to the front-end code:
<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "<% $ connectionstrings: articleconnectionstring %>"
Selectcommand = "select kejian. ID, kejian. name, kejian. huanjing, kejian. type, kejian. downn, kejian. isfabu, kejian. stars, kejian. size, kejian. kemuid, kejian. updowntime, Kemu. name as Kemu, lianji. name as lianji from kejian inner join Kemu on kejian. kemuid = Kemu. id inner join lianji on kejian. lianjiid = lianji. ID where (kejian. name like n' % '+ @ name + N' %') and (kejian. kemuid = @ Kemu) and (kejian. isfabu = 1) Order by kejian. updowntime DESC ">
<Selectparameters>
<Asp: parameter name = "name"/>
<Asp: parameter name = "Kemu"/>
</Selectparameters>
</ASP: sqldatasource>
Add the following to the background code:
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Gridviewdatabind (convert. toint32 (request. querystring ["Kemu"]), request. querystring ["name"]);
}
}

Private void gridviewdatabind (INT Kemu, string name)
{
String SQL = "select kejian. ID, kejian. name, Kemu. name as Kemu, lianji. name as lianji, kejian. huanjing, kejian. type, kejian. downn, kejian. stars, kejian. size, kejian. kemuid ";
SQL + = "from kejian inner join Kemu on kejian. kemuid = Kemu. ID inner join lianji on kejian. lianjiid = lianji. ID ";
SQL + = "where (kejian. isfabu = 1 )";

If (Kemu = 0)
{
SQL + = "and (kejian. name like n' % '+ @ name + N' %') order by kejian. updownuserid DESC ";
This. sqldatasource1.selectparameters. Clear ();
This. sqldatasource1.selectparameters. Add ("name", system. typecode. String, name );
}
Else
{
SQL + = "and (kejian. kemuid = @ Kemu) and (kejian. name like n' % '+ @ name + N' %') order by kejian. updownuserid DESC ";
This. sqldatasource1.selectparameters. Clear ();
This. sqldatasource1.selectparameters. Add ("Kemu", system. typecode. String, Kemu. tostring ());
This. sqldatasource1.selectparameters. Add ("name", system. typecode. String, name );
}

Sqldatasource1.selectcommand = SQL;
}

Protected void gridview1_pageindexchanged (Object sender, eventargs E)
{
Gridviewdatabind (convert. toint32 (request. querystring ["Kemu"]), request. querystring ["name"]);
}

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.