iT邦幫忙

0

請教一下 SqlDataSource 有辦法重新整理嗎

各位好,請教一下
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#

sppboy iT邦新手 5 級 ‧ 2017-01-01 07:54:29 檢舉
放顆按鈕 ,點擊後重新回到這頁 !
或是點擊後→sqldatasource.selectcommand="....."→gridview.databind
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
mis2000lab
iT邦好手 1 級 ‧ 2017-01-01 21:15:42

Q : 按個鈕就再重跑一次sql 讓他重新.....

A : 請在您按下按鈕時
重新DataBinding

例如: GridView1.DataSourceID = "SqlDataSource1"

或是另一種寫法(比較一下,跟上面有何不同)
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

0
JamesDoge
iT邦高手 1 級 ‧ 2022-12-29 01:26:41

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();
        }
    }
}

我要發表回答

立即登入回答