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

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

QueryString:地址欄數據傳遞 ?key=value&key=value
注意事項:不須要保密的東西能夠傳
不要傳過長東西,由於長度有限,過長會形成數據丟失javascript

 

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

 

對數據表的增刪改:java

Default.aspx中添加用戶數據庫

複製代碼
 <input id="btn1" type="button" value="添加用戶" />

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

        </script>
複製代碼

 

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

複製代碼
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;
    }
複製代碼

 

添加:spa

複製代碼
<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驗證code

複製代碼
<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>
複製代碼

 

 

性別默認選中,生日需三個DropDownListorm

<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)
{
//一、構建一個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;server

 
 

//二、將此對象添加到數據庫去
bool ok = new UsersData().Insert(u);對象

 
 

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

 
 

//四、關閉此頁面,刷新展現頁面
}
}

 
複製代碼

2、刪除

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

<td>操做</td>

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

        //二、刪除
        new UsersDA().Delete(Uname);
        //三、調回Main頁面
        Response.Redirect("Main.aspx");
複製代碼

3、修改

新建窗體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>");
        }
        //四、關閉此頁面,刷新展現頁面
        //用JS寫
    }
}
複製代碼
相關文章
相關標籤/搜索