开发者

ASP.Net - need to click a button from the code-behind so I can pop an error message

Inherited an asp.net 3.5 C# site. Main page has a control in it that has a datagrid开发者_如何学Go-if the user fails to put a file name and hits 'update', it's wrong-they have to supply a file name. They also have some custom business logic that I nicely have in the code behind. Any of these could mean the user needs to see an error at which point I cancel out of the SqlDataSource update, thus, reverting the grid back to its original value, fine...however, if I put an error message up in a text box(already did that), now, the user is staring at an error msg...I'd rather pop up a warning / error, and then it's all fixed.

SO.....I have the error in a hiddenfield...now, how can I force a click of a hidden button so I can alert the client?


The following is the code which validates the user input at code behind, and prompt a message if invalid.

// ASPX page
<asp:TextBox runat="server" ID="FileNameTextBox"></asp:TextBox>
<asp:Button runat="server" ID="SubmitButton" Text="Submit" OnClick="SubmitButton_Click" />

// Code behind
protected void SubmitButton_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(FileNameTextBox.Text))
    {
        string script = string.Format("alert('File name is required.')");

        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Validation" + UniqueID, script, true);
    }
}

ASP.Net - need to click a button from the code-behind so I can pop an error message


  • To validate file name textbox you can use RequiredFieldValidator
  • To notify about custom logic error you can use CustomValidator

How to trigger it from code behind: see here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