Form data is saving and validation message is also displaying - asp.net

We are using the callbackpanel for the validation of the Devexpress controls but what actually happening is :
If we click submit button without entering any thing in textboxes in the form. It is showing the validation messages but it is also saving the blank data to the database.
I want to know if validation message is showing then why event in backend is calling for saving data ?

Basically in DevExpress ASPxCallbackPanel, a Button Click does not trigger a Callback, it triggers a Postback that is the difference between an UpdatePanel and a CallbackPanel.
So you have to call the ASPxCallbackPanelClient.PerformCallback('PARAM'); instead.
You did not mention how you trigger the callback, you did not show how your ASPxButton code looks or how your ASPxCallbackPanel looks or how your Validator looks, so considering that please see code below that works very well in you scenario
All the controls as DX, including validators and buttons
<dx:ASPxCallbackPanel runat="server" ID="cbpDIActions" ClientInstanceName="cbpDIActions" OnCallback="cbpDIActions_Callback">
<panelcollection>
<dx:PanelContent>
<dx:ASPxSpinEdit ID="txtBuilFixtNoBathrooms" runat="server" ValidationSettings-RequiredField-IsRequired="true" ValidationSettings-ValidationGroup="SubmitValidation">
</dx:ASPxSpinEdit>
<dx:ASPxButton ID="btn" runat="server" AutoPostBack="false" Text="Process" UseSubmitBehavior="False"
ValidationSettings-ValidationGroup="SubmitValidation" CausesValidation="true"
>
<ClientSideEvents Click="function(s,e){
ASPxClientEdit.ValidateGroup('SubmitValidation');
if (ASPxClientEdit.AreEditorsValid()) {
if(!cbpDIActions.InCallback()) {
cbpDIActions.PerformCallback('PARAM');}
}
}" />
</dx:ASPxButton>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
And in the backend you put all your code in the callback event
protected void cbpDIActions_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
{
if (e.Parameter != null && e.Parameter.ToString() == "PARAM")
{
//PROCESS YOUR CODE
}
}

Related

Disable Button after click in dnn

for one requirements i need to disable button after click and run server side code after disabled for this i have used below code which is called at pageload
btnGREntry.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(btnGREntry, "") +
";this.value='Please wait...';this.disabled = true;");
but it is giving me below error.
An unknown error occurred while processing the request on the server.
The status code returned from the server was: 0
please help me to find out a solution or suggest any other solution to disable button after click
Note: similar things are working in asp.net but i am using in *dotnetnuke version 7.0 *
You can do this in the code-behind.
ASPX
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button" />
Code-behind
protected void Button1_Click1(object sender, EventArgs e)
{
Button a = (Button)sender;
a.Enabled = false;
}
Does the button have to remain disabled continuously or is it to return to enabled after page refresh? In the former case then handle it with both JavaScript and code behind. In the later case handle it with just JavaScript.
<asp:Button ID="Button1" runat="server" onClientClick="DisableButton();" onclick="Button1_Click1" Text="Button" />
JavaScript:
function DisableButton() {
var btn = $("#buttonID").attr("disabled", "disabled");
}

RequiredFieldValidator with ValidationSummary not working with enter key inside empty textbox

Consider the following ASPX:
<asp:ValidationSummary ID="vSummary" runat="server" CssClass="error-message" HeaderText="<p>Please correct the following errors with your profile.</p>" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
<asp:TextBox ID="txtTest" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" Display="None" ErrorMessage="Enter a value in the textbox." ControlToValidate="txtTest"></asp:RequiredFieldValidator>
<asp:Button ID="btn1" runat="server" Text="Button 1" OnClick="btn1Click" ClientIDMode="Static" />
With text entered into the textbox, everything behaves as expected.
Clicking the button submits the form / pressing 'Enter' while inside the textbox submits the form.
However, when the textbox is empty, pressing 'enter' does nothing. Clicking the button will display the validation summary, as expected, but pressing 'enter' does not.
How can I make the validation summary display after the 'Enter' key is pressed inside an empty textbox?
For some reason when you hit enter error does not being displayed by ValidationSummary control. However, if you change in RequiredFieldValidator Display to "Dynamic" you will see error message.
Update: to overcome this problem try to follow instructions from this article.
For lazy ones:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Add new attribute to form1 event to fire when enter key submit button click event
form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + ButtonName.ClientID + "')");
// Allow you to use enter key for sumbit data
ClientScript.RegisterHiddenField("__EVENTTARGET", "ButtonName");
}
}
And finally:
// Add this code to div section on the form1 ASPX page.
<div id="inputArea" onkeypress="javascript:return WebForm_FireDefaultButton(event,'ButtonName')">
That behaviour is because your textbox is being validated but the validation message is not showing because you turned it off with Display = "none". It's doing the validation on client side and therefore, postback did not occur because of empty text.
If you remove the Validation control (which is not what you want) from the page, your text box will start posting back. I guess you don't want the message to show for each control, just in the validation summary, otherwise, that's the easiest way

