简体   繁体   中英

What is the easiest way to check all form values to see if they are empty C#?

I have a form with four values. If any of them are empty I want to alert the user. Would the best way be to just have 4 separate if...then statements? or is there some sort of a fancy thing that I can do with C# to accomplish this?

thanks!

code

The form in question is a static HTML form:

<form id="form1" action="launch.aspx" method="post" target="_blank" name="form1">
    <input type="hidden" name="ClientID" value="123456" />
    <input type="hidden" name="Password" value="986574321" />
    <input type="hidden" name="PracType" value="001" />
    <input type="hidden" name="Encrypt" value="11258746345" />
</form>

Request.Form returns a NameValueCollection. You can loop through that collection and check if the value is set, if it's not then you can return the name of the missing field.

You can do this in C# on the .aspx.cs page, but I think Kev, is right. The RequiredFieldValidator is the quickest, simplest way to get the desired behavior you want.

By the way, why are all your form items hidden? Are we to assume you're using the control? If you can supply us with some more information, I think we can answer your question more accurately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM