首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoPostBack和RequiredFieldValidator行为

AutoPostBack和RequiredFieldValidator行为
EN

Stack Overflow用户
提问于 2015-12-15 20:17:47
回答 2查看 1.1K关注 0票数 0

我有一个带有AutoPostBack属性的用户名文本框,RequiredFieldValidator.The的问题是,当我在文本框中键入某些内容,然后从文本框中取出时,就会触发AutoPostBack,如果我返回文本框并删除我键入的内容,然后从文本框中出来,表单将显示消息“用户名是必需的”,但它只是闪烁一秒钟,然后页面刷新。

我不明白这种行为。我是否可以以某种方式更改表单,以使“用户名是必需的”消息保留下来,或者它根本不会闪现。如果我没有输入文本框就从文本框中出来,什么也不会发生(我认为AutoPostBack也不会)。

另一个问题是一样的,但我想我的怀疑有点不同:

如果文本框为空并按选项卡或实际触发RequiredFieldValidator的内容,为什么不触发RequiredFieldValidator

如果RequiredFieldValidator无效,为什么仍然会发生AutoPostBack,那么在页面生命周期之前是否检查验证器?

代码语言:javascript
复制
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Registration_LoginPortel
{
public partial class RegistrationPage : System.Web.UI.Page
{
    int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {



    }
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        lblLoginAvailable.Visible = false;
        Response.Write("PPpostback" + (++i));
        if (!CheckLogin(Usn.Text.ToString().Trim()))
        {
            //Register the user
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                con.Open(); string s = Usn.Text;
                string sql_insertQuery = "INSERT INTO UserData(username,password,country)values (@UName,@UPass,@UCountry)";
                //string sql_insertQuery = "INSERT into UserData(username,password,country) VALUES ('"+Usn.Text+"','"+pass.Text+"','"+country.Text+"')";
                SqlCommand com = new SqlCommand(sql_insertQuery, con);
                com.Parameters.AddWithValue("@UName", Usn.Text);
                com.Parameters.AddWithValue("@UPass", pass.Text);
                com.Parameters.AddWithValue("@UCountry", country.Text);

                com.ExecuteNonQuery();
                // Response.Redirect("Admin.aspx");
                Response.Write("Registration is successfull");
                con.Close();
            }
            catch (Exception ex)
            {
                //Response.Write("Error : " + ex.Message);
                Response.Write("\n\nError : " + ex.ToString());
            }
        }
        else
        {
            lblLoginAvailable.Visible = true;
            lblLoginAvailable.Text = "This login already exists in our system. Chooce another login please";
        }
    }
    protected bool CheckLogin(string login)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("select count(*) from UserData where lower(username) = lower(@login)", con);
        cmd.Parameters.Add("@login", SqlDbType.VarChar).Value = login;
        string id = "";
        try
        {
            con.Open();
            id = (int)cmd.ExecuteScalar() == 0 ? "" : cmd.ExecuteScalar().ToString();
        }
        catch (Exception ex)
        {
            //...
        }
        finally
        {
            con.Close();
        }
        if (String.IsNullOrEmpty(id)) return false;
        return true;
    }


    protected void Usn_TextChanged(object sender, EventArgs e)
    {
        lblLoginAvailable.Visible = false;

    }

}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-16 00:33:51

您可以只需删除所需的字段验证程序,您的按钮代码将是:

代码语言:javascript
复制
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return checkvalue();" OnClick="Button_Click" />

checkvalue()是一个javascript方法,您将使用该方法检查textbox的值。

代码语言:javascript
复制
<script type="text/javascript">
    function checkvalue(){
        var txt = document.getElementById("yourTextboxId");
        if (txt.value.length == 0){
            alert('Insert a data');
            txt.focus();
            return false;
        }
        return true;
    }
</script>

只有当用户单击按钮时,才会出现此脚本,它不会触发回发。此脚本将检查文本框内的文本长度,如果长度为0,即不输入文本,将要求用户输入文本,光标将自动设置为textbox控件。

希望它能帮上忙

票数 0
EN

Stack Overflow用户

发布于 2015-12-15 21:37:43

这个页面是回发的,所以回到服务器上,它就像在和你的RequiredFieldValidator搏斗。在后台,您的RequiredFieldValidator将Javascript传递到页面,该页面将执行其魔术,并检查它是否需要显示消息。当回发事件触发时,它会回发整个页面,并在返回时重新加载页面,从而丢失UI上的jscript消息。

我建议不要对这样的东西使用自动备份,因为它会给服务器带来不必要的负载。你到底想让那件事做什么?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34298600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档