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!

Posted in | | | Submitted by keith on Wed, 2008-03-05 06:12.
read more | add new comment

Reply

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.
  • Use <!--pagebreak--> to create page breaks.
More information about formatting options

Syndicate, Bookmark & Sitemap

Syndicate Content Bookmark This Site NeoHide SiteMap

Quick Search

User login

Search

Sponsored Ads

Women's Corner

Who's new

  • akibsayyed
  • biomarwan
  • movjerap
  • hackernet
  • sZwCGKObKbpH

Syndicate

Syndicate content

FastBox Ad Feed

Subscribe Feeds

Feed Icon
Google
Yahoo!
Bloglines
Newsgator
MSN
Feedster
AOL
Furl
Rojo
Newsburst
Pluck
FeedFeeds
Add Kinja
SoloSub
MultiRSS
rMail
RSSFwd
Blogarithm
Eskobo
GritWire
BotABlog
Monitor This
Simplify