locked
SelectCommand RRS feed

  • Question

  • User-925002149 posted

    Hi 

    I'm displaying 4items on a datalist using the SqlDataSource

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:News %>"

    SelectCommand="SELECT TOP 4[Id], [Date], [Title], [PictureUrl], [Description],[VideoUrl], [Author], [Type] FROM [News] ORDER BY [Date] DESC , [Id] DESC">
    </asp:SqlDataSource>

    I would like to use a button which will enables the Datalist to display 8items.

    Any idea about how to do this?

    protected void MoreTitle_Click(object sender, EventArgs e)
    {
    // any codes here?
    }

    Monday, November 26, 2012 1:52 PM

Answers

  • User3866881 posted

    But I want to have a button "More Titles" when clicked on this button, the Datalist will display 8Items instead of 4items

    Hello,

    I think since you wanna dynamically change your SQL statement——you have to dynamically databind to the data presentation control by referring this below:

    1)Remove using DataSource first.

    2)Then you should dynamically data-binding in the button's click:

    using(SqlDataAdapter adapter = new SqlDataAdapter("select TOP 8 * from xxx","connection string here"))
    {
       DataTable dt = new DataTable();
       adapter.Fill(dt);
       //Do binding……
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 27, 2012 1:49 AM

All replies

  • User-718146471 posted

    Well, the problem is with your query itself. You are asking for only the top four records. Why do you want 4 empty rows in your datalist? If you want 8, then SELECT TOP 8. Am I understanding you correctly or are you trying to do something else?

    Monday, November 26, 2012 2:00 PM
  • User-925002149 posted

    something else

    for the moment i'm displaying the 2items without any problem.

    But I want to have a button "More Titles" when clicked on this button, the Datalist will display 8Items instead of 4items

    Monday, November 26, 2012 2:24 PM
  • User3866881 posted

    But I want to have a button "More Titles" when clicked on this button, the Datalist will display 8Items instead of 4items

    Hello,

    I think since you wanna dynamically change your SQL statement——you have to dynamically databind to the data presentation control by referring this below:

    1)Remove using DataSource first.

    2)Then you should dynamically data-binding in the button's click:

    using(SqlDataAdapter adapter = new SqlDataAdapter("select TOP 8 * from xxx","connection string here"))
    {
       DataTable dt = new DataTable();
       adapter.Fill(dt);
       //Do binding……
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 27, 2012 1:49 AM