各位好,請教一下
SqlDataSource 有辦法重新整理嗎 例如
<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
 ConnectionString="<%$ ConnectionStrings:xxx_DBConnectionString %>"  
 SelectCommand="SELECT * FROM [stu] ORDER BY NEWID()"></asp:SqlDataSource> 
	 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"  
 AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"  
 Visible="False" Width="758px"> 
我是用GridView1 去亂數顯示所有學生資料
那有辦法按個鈕就再重跑一次sql 讓他重新亂數抓嗎(除了視窗重啟外有無其他辦法呢) 我是用 c#
Q : 按個鈕就再重跑一次sql 讓他重新.....
A : 請在您按下按鈕時
重新DataBinding
例如: GridView1.DataSourceID = "SqlDataSource1"
或是另一種寫法(比較一下,跟上面有何不同)
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
ASPX 頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <!-- 宣告 SqlDataSource 並設定 SelectCommand -->
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:xxx_DBConnectionString %>" 
                SelectCommand="SELECT * FROM [stu] ORDER BY NEWID()">
            </asp:SqlDataSource>
                    <!-- 使用 GridView 顯示資料 -->
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
            Visible="False" Width="758px">
        </asp:GridView>
        <!-- 宣告按鈕,在按鈕的 click 事件處理方法中會重新綁定資料 -->
        <asp:Button ID="Button1" runat="server" Text="重新整理" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>
C# 程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        // 在頁面載入時綁定資料
        SqlDataSource1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            // 在按鈕的 click 事件處理方法中重新綁定資料
            SqlDataSource1.DataBind();
        }
    }
}