当前位置:编程学习 > C#/ASP.NET >>

初学请教form表单问题

各位路过的大侠:我建了个htm页面:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.htm.cs" Inherits="index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form  method="post" action="index.aspx">
    <div>
    
        <table style="width:199px" align="center">
            <tr>
                <td align="center">
                    <input id="Text1" type="text" /></td>
            </tr>
            <tr>
                <td align="center">
                    <input id="Text2" type="text" /></td>
            </tr>
            <tr>
                <td align="center">
                    <input id="Button1" type="button" value="button" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


相应的index.aspx.cn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username = Request.Form["TextBox1 "].ToString();       
        string passwd = Request.Form["TextBox2"].ToString();
               SqlConnection con = new SqlConnection("server=.:database=ll;uid=sa;pwd=lvli19840414;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from ll where username='" + username + "'and passwd='" + passwd + "'", con);
        int count = Convert.ToInt32(cmd.ExecuteScalar());
        if (count > 0)
        {
            Response.Redirect("main.aspx");
        }
        else
        {
            Response.Redirect("loginfale.htm");
        }
    }
}
从htm页面浏览怎么点击botton怎么一点反应都没有啊, --------------------编程问答--------------------  <input id="Button1" type="button" value="button" />

改成
<input id="Submit1" type="submit" value="submit" />
--------------------编程问答-------------------- 你的代码有很多问题。 --------------------编程问答--------------------
引用 1 楼 liuchaolin 的回复:
 <input id="Button1" type="button" value="button" />

改成
<input id="Submit1" type="submit" value="submit" />

+ --------------------编程问答--------------------   <form method="post" runat="server" id="form1"  action="index.aspx">
 <input id="Button1" type="submit" value="button" /> --------------------编程问答-------------------- <input id="Button1" type="submit" value="button" />

还有就是  你加载页面的时候会 经过一次代码  而Request.Form["TextBox1 "] 在第一次加载的时候很可能会为null  这样会报错 建议 加个if (Request.Form["TextBox1 "] != null) --------------------编程问答-------------------- 把 Page_Load里的代码放到按钮的单击事件里面。 --------------------编程问答-------------------- 肯定没有啦 你用的是客户端控件要用服务端的换个BUTTON就行了 --------------------编程问答-------------------- string username = Request.Form["TextBox1 "].ToString();  
string passwd = Request.Form["TextBox2"].ToString();
//你获取的值TextBox1和TextBox2根本找不到
<input id="Text1" type="text" />
//你没有定义html标签的name属性,而且也没有TextBox1啊
<input id="Button1" type="button" value="button" />
//submit才是提交,如果用button按钮必须document.getElementById('form1').submit();获取表单id


<body>
    <form action="Default.aspx" method="post" id="form1">
        <table border="1">
            <tr>
                <td><label for="username">账号</label></td>
                <td><input type="text" name="textBox1" id="username" /></td>
            </tr>
            <tr>
                <td><label for="password">密码</label></td>
                <td><input type="text" name="textBox2" id="password" /></td>
            </tr>
            <tr>
                <td> </td>
                <td><input type="button" value="提交" onclick="document.getElementById('form1').submit()" /></td>
            </tr>
        </table>
    </form>
</body>


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Form["textBox1"];
        string pass = Request.Form["textBox2"];
        string words = string.Format("账号是:{0},密码是:{1}", name, pass);
        Response.Write(words);
    }
}
--------------------编程问答-------------------- submit --------------------编程问答-------------------- 对,submit按钮才会提交按钮,跟服务器控件取消PostBack一样的。可以查看生成的代码。 --------------------编程问答-------------------- 看看!!! --------------------编程问答-------------------- 按照8楼大侠的方法,账号是:,密码是:   读不出字符串 

我自己的改为submit后页面点击按钮,怎么是错误

Server Error in '/WebSite15' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

--------------------编程问答-------------------- 说明其他的代码还有问题 --------------------编程问答-------------------- 八楼正解,不过要转换
 string name = Request.Form["textBox1"].ToString();;
 string pass = Request.Form["textBox2"].ToString();;
如果用Button要在属性中写onclick时间,当然后台触发
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" /> --------------------编程问答--------------------
引用 12 楼 vicki2008 的回复:
按照8楼大侠的方法,账号是:,密码是:   读不出字符串 

我自己的改为submit后页面点击按钮,怎么是错误

Server Error in '/WebSite15' Application.
--------------------------------------------------------------------------------

Object ref……


string username = Request.Form["TextBox1 "].ToString();  
string passwd = Request.Form["TextBox2"].ToString();
//你获取的值TextBox1和TextBox2根本找不到
<input id="Text1" type="text" />
//你没有定义html标签的name属性,而且也没有TextBox1啊
<input id="Button1" type="button" value="button" />
//submit才是提交,如果用button按钮必须document.getElementById('form1').submit();获取表单id

以上是你的问题所在,改成这样试试,或者用我给出的代码和你自己的做比较,看看哪里出问题
<input id="Text1" type="text" name="TextBox2" /> --------------------编程问答-------------------- 疯了,还是搞不定,求大侠qq指导:qq:190404072 --------------------编程问答-------------------- button 就是button  你可以激活submit时间 或者按钮type直接改成提交方式就有反映了
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,