Webform 內置對象(Response對象、Request對象,QueryString)

来源:http://www.cnblogs.com/dreamer666/archive/2016/09/24/5903317.html
-Advertisement-
Play Games

Response對象:響應請求 Response.Write("<script>alert('添加成功!')</script>"); Response.Redirect("Default.aspx"); Request對象:獲取請求 Request["key"]來獲取傳遞過來的值 QueryStri ...


Response對象:響應請求
Response.Write("<script>alert('添加成功!')</script>");
Response.Redirect("Default.aspx");

Request對象:獲取請求
Request["key"]來獲取傳遞過來的值

QueryString:地址欄數據傳遞 ?key=value&key=value
註意事項:不需要保密的東西可以傳
不要傳過長東西,因為長度有限,過長會造成數據丟失

 

對數據表的增刪改:

Default.aspx中添加用戶

 <input id="btn1" type="button" value="添加用戶" />

        <script>
            document.getElementById("btn1").onclick = function () {
                window.open("Default3.aspx", "_self");
            };

        </script>

 

首先數據訪問類造一個添加方法

public bool Insert(Users u)
    {//添加
        bool isok = false;
        cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f)";
        cmd.Parameters.Clear();
        cmd.Parameters.Add("@a", u.UserName);
        cmd.Parameters.Add("@b", u.PassWord);
        cmd.Parameters.Add("@c", u.NickName);
        cmd.Parameters.Add("@d", u.Sex);
        cmd.Parameters.Add("@e", u.Birthday);
        cmd.Parameters.Add("@f", u.Nation);

        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
            isok = true;
        }
        catch { }
        conn.Close();
        return isok;
    }

 

添加:

<body>


    <form id="form1" runat="server">
        <h1>用戶添加</h1>
    用戶名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
        
        密碼:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><br /><br />
        確認密碼:<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox><br /><br />
        昵稱:<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br /><br />
        性別:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Value="true" Selected="True">男</asp:ListItem>
            <asp:ListItem Value="false">女</asp:ListItem>
        </asp:RadioButtonList><br /><br />
        生日:<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>年<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>月<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList>日<br /><br />
        民族:<asp:DropDownList ID="DropDownList1" runat="server" Width="122px"></asp:DropDownList><br /><br />
       &nbsp &nbsp &nbsp &nbsp <asp:Button ID="Button1" runat="server" Text="註 冊" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        

    
    </form>



</body>

密碼JS驗證

<script type="text/javascript">
        window.onload = function () {/*document操作取出密碼框里內容*/
            document.getElementById("Button1").onclick = function () {
                var pwd1 = document.getElementById("TextBox3").value;
                var pwd2 = document.getElementById("TextBox4").value;
               /* alert(pwd1);檢測一下*/
               /* alert(pwd2);*/
                if (pwd1 != pwd2) {
                    document.getElementById("Label2").innerText = "兩次密碼輸入不一致";
                    return false;/*密碼不一阻止刷新,一樣就刷新*/
                }
            };
        };

    </script>

    <style type="text/css">
        #Label2 {
        
        color:red;/*Label2里所呈現的文字顯示紅色*/
        }

    </style>
</head>

 

 

性別預設選中,生日需三個DropDownList

<asp:ListItem Value="true" Selected="True">男</asp:ListItem>
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)//數據綁定
        {
            for (int i = DateTime.Now.Year; i >= 1900; i--)
            {
                //添加年
                ListItem li = new ListItem(i.ToString(),i.ToString());
                DropDownList2.Items.Add(li);
            }

            for (int i = 1; i <= 12; i++)
            {
                //
                ListItem li = new ListItem(i.ToString(), i.ToString());
                DropDownList3.Items.Add(li);
            }

            for (int i = 1; i <= 31; i++)
            {
                //
                ListItem li = new ListItem(i.ToString(), i.ToString());
                DropDownList4.Items.Add(li);
            }

            //取出民族的數據
            DropDownList1.DataSource = new NationDA().Select();
            DropDownList1.DataTextField = "NationName";
            DropDownList1.DataValueField = "NationCode";
            DropDownList1.DataBind();
        }
         Button1.Click += Button1_Click;//事件委托
    }

void Button1_Click(object sender, EventArgs e)
{
//1、構建一個Users對象
Users u = new Users();
u.UserName = TextBox1.Text;
u.PassWord = TextBox3.Text;
u.NickName = TextBox4.Text;
u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value);
string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue;
u.Birthday = Convert.ToDateTime(date);
u.Nation = DropDownList4.SelectedItem.Value;


//2、將此對象添加到資料庫去
bool ok = new UsersData().Insert(u);


//3、提示添加成功
if (ok)
{
Response.Write("<script>alert('添加成功!')</script>");
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script>alert('添加失敗!')</script>");
}


//4、關閉此頁面,刷新展示頁面
}
}

 

二、刪除

操作,在default.aspx主頁數據顯示中添加一列,點刪除,打開新的網頁delete.aspx執行代碼後關閉,刷新主頁面