UserControl conditional validation approach?

I have a custom UserControl which contains several TextBoxes with Validators. One TextBox with corresponding Validator is optional based on a CheckBox. Pseudo:
My Control.ascx:
<asp:TextBox id="txtAddress" />
<asp:Validator id="valAddress" />
<asp:CheckBox id="condition" />
<asp:TextBox id="txtConditional" />
<asp:Validator id="valConditional" ValidationGroup="ConditionalGroup" />
My Control.ascx.cs
public void Validate() {
if(condition.Checked) {
Page.Validate("ConditionalGroup");
}
}
I also have a page which basically looks like this:
Page.aspx
<my:Control id="myControl" />
<asp:Button onClick="doPost" />
Page.aspx.cs
protected void doPost(object sender, EventArgs e) {
myControl.Validate(); //This feels wrong
if(Page.IsValid) {
//go
}
}
This all works, however I would like to take the myControl.Validate() line out of the Page.aspx.cs and put it in the My Control.ascx.cs. Putting it in the Page_Load of the control is not an option because the conditional checkbox value is always false. There is no event available after Page_Load and before the doPost click handler is fired...
It feels wrong to call the custom Validate function on the Page where I think it should belong somewhere in the UserControl. Is it true that this is architecturally wrong? Is there another solution for this maybe by using an event handler?
You could try by enabling the validator only if the user checks on the check box. And disable the validator if the unchecks it. This has to be done in the user control. It can be done in the client side or in the server side. In this way, the page would validate all the validators that are enabled for the page.

Button's cause validation property is set to false but still it fires validation

I have a button in EmptyTempletField of gridview for which causevalidation property is set to false.
But when I try to add row in gridview from empty template field by clicking on that button, the button is not firing row command event. Moreover, it fires the validation. I have few validations which are grouped. But this button fires all validations irrespective of group. If I click on the button second time, then it fires the row command event. I can't understand what is happening..
Why button fires validation which it is not supposed to fire...???
It is not clear what is exaclty happening with your code. Anyway, it should be something similar to this
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDeletePicture" runat="server" CommandName="YOURCOMMAND" Text="command" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "YOURCOMMAND")
{
//your code
}
}
And read this article about GridView.RowCommand, which is helpful
Hope this helps

Why is break point unreachable when using jQuery Droplistfilter

I am using this drop list filter on my Asp.Net Form.
jQuery plugin DropListFilter
The filter on the dropdownlist list works perfecty.
<script type="text/javascript">
$(document).ready(function() {
$('select').droplistFilter();
});
</script>
<asp:DropDownList ID="ddlMonth" runat="server" CssClass="ddlStyle"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfv_ddlMonth" runat="server" ControlToValidate="ddlMonth"
CssClass="warning" Display="Dynamic" ErrorMessage="*" ForeColor=""></asp:RequiredFieldValidator>
<asp:Button ID="btnRun" runat="server" CssClass="btnStyle" OnClick="btnRun_Click" Text="Run Report" />
CodeBehind
protected void btnRun_Click(object sender, EventArgs e)
{
Response.Write(ddlMonth.SelectedValue.ToString());
return;
}
If I do a search on the dropdownlist using the jQuery filter and click run report, the debugger does not stop at the above Response.Write statement.
On application of the filter and pressing Run the debugger does not even hit the load method below.
protected void Page_Load(object sender, EventArgs e)
{
// Bind Month if !PostBack
}
Upon further investigating I am getting the following error on the Application_Error:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I do not want to disable the event validation, so what can I do to make the filter work?
The droplistFilter is changing what is being expected by ASP.
ASP doesn't like it when things are changed without it's explicit written consent.
ASP is expecting that dropdown to contain what it put in the dropdown, when you change what is in that with javascript asp sees that as a security risk and is giving you the validation error.

Resources