I have spent hours on this and read countless threads but not sure what i may be missing to get this to work the way i would like.
I would like to select a file to upload and when the user clicks the button to save the record if there are any validation errors to retain the value of file that was selected to upload.
Heres my markup
<asp:MultiView runat="server" ID="RegisterMultiView">
<asp:View ID="DefaultView" runat="server">
<asp:UpdatePanel ID="upCountryBrands" runat="server">
<ContentTemplate>
Select parent
<asp:RequiredFieldValidator
SetFocusOnError="true"
runat="server" ID="RequiredFieldValidator3"
ControlToValidate="ddlParent" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlParent" runat="server"
OnSelectedIndexChanged="ddlParent_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
Select Child
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator2"
ControlToValidate="ddlChild" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlChild" runat="server"
OnSelectedIndexChanged="ddlChild_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Name
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator1"
ControlToValidate="txtName" ValidationGroup="MainForm" ErrorMessage="Please enter your name" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:TextBox ID="txtName" runat="server" MaxLength="100" />
Enter description of your issue
<asp:TextBox ID="txtIssue" runat="server" TextMode="MultiLine" /> <%--This is a Tiny MCE control--%>
Image 1
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="rfv1" ControlToValidate="FileUpload1" ValidationGroup="MainForm"
ErrorMessage="Please select an image." Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFileTypes" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload1" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" accept="image/*" ViewStateMode="Enabled" EnableViewState="true" />
<asp:Label ID="lblImageOneStatus" runat="server"></asp:Label>
Image 2
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator9"
ControlToValidate="FileUpload2" ValidationGroup="MainForm" ErrorMessage="Images only" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload2" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" class="form-control-file" accept="image/*" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="MainForm" OnClick="btnSave_Click" OnClientClick="txtIssue.triggerSave(false,true);" />
</asp:View>
<asp:View ID="SuccessView" runat="server">
Thanks
</asp:View>
</asp:MultiView>
Couple of notes:
I have an updatePanel surrounding the dropdown so it looks like theres no postback.
I have tried surrounding the FileUpload controls with an UpdatePanel but this made no difference.
I have a TonyMCE control, i dont think this would add a spanner into the works but i have left out the associated Javascript so i can provide if required.
I have tried adding triggers but again this made no difference so i can only assume i am doing something wrong.
How could i ensure when a file is selected using the FileUpload controls and then press Save it retains the value of the file?
Related
I have a question. I have a textarea and 2 textboxes. What I want to do is to check if textarea is filled, other 2 textboxes must not be empty. We can give messagebox like this (please necessary fields must be filled)
This is my code :
<textarea id="txtCustomerRepresentatorOpinion" name="txtCustomerRepresentatorOpinion" runat="server" style="width: 99%"></textarea>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCustomerRepresentatorOpinion"
ValidationGroup="Save" Text="*" ClientValidationFunction="txtArea" />
<telerik:RadTextBox Width="321px" runat="server" ID="txtCustomerRepresentatorOpinionNameSurname" TextMode="SingleLine" />
<asp:RequiredFieldValidator ID="VldAdi" runat="server" ControlToValidate="txtCustomerRepresentatorOpinionNameSurname"
ValidationGroup="Save" Text="*" ErrorMessage="Lütfen Adı Soyadı giriniz."/>
<telerik:RadDatePicker ID="rdpCustomerRepresentatorOpinionDate" ZIndex="9999" Style="vertical-align: middle;" runat="server">
</telerik:RadDatePicker>
<asp:RequiredFieldValidator ID="VldTarih" runat="server" ControlToValidate="rdpCustomerRepresentatorOpinionDate"
ValidationGroup="Save" Text="*" ErrorMessage="Lütfen Tarihi giriniz.">
</asp:RequiredFieldValidator>
Which side should I use (client side or server side)? How i do this job? Right now, I can show messages but textarea is not working.
Change your textarea into an asp.net TextBox Control with TextMode="MultiLine". Then a RequiredFieldValidator will work.
<asp:TextBox ID="txtCustomerRepresentatorOpinion" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required field" ControlToValidate="txtCustomerRepresentatorOpinion"></asp:RequiredFieldValidator>
Here's the thing. I have a ASP.NET Web Application in C#. In the Default.aspx I have an UpdatePanel containing all the elements. There, I have three validators associated with a TextBox, 4 RadioButton (two belong two one group and the other two to another group) and for each group I have a validator in order to verify if one element was checked for each group. For all RadioButton the AutoPostBack is set to "True" because when the "No" option is selected for both groups then I will enable a multi-line TextBox so the user can input some text. There's also a ValidationSummary at the end that displays error messages in a pop-up box and in the page as well. The main issue is the following: if I first select the two options from the RadioButtons and then click in the submit button, without input any text in the TextBox with the 3 validators, I will get 3 pop-up error messages. And more interesting, if I select after the 3 pop-up error messages another option from any of the RadioButton groups, I will get 4 pop-up error messages. I have a guess that it has to do with the AutoPostBack attribute of the RadioButton because it happens when I click them, but I don't know why or how to fix it.
Here's the code for Default.aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="M-DCPS 7-digit Student ID#:"></asp:Label>
<asp:TextBox ID="studentIDtbox" runat="server" MaxLength="7" Width="6%" CausesValidation="false"></asp:TextBox>
<asp:RequiredFieldValidator Font-Size="Large" ID="RequiredFieldValidatorAccount" runat="server" ControlToValidate="studentIDtbox"
ErrorMessage="ID# required" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator Font-Size="Large" ID="RegularExpressionValidator2" runat="server" ControlToValidate="studentIDtbox" ErrorMessage=" ID requires 7 digits" ValidationExpression="^[\s\S]{7,}$" ForeColor="Red">*</asp:RegularExpressionValidator>
<asp:RegularExpressionValidator Font-Size="Large" ID="RegularExpressionValidator1" runat="server" ControlToValidate="studentIDtbox"
ErrorMessage=" ID only accepts numbers" ValidationExpression="^\d+$" ForeColor="Red">*</asp:RegularExpressionValidator>
<asp:Label ID="Label2" runat="server" Text="Paper Form:"></asp:Label>
<asp:DropDownList ID="paperForm" runat="server"></asp:DropDownList>
<asp:Label ID="Label3" runat="server" Text="Paper Accommodation:"></asp:Label>
<asp:DropDownList ID="paperType" runat="server"></asp:DropDownList>
<asp:CustomValidator Font-Size="Large" runat="server" ID="cvIEP" OnServerValidate="cvIEP_ServerValidate"
Enabled="true" Display="Dynamic" SetFocusOnError="true" ErrorMessage="You must select at least one item for documentation on IEP/Section 504." ForeColor="Red">*</asp:CustomValidator>
<asp:Label ID="Label4" runat="server" Text="Paper is documented?"></asp:Label>
<asp:RadioButton ID="iepYesRb" runat="server" GroupName="IEPRequirement" Text="Yes" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:RadioButton ID="iepNoRb" runat="server" GroupName="IEPRequirement" Text="No" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:CustomValidator Font-Size="Large" runat="server" ID="cvRuse" OnServerValidate="cvRuse_ServerValidate"
Enabled="true" SetFocusOnError="true" ErrorMessage="You must select at least one item for regular use." ForeColor="Red">*</asp:CustomValidator>
<asp:Label ID="Label5" runat="server" Text="Is it used regularly?"></asp:Label>
<asp:RadioButton ID="ruseYesRb" runat="server" GroupName="RegularUse" Text="Yes" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:RadioButton ID="ruseNoRb" runat="server" GroupName="RegularUse" Text="No" OnCheckedChanged="rButton_CheckedChanged" AutoPostBack="True" CausesValidation="false"/>
<asp:Label ID="Label6" runat="server" Text="Provide other evidence if two 'No' where selected."></asp:Label>
<asp:TextBox ID="evidenceTbox" Style="height: 130px" runat="server" TextMode="MultiLine" Width="100%" Enabled="false"></asp:TextBox>
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submit_OnClick" />
<asp:ValidationSummary ID="valTest" runat="server" ShowMessageBox="true" BackColor="LightYellow" ForeColor="Red" Width="100%" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="submitButton" />
<asp:AsyncPostBackTrigger ControlID="iepYesRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="iepNoRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="ruseYesRb" EventName="CheckedChanged" />
<asp:AsyncPostBackTrigger ControlID="ruseNoRb" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
And here's the code for Default.aspx.cs:
protected void rButton_CheckedChanged(object sender, EventArgs e)
{
evidenceTbox.Enabled = iepNoRb.Checked && ruseNoRb.Checked;
}
protected void cvIEP_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = iepNoRb.Checked || iepYesRb.Checked;
}
protected void cvRuse_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = ruseNoRb.Checked || ruseYesRb.Checked;
}
protected void submit_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alerts", "javascript:alert('Record inserted properly')", true);
}
}
Any help will be appreciated and critics will be very welcome. Thanks.
I'm having difficulty trying to access an FileUpload on a event ButtonImagem_Click in the code behind in C# that is within a CompleteWizardStep on a CreateUserWizard.
Where is my code:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" Title="Criar conta">
<ContentTemplate>
<%--...--%>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server" ID="CompleteWizardStep1">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1" ErrorMessage=""></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1" ErrorMessage="Apenas são aceites imagens." ValidationExpression="(.*\.([Gg][Ii][Ff])|.*\.([Jj][Pp][Gg])|.*\.([Bb][Mm][Pp])|.*\.([pP][nN][gG])|.*\.([tT][iI][iI][fF])$)"></asp:RegularExpressionValidator>
<br /><asp:Button ID="ButtonImagem" runat="server" Text="Concluir" OnClick="ButtonImagem_Click" />
</ContentTemplate>
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Found a solution, just needed to add ContentTemplateContainer like this:
FileUpload FileU = (FileUpload)CompleteWizardStep1.ContentTemplateContainer.FindControl("FileUpload1");
Is it possible to show the error message of a Regular Expression validator in Asp.net into a message box/ dialog box?
I've tried everything i knew but nothing works for me.
Thanks alot
<form id="form1" runat="server">
<asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
<asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="None" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
<asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
<asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
Display="None" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
ValueToCompare="-1"></asp:CompareValidator>
<br />
<asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
ShowSummary="False" ValidationGroup="Validation" />
</form>
use the same ValidationGroup text on all validation controls and add a ValidationSummary with the ValidationGroup and ShowMessageBox="true"
So with the suggestions i updated my code.
Textbox:
<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"/>
Button:
<asp:Button runat="server" Text="Submit" ID="btnSubmitStudent" ValidationGroup="Validation_ID" CausesValidation="true" OnClick="btnSubmitStudent_Click" />
Code Behind:
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.
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
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>