开发者

i have got a validation problem!

开发者 https://www.devze.com 2023-03-05 15:09 出处:网络
i want to validate the control myself. So i put a label and a condition when i press the button. protected void sendButton1_Click(object sender, EventArgs e)

i want to validate the control myself. So i put a label and a condition when i press the button.

 protected void sendButton1_Click(object sender, EventArgs e)
{
    if (QuestionDetailsTextBox2.Text.Length > 5000)
    {
        QuestionDetailsTextBox2.Text = "You cant enter more than 5000 characters";
    }
    else if(QuestionTextBox1.Text.Length > 100)
    {
        QuestionDetailsTextBox2.Text = "You cant enter more than 100 characters";
    }
    else if (checkValidation())
    {

        QuestionTextBox1.BorderColor = System.Drawing.Color.Red;
    }
    else
    {
        Response.Redirect("AnswerQuestion.aspx");
    }
}

i added a regular expression validater. But i also did this:

  protected void topicDropDownMenu_SelectedIndexChanged1(object sender, EventArgs e)
{
    SubTopicDropDownList.Items.Clear();

    string[] chosenItem = topic[topicDropDownMenu.SelectedItem.Value];

    foreach (string item in chosenItem)
    {
        SubTopicDropDownList.Items.Add(item);
    }

}

Again, i need to press the button twice so th开发者_StackOverflow中文版at it would redirect me :(... ajax is still necessary?


If you want an immediate response you will have to move your validation logic to the client. This usually means you write your validation logic in javascript and call it from the client side click event of the button or change event of the textbox before processing the postback events.

Doing your checks in code behind requires a round trip to the server. Whether you do it this way and postback the full page or use ajax and do a partial postback, there is a delay while the data is sent to the server and the client waits for a response. How big of a delay depends on how much data needs to be transferred and network conditions. This is why ajax is generally faster, it gets to send smaller batches of data.

However you handle the client side validation, it is generally considered a best practice to repeat the validation logic in the code behind. This is to catch any instances where a user has client side scripting disabled.


You are looking for a Partial Page Update.

You can use AJAX and UpdatePanel.

0

精彩评论

暂无评论...
验证码 换一张
取 消