2024年3月29日 星期五

[研究]ASP.NET,WebForm,要傳遞 SqlDataSource1.SelectParameters 當時的值

[研究]ASP.NET,WebForm,要傳遞 SqlDataSource1.SelectParameters 當時的值

2024-03-29

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

這是傳遞 DefaultValue

Dictionary<string, string> parameters = new Dictionary<string, string>();
foreach (Parameter parameter in SqlDataSource1.SelectParameters)
{
    parameters.Add(parameter.Name, parameter.DefaultValue);
}


下面是傳遞當時的值

Default.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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>A2</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>" 
            SelectCommand="SELECT * FROM [MyTable] WHERE Field2=@Field2">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Field2" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
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)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            foreach (Parameter parameter in SqlDataSource1.SelectParameters)
            {
                string value = "";

                if (parameter is ControlParameter)
                {
                    ControlParameter controlParam = (ControlParameter)parameter;
                    if (controlParam.ControlID != "")
                    {
                        Control control = Page.FindControl(controlParam.ControlID);
                        if (control != null && control is TextBox)
                        {
                            value = (control as TextBox).Text;
                        }
                        if (control != null && control is DropDownList)
                        {
                            value = (control as DropDownList).Text;
                        }
                    }
                }
                else if (parameter is FormParameter)
                {
                    FormParameter formParam = (FormParameter)parameter;
                    value = HttpContext.Current.Request.Form[formParam.FormField];
                }
                else if (parameter is QueryStringParameter)
                {
                    QueryStringParameter queryParam = (QueryStringParameter)parameter;
                    value = HttpContext.Current.Request.QueryString[queryParam.QueryStringField];
                }
                else if (parameter is SessionParameter)
                {
                    SessionParameter sessionParam = (SessionParameter)parameter;
                    value = HttpContext.Current.Session[sessionParam.SessionField]?.ToString();
                }
                else if (parameter is CookieParameter)
                {
                    CookieParameter cookieParam = (CookieParameter)parameter;
                    value = HttpContext.Current.Request.Cookies[cookieParam.CookieName]?.Value;
                }
                else if (parameter is ProfileParameter)
                {
                    ProfileParameter profileParam = (ProfileParameter)parameter;

                    //error CS1503: 引數 1: 無法從 'System.Web.HttpContext' 轉換成 'string'
                    //value = ProfileBase.Create(Context, true)[profileParam.PropertyName]?.ToString();

                    //error CS1503: 引數 1: 無法從 'System.Web.HttpContext' 轉換成 'string'
                    //value = ProfileBase.Create(HttpContext.Current, true)[profileParam.PropertyName]?.ToString();

                    //error CS1503: 引數 1: 無法從 'System.Web.HttpContext' 轉換成 'string'
                    //ProfileBase profile = ProfileBase.Create(HttpContext.Current);
                    //value = profile.GetPropertyValue(profileParam.PropertyName)?.ToString();

                    ProfileBase profile = HttpContext.Current.Profile;
                    value = profile.GetPropertyValue(profileParam.PropertyName)?.ToString();
                }
                else if (parameter is ControlParameter)
                {
                    ControlParameter controlParam = (ControlParameter)parameter;
                    value = (controlParam.ControlID != "") ? ((WebControl)Page.FindControl(controlParam.ControlID))?.Attributes[controlParam.PropertyName] : "";
                }
                else if (parameter is Parameter)
                {
                    value = parameter.DefaultValue;
                }

                parameters.Add(parameter.Name, value);
            }
        }
    }
}

實際測試成功。

(完)

相關

[研究]ASP.NET,WebForm,要傳遞 SqlDataSource1.SelectParameters 當時的值https://shaurong.blogspot.com/2024/03/aspnetwebform-sqldatasource1selectparam.html

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出資料庫某資料表成 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-freedataexports-119-ods_29.html

[研究]ASP.NET,WebForm,用 FreeDataExports 1.1.9 匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-freedataexports-119-ods.html

[研究]ASP.NET,WebForm, 把 OdsReadWrite.cs 從 Ionic.Zip 或 DotNetZip 改成用 System.IO.Compression
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreadwritecs-ioniczip.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 共用匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_79.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 匯出 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_23.html

[研究]ASP.NET,WebForm,用 OdsReaderWriter + DotNetZip (Ionic.Zip) 1.16.0 匯入 .ods
https://shaurong.blogspot.com/2024/03/aspnetwebform-odsreaderwriter-dotnetzip_93.html

How to Read and Write ODF/ODS Files (OpenDocument Spreadsheets) - CodeProject
https://www.codeproject.com/Articles/38425/How-to-Read-and-Write-ODF-ODS-Files-OpenDocument-2

[研究][ASP.NET]讀取、匯出、寫入、匯入 Excel .xlsx .xls ODF .ods
https://shaurong.blogspot.com/2020/04/caspnetwebform-excel-xlsx-xls-odf-ods.html

[研究][ASP.NET]使用 OdsReaderWriter + DotNetZip 1.15.0 (Ionic.Zip) 匯出寫入 .ods
https://shaurong.blogspot.com/2021/02/aspnet-odsreaderwriter-dotnetzip-1150_9.html

[研究][ASP.NET]使用 OdsReaderWriter + DotNetZip 1.15.0 讀取匯入 .ods 到資料庫
https://shaurong.blogspot.com/2021/02/aspnet-odsreaderwriter-dotnetzip-1150.html

[研究][C#]用OdsReaderWrite匯出(寫入)、匯入(讀取) ODF/.ods 檔案 (使用 Ionic.Zip、DotNetZip)
https://shaurong.blogspot.com/2020/09/caspnetwebform-ioniczip-odfods.html

[研究] [C#] 用OdsReadWrite讀取 LibreOffice、OpenOffice 的試算表(Calc) 檔案 (.ods)(使用 .NET 4.5 的 ZipFile)
https://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods-net-45.html

[研究] [C#] 用OdsReaderWrite讀取 LibreOffice 5.1.6 的試算表(Calc) 檔案 (.ods)(使用 DotNetZip)
https://shaurong.blogspot.com/2016/12/c-libreoffice-516-calc-ods.html

NuGet Gallery | OdsReaderWriter 1.0.10
https://www.nuget.org/packages/OdsReaderWriter

sortings/OdsReadWrite at master · Luviz/sortings · GitHub
https://github.com/Luviz/sortings/tree/master/OdsReadWrite

沒有留言:

張貼留言