How to get TotalRowCount from LinqDataSource to Literal?

I have a LinqDataSource that I am using to calculate the number of rows in a table. I would like to update the value of a literal with a number with the following code taken from MSDN ( linqdatasourcestatuseventargs.totalrowcount.aspx ):

protected void linqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    Literal1.Text = e.TotalRowCount.ToString();
}

      

How can I trigger a select event on a data source? The SqlDataSource class has a Select () method so it can be invoked programmatically, for example. Page_Load, but LinqDataSource doesn't have this method. I currently solved the problem by binding my datasource to an empty FormView, but that is too ugly.

I'm sure there is a much better way to get the total number of lines in my literal when using LinqToSql, I just don't know how.

Tvanfosson's suggestion to attach a method to the selected event of the datasource unfortunately doesn't solve my problem because the select event still doesn't fire on page load. (I have already bound the _Selected method using the OnSelected attribute, by the way, like this)

<asp:LinqDataSource ID="linqDataSource1" runat="server"
    OnSelected="linqDataSource1_Selected">

      

0


source to share


2 answers


I ended up losing the datasource and putting code in code instead. This is not really what I was going to do, but still pretty short. I look something like this:



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var context = new MyDataContext();

        numberOfModificationsLiteral.Text =
            (
                from modification in context.Modifications
                where modification.Start >= DateTime.Now
                select modification
            ).Count().ToString();
    }
}

      

0


source


Connect your method as an event handler for the selected event in Page_Load.



public void Page_Load( object sender, EventArgs e )
{
     linqDataSource1.Selected += LinqDataSource1_Selected;
}

protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    Literal1.Text = e.TotalRowCount.ToString();
}

      

+3


source







All Articles