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.
Related
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?
I have created a student enrolment wizard that they go through every year that uses the MultiView control and I have multiple View Controls within it. An example of one View control can be seen below;
<asp:View ID="address_view" runat="server">
<h1>Address:</h1>
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
<asp:DropDownList ID="address_dropdown_insert" runat="server">
<asp:ListItem Value="Home">Home Address</asp:ListItem>
<asp:ListItem Value="Term">Term Time Address</asp:ListItem>
<asp:ListItem Value="Mail">Mail Address</asp:ListItem>
<asp:ListItem Value="Business">Business Address</asp:ListItem>
</asp:DropDownList><br />
Address 1:
<asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
Address 2:
<asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
Town/City:
<asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
County:
<asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
PostCode:
<asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
Country:
<asp:TextBox Text="" runat="server" ID="countryTextBox" />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
<asp:ListView ID="address_list" runat="server" DataSourceID="user_address" DataKeyNames="address_id">
<EditItemTemplate>
// code here
</EditItemTemplate>
<EmptyDataTemplate>
<br />
<p>There are currently no addresses found, please click the button above to add a new address.</p>
</EmptyDataTemplate>
<ItemTemplate>
//code here
</ItemTemplate>
<LayoutTemplate>
//code here
</LayoutTemplate>
</asp:ListView>
<br />
<asp:Button CommandName="NextView" ID="Button1" Enabled="false" runat="server" Text="Next" />
</asp:View>
I have the following C# on PageLoad
protected void Page_Load(object sender, EventArgs e)
{
Button1.Enabled = address_list.Items.Any();
}
However, the button still appears even if there is no data in the ListView.
My question is, how can I prevent a user from continuing onto the next view when they click Button1 if their is no data in the ListView?
I'm fairly new to C# so any help would be much appreciated.
Try something like:
if(address_list.Items.Count() = 0)
{
Button1.enabled = false
}else
{
Button1.enabled = true
}
But have in mind that if you call this before you bind the data it wont work so be sure to not use this code as the first line in the Page load event
The lv.items.Count() will return 0 if it has no rows after binded
I have created a registration wizard in ASP.net that uses the MultiView control and I have multiple View Controls within it. An example of one View control can be seen below;
<asp:View ID="address_view" runat="server">
<h1>Address:</h1>
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
<asp:DropDownList ID="address_dropdown_insert" runat="server">
<asp:ListItem Value="Home">Home Address</asp:ListItem>
<asp:ListItem Value="Term">Term Time Address</asp:ListItem>
<asp:ListItem Value="Mail">Mail Address</asp:ListItem>
<asp:ListItem Value="Business">Business Address</asp:ListItem>
</asp:DropDownList><br />
Address 1:
<asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
Address 2:
<asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
Town/City:
<asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
County:
<asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
PostCode:
<asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
Country:
<asp:TextBox Text="" runat="server" ID="countryTextBox" />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
<asp:ListView ID="address_list" runat="server" DataSourceID="user_address" DataKeyNames="address_id">
<EditItemTemplate>
// code here
</EditItemTemplate>
<EmptyDataTemplate>
<br />
<p>There are currently no addresses found, please click the button above to add a new address.</p>
</EmptyDataTemplate>
<ItemTemplate>
//code here
</ItemTemplate>
<LayoutTemplate>
//code here
</LayoutTemplate>
</asp:ListView>
<br />
<asp:Button CommandName="NextView" ID="Button1" runat="server" Text="Next" />
</asp:View>
The database table that the insert form inserts to is named address. It has the following fields
address_id
user_id
address_type
address_1
address_2
town_city
county
postcode
country
The user_id is pulled from another table called users.
I have the following C# to get the user_id of the current user who is logged in;
//get id of logged in user
string user_id = Session["user_id"].ToString();
My question is, how can I prevent a user from continuing onto the next view when they click Button1 if their user_id does not appear in the address table?
I'm fairly new to C# so any help would be much appreciated.
Presentationally you will want to disable Button1 until an address is entered. Initally your Button1 markup will include Enabled = "false"
<asp:Button CommandName="NextView" Enabled="false" ID="Button1" runat="server" Text="Next" />
Then within the insert_address_button_Click handler, after an address has been successfully added, enable the button like so:
protected void insert_address_button_Click(object sender, EventArgs e)
{
// ..insert the address. If successful, enable the "Next" button
Button1.Enabled = true;
}
That's a simple approach, but should do what you ask.
EDIT:
To address Button1's state in the user returns to the address view:
protected void Page_Load(object sender, EventArgs e)
{
Button1.Enabled = address_list.Items.Any();
}
My problem is that I can't seem to get my textbox to fire the OnTextChanged event. I also checked and it doesn't seem like it is doing an autopostback despite the fact that it is set to true.
Basically, I am trying to validate the text in the textbox and either enable or disable a button based on the validation status.
Here's my code:
<asp:Panel ID="panelAccessControl" runat="server" Visible="false">
<asp:Panel ID="panelAddMileageRate" runat="server" BorderWidth="2" >
<h2>Add Mileage Rate</h2>
<asp:ScriptManager ID="scriptManagerMileageRate" runat="server" />
<asp:UpdatePanel ID="updatePanelAddMileageRate" runat="server">
<ContentTemplate>
<p>
Purpose:
<asp:DropDownList ID="ddlAddPurpose" runat="server"
AutoPostBack="true" Width="200px"></asp:DropDownList>
<br />
Country:
<asp:DropDownList ID="ddlAddCountry" runat="server"
AutoPostBack="true" Width="200px" >
</asp:DropDownList>
<br />
Effective Date:
<asp:Calendar ID="calAddEffectiveDate" runat="server">
</asp:Calendar>
<br />
Mileage Rate:
<asp:TextBox ID="txtAddMileageRate" runat="server"
AutoPostBack="true" Width="200px"
CausesValidation="true"
ontextchanged="txtAddMileageRate_TextChanged">
</asp:TextBox>
<asp:RegularExpressionValidator
ID="validatorAddMileageRate"
ControlToValidate="txtAddMileageRate" runat="server"
SetFocusOnError="true"
ErrorMessage="Only Numbers allowed"
ValidationExpression="^\d{1,20}(\.\d{0,4})?$">
</asp:RegularExpressionValidator>
</p>
</ContentTemplate>
</asp:UpdatePanel>
<p>
<asp:Button ID="buttonAddSecurityGroup" runat="server"
Text="Submit" onclick="buttonAddSecurityGroup_Click" />
</p>
</asp:Panel>
<asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
</asp:Panel>
The text box in question is txtAddMileageRate.
Set the EventName property for your txtAddMileageRate AsyncPostBackTrigger to TextChanged.
Add following codes inside your update panel and outside of ContentTemplate
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtAddMileageRate"
EventName="TextChanged" />
</Triggers>
Other sugguestion:
Have you tried looking at the AutoComplete (http://www.asp.net/ajax/ajaxcontroltoolkit/samples/autocomplete/autocomplete) control that is part of the AjaxControlToolKit? Its behaves the same way you want your solution to behave.
I'm trying to get the original object back from a listview, when I click on the button.
I thought it was in e.Item.DataItem but that always seems to be null.
<form runat="server">
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="tbCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:TextBox ID="tbEmailAdress" runat="server" Text='<%# Eval("EmailAddres") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<asp:Button runat="server" Text="Create or Update Account" CommandArgument='<%# Container.DataItem.ToString() %>' />
<br />
</ItemTemplate>
</asp:ListView>
</form>
private void ListList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
You need to keep the original datasource in session and get the real values while clicking a button.