ASP.net--Web服务端控件

在ashx文件中输出html中的文本框和按钮,并且当单击按钮时,输出文本框中的文字到网页上,同时文本框中的内容保持不变。

html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="hello2.ashx">
<input type="hidden" name="ispostback" value=true />
姓名:<input type="text" name="UserName" value="@value" /><input type="submit" value="提交" />
@msg
</form>

</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// hello2 的摘要说明
    /// </summary>
    public class hello2 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string username = context.Request["UserName"];
            string msg = "";
            string ispostback=context.Request["ispostback"];

            if (ispostback == "true")
            {
                context.Response.Write("提交进入");
                msg = username + "你好!";
            }
            else
            {
                context.Response.Write("直接进入");
                username = "";
                msg = "";
            }
            string fullPath = context.Server.MapPath("hello2.htm");
            string htmlContent = System.IO.File.ReadAllText(fullPath);
            htmlContent= htmlContent.Replace("@value", username);
            htmlContent = htmlContent.Replace("@msg", msg);
            context.Response.Write(htmlContent);
           }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


1 完成登录页面(到记事本文件进行密码和用户名的验证)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] lines=File.ReadAllLines("password.txt");
        string username = txtUserName.Value;
        string password = txtPassWord.Value;
        for (int i = 0; i < lines.Length; i++)
        {
           string[] line=lines[i].Split(' ');
           if (username == line[0] && password == line[1])
           {
               htmllabel1.InnerText = "登陆成功";
               return;
           }
           else
           {
               htmllabel1.InnerText = "登陆失败";
           }
        }
    }
}

2 完成 用户注册页面(将注册信息显示在本页面 div 中)。

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
    <style type="text/css" >
    .table111
    {
        margin:0 auto;
        width:50%;
        border:1px double green;
       
        }
        #Button1
        {
            width: 92px;
        }
    </style>
    <script Xlanguage="javascript" type="text/javascript">
// <![CDATA[

// ]]>
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table class="table1">
    <tr><td>用户名:</td><td><input type="text" id="userNameTxt" runat="server" /> </td></tr>
     <tr><td>密 码:</td><td><input type="text" id="passWordTxt" runat="server" /> </td></tr>
      <tr><td>确认密码:</td><td><input type="text" id="passWord2" runat="server" /> </td></tr>

       <tr><td>性别</td><td><input type="radio" id="radio1" runat="server" name="sex" />男 <input type="radio" id="Radio2" runat="server" name="sex"/>女</td></tr>
  
   <tr><td>头像:</td><td><input type="file" runat="server" id="file1"/> </td></tr>
   <tr><td><input type="button" id="Button1" runat="server" Xonserverclick="btnClick" value="提交" Xοnclick="return Button1_onclick()" />
       <asp:Button ID="Button2" runat="server" Xοnclick="Button2_Click" Text="Button" />
       </td> <td></td></tr>
    </table>
    <p></p>
    <hr />
    <div id="divResult" runat="server">
    </div>

    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class regination : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        StringBuilder sb = new StringBuilder();
        sb.Append("用户名:");
        sb.Append(userNameTxt.Value);
        sb.Append("<br/>");
        sb.Append("密码:");
        sb.Append(passWord2.Value);
        sb.Append("<br/>");
        sb.Append("性别:");
        if (radio1.Checked)
        {
            sb.Append("男");
        }
        else
        {
            sb.Append("女");
        }
        sb.Append("<br/>");

        string filename = this.file1.PostedFile.FileName;

        filename = DateTime.Now.Ticks.ToString() + filename.Substring(filename.LastIndexOf("."));
        this.file1.PostedFile.SaveAs(Server.MapPath("upload") + "\\" + filename);

        sb.Append("头像:");
        sb.Append("<img src=\"upload/" + filename + "\" />");
        this.divResult.InnerHtml = sb.ToString();

    }
  }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值