<td>操作</td>

   <td><a href="delete.aspx"?un=<%#Eval("UserName")
>刪除</a></td>
//新網頁中執行的刪除代碼
//1、獲取要刪除的主鍵值,username,做刪除的方法
        string Uname = Request["un"].ToString(); 獲取請求

        //2、刪除
        new UsersDA().Delete(Uname);
        //3、調回Main頁面
        Response.Redirect("Main.aspx");

三、修改

新建窗體xiugai.aspx  數據展示頁面添加一列修改,點擊進入xiugai.aspx

<td><a href="#">修改</a></td>

數據操作類添加方法:

public bool Update(Users u)
    {
        bool isok = false;
        cmd.CommandText = "update Users set PassWord=@b,NickName=@c,Sex=@d,Birthday=@e,Nation=@f where UserName=@a";
        cmd.Parameters.Clear();
        cmd.Parameters.Add("@a", u.UserName);
        cmd.Parameters.Add("@b", u.PassWord);
        cmd.Parameters.Add("@c", u.NickName);
        cmd.Parameters.Add("@d", u.Sex);
        cmd.Parameters.Add("@e", u.Birthday);
        cmd.Parameters.Add("@f", u.Nation);

        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
            isok = true;
        }
        catch { }
        conn.Close();
        return isok;
    }
//1步、構建一個Users對象
        Users u = new Users();
        u.UserName = Label1.Text;

        if (TextBox3.Text == "" && TextBox4.Text == "")
        {//判斷密碼的
            u.PassWord = pwd;
        }
        else
        {
            u.PassWord = TextBox3.Text;
        }

        u.NickName = TextBox6.Text;
        u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value);
        string data = DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue + "-" + DropDownList4.SelectedValue;
        u.Birthday = Convert.ToDateTime(data);
        u.Nation = DropDownList1.SelectedItem.Value;

        //2步、將此對象添加到資料庫去,先在UserDA里修改方法
        bool ok = new UsersDA().Update(u);
        //3步、提示修改成功
        if (ok)
        {
            Response.Write("<script>alert('修改成功!')</script>");
          4步、Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>");
            //Response.Redirect("Main.aspx");//重定項

        }
        else
        {
            Response.Write("<script>alert('修改失敗!')</script>");
        }
        //4、關閉此頁面,刷新展示頁面
        //用JS寫
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Linq to JSON是用來操作JSON對象的.可以用於快速查詢,修改和創建JSON對象.當JSON對象內容比較複雜,而我們僅僅需要其中的一小部分數據時,可以考慮使用Linq to JSON來讀取和修改部分的數據而非反序列化全部. 二.創建JSON數組和對象 在進行Linq to JSON之前,首 ...
  • 一、內置對象 (一)Response對象 1、簡介:response 對象在ASP中負責將信息傳遞給用戶.Response對象用於動態響應客戶端請求,並將動態生成的響應結果返回到客戶端瀏覽器中,使用Response對象可以直接發送信息給瀏覽器,重定向瀏覽器到另一個URL或設置cookie的值等. 2 ...
  • 很久沒在博客園寫文章了,打算把一直由自己一個人寫的一整套系統開放出來,今天先放一些截圖及可以演示的地址! 這套系統包含數據層(HB.Data)、計劃任務(HB.PlanTask)、日誌系統(HB.Log)、模版系統(HB.TemplateEngine)、Url重寫(HB.UrlRewriter)、a ...
  • 類型參數使得設計類和方法時,不必確定一個或多個具體參數,其的具體參數可延遲到客戶代碼中聲明、實現。 這意味著使用泛型的類型參數T,寫一個類MyList<T>,客戶代碼可以這樣調用:MyList<int>, MyList<string>或 MyList<MyClass>。 這避免了運行時類型轉換或裝箱 ...
  • 一、說明 1) 這個類 是我 在真實項目中,優化解決真實問題 時,不參考第三方代碼,完全由自己查閱MSDN官方文檔 , 完成的一個真實生產環境中使用的功能類 2) 讀者在使用此類時,請尊重原創,在代碼中加上原創註釋:// Author -- Meng.NET (cnblogs.com) ,同時歡迎 ...
  • 內置對象: Response對象:響應請求 Response.Write("<script>alert('添加成功!')</script>"); Response.Redirect("Default.aspx"); Request對象:獲取請求 Request["key"]來獲取傳遞過來的值 Que ...
  • 回到目錄 我們知道mvc里有一些過濾器,AuthorizeAttribute用來做授權,一般在用戶授權方面可以使用它,當使用沒有登陸,我們直接跳到登陸頁,這是沒有問題的,可我要說的是,當用戶對某個Action沒有許可權時,如何禁止對當前action的執行,這個聽起來很不可思議,因為我們一般感覺,當Au ...
  • httpModules是往當前應用程式添加HttpModule(http模塊)的標簽。配置節如下 提起httpModule不得不提一下Http請求處理流程 ASP.NET對請求處理的過程: 當請求一個*.aspx文件的時候,這個請求會被inetinfo.exe進程截獲,它判斷文件的尾碼(aspx)之 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...