0

テキストボックスを持つ小さなWebページがあり、入力として数値のみを使用したい. C#側でそれを行うにはどうすればよいですか。私を助けてください!if(userId = xxx !numbers)数字を入力してくださいというメッセージを残してくださいというifステートメントが必要だと思っていました。私はそれを理解できませんでした。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AlexUsers.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            string userId = Request["UserId"];

            return View();
        }

    }
}




<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>

<html>
<head runat="server">
    <title>Search</title>
</head>
    <div align="center">
        <form id="searchUser" method="post" action="">
            <table align="center">
        <tr>
            <td class="label">
                Enter ID:
            </td>
            <td>
                <input type="text" name="UserId" id="UserId" />
            </td>
        </tr>
        <tr>
            <td>
                <button class="searchButton" id="searchButtong">Search</button>
            </td>
        </tr>
      </table>
     </form>
   </div>
   <hr /> 
</html>
4

1 に答える 1

0

送信後、Integer.TryParse を使用して、入力が整数かどうかを確認できます。

string userId = Request["UserId"];
int userIdINT;
if(Int32.TryParse(userId,userIdINT)){       
    // your code here - userIdINT is an integer converted from userId (string) now

}
于 2012-09-21T20:19:00.467 に答える