Skip to main content

Validate RadioButtonList and Textbox in FormView

After weeks of researching for answer, I finally came up with the solution of how to validate objects within a FormView using ASP.NET 2.0 programming language.

Assuming you have a FormView, and you require users to update information to the SQL database. There is a RadioButtonList and a TextBox. The entry within the textbox is optional, but it will be mandatory if one of the option is chosen. Take for example, the screen-shot below.

(find out how to solve the validation issue...)

Task: To validate that the texbox is populated with information if the radio button selection is "Reject".

Procedure:

  1. From the Formview, specify the following command:
    OnItemUpdating = "ValidateReject"

    This is inserted in the line containing:

    <asp:FormView ID=... OnItemUpdating="ValidateReject"...>

  2. Write the following C# code for ValidateReject:
    protected void ValidateReject(object sender, FormViewUpdateEventArgs e)
    {
    FormViewRow row = MyFormView.Row;
    RadioButtonList list = (RadioButtonList)row.FindControl("MyRadioButtonList");
    string y1 = list.SelectedValue;
    labelShow.Text = null;
    if (y1 == "Reject")
    {
    TextBox Text1 = (TextBox)row.FindControl("textbox1");
    string x1 = Text1.Text;
    if (x1 == null || x1 == "")
    {
    labelShow.Text = "<font color=\"red\">Rejection Without Reasons!</font>";
    e.Cancel = true;
    }
    else
    labelShow.Text = null;
    }
    }
  3. I have also included a text label to indicate the reason:
    <asp:Label ID="labelShow" runat="server" />

With a few line of codes, you can easily validate any information from the FormView! In addition, this can also be extended to DetailsView as well!

It is me

Hello
I love google and Yahoo!
:)
G'night

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options