Reputation: 175
So with the suggestions i updated my code.
<asp:TextBox ID="txtStudent_Id" runat="server"></asp:TextBox></td>
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtStudent_Id"
ErrorMessage="Id is required"
ID="validator_ID"
ValidationGroup="Validation_ID">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtStudent_Id"
ValidationExpression="\d+"
Display="Static"
EnableClientScript="true"
ErrorMessage="Please enter numbers only" ValidationGroup="Validation_ID"
runat="server"/>
<asp:Button runat="server" Text="Submit" ID="btnSubmitStudent" ValidationGroup="Validation_ID" CausesValidation="true" OnClick="btnSubmitStudent_Click" />
addstudent.Student_Id =Convert.ToInt32(txtStudent_Id.Text);
Now that i have also entered the regular expression validator it should work but i still get the same error.
Upvotes: 2
Views: 959
Reputation: 9460
TextBox
<asp:TextBox ID="txtStudent_Id"
runat="server"
ValidationGroup="VAlidation_Name">
</asp:TextBox>
RequiredFieldValidator
you have not added id to the RequiredFieldValidator and also ValidationGroup
<asp:RequiredFieldValidator runat="server"
Display="Dynamic"
ControlToValidate="txtStudent_Id"
ID="rfvStudent_Id"
ForeColor="Red"
ValidationGroup="VAlidation_Name"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>
Button Submit
<asp:Button ID="btnSubmit" Text="Submit"
runat="server"
ValidationGroup="VAlidation_Name" CausesValidation="true" />
In default CausesValidation="true"
but check that too.
Code Part
addstudent.Student_Id =int.TryParse(txtStudent_Id.Text);
Suggestion:
And you can use AjaxToolKit Extender MaskedEdit to make the user enter only integer.
<asp:MaskedEditExtender ID="txtEndDate_MaskedEditExtender" runat="server" ClearTextOnInvalid="True"
ClearMaskOnLostFocus="true" Enabled="True" Mask="9999999"
MaskType="Date" TargetControlID="txtStudent_Id"
ErrorTooltipEnabled="True">
Updated:
void SubmitButton_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
// Do form stuff
}
Check this too EnableClientScript="True"
and Enabled="True"
<asp:RequiredFieldValidator runat="server"
Display="Dynamic"
ControlToValidate="txtStudent_Id"
ID="rfvStudent_Id"
EnableClientScript="True"
Enabled="True"
ForeColor="Red"
ValidationGroup="VAlidation_Name"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>
Upvotes: 0
Reputation: 38683
Don't need a solution for your error.need a solution why the validation control not works? and how can solve this problem?then what i do?
Main thing
You have missed ValidationGroup="XXXXX" in validation control and button control, So it's allowed to server side code, so first add that and
try with this asp:RegularExpressionValidator
for only allow integer value, So It's not allow if you type any special and string characters
<asp:TextBox ID="txtStudent_Id" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtStudent_Id" ValidationGroup="XXXXX" EnableClientScript="True"
ErrorMessage="Id is required">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtStudent_Id"
ValidationExpression="\d+"
Display="Static"
EnableClientScript="true"
ErrorMessage="Please enter numbers only" ValidationGroup="XXXXX"
runat="server"/>
<asp:button ValidationGroup="XXXXX"></asp:button>
or
<asp:button ValidationGroup="XXXXX" CasusesValidation="true"></asp:button>
If you try my answer,then don't worry about Input string was not in a correct format. error.
Update:
please try this in your button click event.
Page.Validate();
if (Page.IsValid == false)
{
return;
}
//Call your code here
Upvotes: 1