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.
Related
<form runat="server" id="from1">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<script src='https://www.google.com/recaptcha/api.js'></script>
<asp:Panel runat="server" ID="pnlSiteQuestions">
>>>>> CONTROLS removed for brevity <<<<<
<asp:Button ID="btnContinue" runat="server" Text="Continue" OnClick="btnContinue_Click" ValidationGroup="Questions" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="pnlNotRequired" runat="server" Visible="false">
Sorry you do not qualify.
</asp:Panel>
<asp:Panel runat="server" ID="pnlAdditionalDetails" Visible="false">
<asp:ValidationSummary runat="server" ID="ValidationSummary1" />
Name
<asp:RequiredFieldValidator runat="server" ID="ValidatorName" ControlToValidate="ContactFormNameTextBox" ErrorMessage="Name is required" CssClass="ErrorClass">Required</asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="txtName" />
<div class="g-recaptcha" data-sitekey="ABC123"></div>
<asp:Button runat="server" ID="button1" Text="Confirm" OnClick="button1_Click" />
</asp:Panel>
</form>
I have the above form and decided to add an update panel. When a user clicks Continue, it goes to a database and determines if the user qualifies. If not pnlNotRequired message is displayed otherwise pnlAdditionalDetails is displayed.
Everything is/was working until i added the update panel. So removing the UP has everything working.
I tried adding a trigger but i didnt fully understand how it should be configured as i dont completely understand why this is occurring?
No Javascript errors listed either. Anyway to overcome this issue?
Update panel should cover all panels. Add property to update panel "UpdateMode='Conditional'" than add trigger block
So, I have this update panel:
<asp:UpdatePanel runat="server" RenderMode="Inline" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ddOutcomeType" />
<asp:AsyncPostBackTrigger ControlID="ddOutcomeCode" />
</Triggers>
<ContentTemplate>
<fieldset>
<asp:DropDownList ID="ddOutcomeType" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddOutcomeType_OnSelectedIndexChanged">
<asp:ListItem Value="0">Please Select...</asp:ListItem>
<asp:ListItem Value="EDU">Continuing in Education</asp:ListItem>
<asp:ListItem Value="EMP">Paid Employment</asp:ListItem>
<asp:ListItem Value="NPE">Not in Paid Employment</asp:ListItem>
<asp:ListItem Value="GAP">Gap Year</asp:ListItem>
<asp:ListItem Value="OTH">Other</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddOutcomeCode" runat="server" DataTextField="COMPDESC"
DataValueField="EBSVALUE"
OnSelectedIndexChanged="ddOutcomeCode_OnSelectedIndexChanged"
AutoPostBack="True" Visible="false" />
<br /><br />
<asp:Label ID="lblDestMessage" runat="server"
Text="Please start typing at least 3 characters of the HE institute
then select from the list (Don't make your own up!)"
Visible="false">
</asp:Label>
<h4 style="align-content:center;">
<asp:Label ID="Label1" runat="server" Text="Comments on Destination">
</asp:Label></h4>
<asp:TextBox ID="txtDestComments" runat="server" Width="70%"></asp:TextBox>
<br />
<asp:RegularExpressionValidator ID="validJust" runat="server"
Display="Dynamic" ControlToValidate="txtDestComments"
ValidationExpression="^([\S\s]{0,249})$" CssClass="label label-danger"
ErrorMessage="Please enter maximum 250 characters"
SetFocusOnError="True">
</asp:RegularExpressionValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
CssClass="btn btn-primary" OnClick="btnSubmit_OnClick" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
I Have looked at other questions for the same problem but none of the fixes work for this, i tried putting the button outside of the update panel but then the Hidden Fields that are changed by the dropdown lists dont stay changed outside of the update panels.
Any Help?
You are using UpdateMode property of update panel as UpdateMode="Conditional". Either you have to explicitly use Update() method of update panel in button Click event on server side to update the content inside update panel or remove this UpdateMode property because by default it is set as Always.
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 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
Frens,
i have a textbox with ajaxToolkit:CalendarExtender which losses data when i chose radio buttons ....
please read my code...
<asp:UpdatePanel ID="uppnl_Select_File_Format" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="input-field-box-container">
<asp:TextBox ID="txtExpiryDate" runat="server"
SkinID="FormInputTextBox" ReadOnly="true"
ValidationGroup="PublishUser"> </asp:TextBox>
<ajaxToolkit:CalendarExtender ID="ajax_Expiry_Date" runat="server" TargetControlID="txtExpiryDate">
</ajaxToolkit:CalendarExtender>
</div>
<div class="input-field-box-container">
<asp:RadioButton ID="rbtnEnabled" GroupName="Print" Text="Enable" runat="server"
AutoPostBack="true"
OnCheckedChanged="rbtnEnabled_CheckedChanged" CssClass="checkbox-auto"
Width="220px" />
<asp:RadioButton ID="rbtnDisabled"
GroupName="Print" Text="Disable" runat="server"
AutoPostBack="true" OnCheckedChanged="rbtnDisabled_CheckedChanged" CssClass="checkbox-auto"
Width="220px" />
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnCreate" />
<asp:AsyncPostBackTrigger ControlID="ajax_Expiry_Date" />
</Triggers>
</asp:UpdatePanel>
Your radioButton's AutoPostback attribute(or what it is called) is true that means when you change the choice of radio button the page will post back and which will cause to refresh the UpdatePanel.As long as your radio button and CalenderExtender is on the same UpdatePanel every time OnCheckedChanged="rbtnDisabled_CheckedChanged" works CalenderExtender will loose data.
Well you can understand that your solution is using different UpdatePanels for those RadioButtons and CalenderExtenders.
The solution I arrived at was to remove ReadOnly="true".