In Asp.net 2.0, the gridview is bound to the sqldatasource control. How can we get the number of records returned by sqldatasource?
For example, in the sqldatasource control, how does one return the number of record rows? In. NET 2.0, you can use
Onselected event implementation, and set the affectedrows attribute in the select event sqldatasourcestatuseventargs parameter.
Code As follows:
... (The beginning part is omitted)
<SCRIPT runat = "server">
Protected void sqldatasourceincluselected (Object sender, sqldatasourcestatuseventargs E)
{
Totalrows. Text = E. affectedrows. tostring ();
}
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" datakeynames = "customerid"
Datasourceid = "sqldatasource1" allowpaging = "true">
<Columns>
<Asp: boundfield datafield = "customerid" headertext = "customerid" readonly = "true" sortexpression = "customerid"/>
<Asp: boundfield datafield = "companyName" headertext = "companyName" sortexpression = "companyName"/>
<Asp: boundfield datafield = "contactname" headertext = "contactname" sortexpression = "contactname"/>
</Columns>
</ASP: gridview>
<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "Data Source = (local); initial catalog = northwind; user id = sa; Password = 123456 ;"
Providername = "system. Data. sqlclient" selectcommand = "select [customerid], [companyName], [contactname] from [MERs]" onselected = "sqldatasource1_selected">
</ASP: sqldatasource>
<Asp: Label id = "label1" runat = "server" text = "number of total rows:"> </ASP: Label>
<Asp: Label id = "totalrows" runat = "server" text = "label"> </ASP: Label>