我的布局页面中有Form标签。我不能在内容页面中使用表单标签,所以我使用Form method =“post”如何为此创建按钮单击事件以及如何为html输入类型提供验证?
<form class="contact-form-title white" method="post">
<label id="lblFirstName" runat="server" title="First Name:">First Name:</label>
<input type="text" id="txtFirstNam" runat="server" /><br />
<label id="lblLastName" runat="server" title="Last Name:">Last Name:</label>
<input type="text" id="txtLastNam" runat="server" /><br />
<label id="lblEmail" runat="server" title="Email ID:">Email ID:</label>
<input type="text" id="txtEmail" runat="server" />
<label id="lblMessage" runat="server" title="Message">Message:</label>
<textarea id="txtMessag" runat="server" ></textarea><br />
<input type="submit" class="btn delicious f-center" runat="server" id="btnContac" name="SUBMIT" onserverclick="btnContac_Click" style="height:25%; width:10%;"/>
</form>
后端:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnContac_Click(object sender, EventArgs e)
{
string FirstName = txtFirstNam.Value;
string LastName = txtLastNam.Value;
string EmailID = txtEmail.Value;
string Message = txtLastNam.Value;
}
答案 0 :(得分:0)
方法1:客户端控制 在Code后面使用ispostback来捕获回发事件,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// write form population code;
}
else
{
//you will get posted data here;
}
}
要在客户端进行验证,请在下面的行中编写自己的java脚本验证程序。
<script type="text/javascript">
function validate()
{
var Firstname = document.getElementById('txtFirstNam').value
if (Firstname == "")
{
alert("Enter First Name");
return false;
}
if (LastName == "") {
alert("Enter Last Name");
return false;
}
}
</script>
然后在提交按钮上单击
调用此验证器<input type="submit" class="btn delicious f-center" runat="server" id="btnContac" name="SUBMIT" onclick="return validate();" style="height:25%; width:10%;"/>
选项2:服务器端控制
将所有输入和按钮更改为服务器控件(将runat=server
)控件添加到服务器中,然后您可以在代码中捕获服务器端的事件,就像您尝试的那样。 good place开始
在服务器端方法中,您有机会使用内置验证器。
ASP.NET provides the following validation controls:
RequiredFieldValidator.
RangeValidator.
CompareValidator.
RegularExpressionValidator.
CustomValidator.
ValidationSummary.
验证者参考here