In a ListView, I have a CustomValidator set up to validate a field whenever a button with CommandName="Delete" is clicked.
<ItemTemplate>
<asp:TextBox ID="NameTextBox" Text=<%# Eval("Name") %> runat="server" />
<asp:Button ID="DeleteButton" Text="Delete" CommandName="Delete" ValidationGroup="Delete" runat="server" />
<asp:CustomValidator ValidationGroup="Delete" SetFocusOnError="true" Display="Dynamic" OnServerValidate="CustomValidator_ServerValidate" runat="server">You can't delete this.</asp:CustomValidator>
</ItemTemplate>
However, the error message is never displayed and the processing continues. What's strange is that the custom validation method is called, finds the field, and properly sets up e.IsValid to false. It does not matter whether I check Page.IsValid or not, because the error message is not displayed anyway.
It works if I remove the CommandName="Delete" from the button.
With Google I found the following solution, which seems to indicate someone has had a similar issue:
http://devio.wordpress.com/2007/12/11/formview-delete-button-and-customvalidators/
But I want to make sure that this solution is the way to go. I mean, is custom validation really not supposed to work with a delete button in a databound control, seriously?
I've alrealy heard about a problem like that, he solved it by doing it entirely differently. Like, instead of the customValidator, he puts a Label set EnableViewState="False" and Visible="False" and he check on the delete event the conditions and put the response back into the label. Maybe it can't works for you too?
But, if you realy ask "Why?????", I know he didn't find the exact reason ...
Related
Trying to debug a aspx script. Don't know any asp.net.
I've come across this piece of code:
<asp:LinkButton
ID="EditButton"
runat="server"
CausesValidation="False"
CommandName="Edit"
Text="Edit"
CssClass="LightBlue">
</asp:LinkButton>
From this site:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.command%28v=vs.110%29.aspx
I see that the method to look for is LinkButton_Command. I have done a search in all the files and not found this method.
This link is doing something, so it's not ignored. Could this method have a different name and if so how do I find what it is?
I have changed the Text attribute and that change is there when I run it, so I have the right piece of code.
I've also changed the CommandName attribute and the link stops working, so something somewhere is handling it.
There are two more linkbuttons in the code immediately after it:
<asp:LinkButton
ID="DeleteButton"
runat="server"
CausesValidation="False"
CommandName="Delete"
Text="Delete"
CssClass="LightBlue"
OnClick="DeleteButton_Click">
</asp:LinkButton>
<asp:LinkButton
ID="NewButton"
runat="server"
CausesValidation="False"
CommandName="New"
Text="New"
CssClass="LightBlue">
</asp:LinkButton>
I notice that the deletebutton has an onclick attribute, and I can find that method within the same script, but nothing obvious near that handler for the other two.
EDIT:
Another way of getting the answer that I want might be to ask the question:
What are all the different ways to add a click handler to a asp:linkbutton?
EDIT:
Don't know if it helps, but the link button is inside the following structure:
<ajax:UpdatePanel...>
<ContentTemplate>
<asp:FormView
id="FormView1"
runat="server"
OnItemDeleted="FormView1_ItemDeleted"
DataKeyNames="Id"
OnDataBound="FormView1_DataBound"
OnItemUpdated="FormView1_ItemUpdated"
OnItemUpdating="FormView1_ItemUpdating"
OnItemInserted="FormView1_ItemInserted"
OnItemInserting="FormView1_ItemInserting"
DefaultMode="Insert"
DataSourceID="odsLogEntryForm"
>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="EditButton"...
The difference with the example from the site you posted is that they used the OnCommand="LinkButton_Command" to subscribe the handler to the event. In your example this is not the case. This can mean 2 things:
- The event is not handled
- The event is subscribed to in code, probably in the code behind file of the aspx/ascx file (.aspx.cs). Look for the following code:
EditButton.Command += <name of the function to handle the command event>;
EditButton is the name of your LinkButton.
Command is the name of the event to handle.
<name of the function to handle the command event> is a function which the programmer created. He/She could have named it anything.
After seeing your edit:
The event is handled by the encapsulating FormView. A FormView will handle command events of a inner buttons. Since the CommandName of the LinkButtons is set to "Delete" and "New" is will be handled by the corresponding events in the FormView.
see: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview_events(v=vs.110).aspx for the available events of a FormVIew in this case the events with the name "Item" apply.
I'm making a custom module for mojoPortal CMS which needs to allow the client to add an affiliate into the database. As far as I can tell, this requires creating a .ascx file and then installing that using the administration toolbar in the Web interface to get it to a point where I can put it into a page, as http://www.mojoportal.com/hello-world-developer-quick-start.aspx.
The form is simple enough, but the values in the text boxes just stay empty when I submit, though the file upload works fine. The code:
<asp:Label ID="Label1" runat="server" Text="Company Name" AssociatedControlID="CompanyName">
</asp:Label>
<asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Company Description" AssociatedControlID="CompanyDescription"></asp:Label>
<asp:TextBox ID="CompanyDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Company Logo" AssociatedControlID="CompanyLogo"></asp:Label>
<asp:FileUpload ID="CompanyLogo" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Add Affiliate" />
EnableViewState for the page and the controls is enabled
The text box is not set to ReadOnly, and there is no funky JavaScript dynamically modifying elements (at least, I didn't set any).
I can work around this by using HTML elements, and get the values using Request.Form. The information is actually there, I can see it in the Request.Form, but I would have to get that by something like Request.Form[CompanyName.ClientId.Replace("_","$")] or Request.Form[6] which both seem very messy and IIRC aren't really the way things are supposed to roll in .NET. Besides, having worked until 3 last night, I really want to know what the answer is now!
Any thoughts anyone?
What I had done was not created a click event for the button, relying on the fact that it was posting to the server (like I would in PHP). Oops! When I added a click event, then the text boxes retained their value when I was within that method.
I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.
I am having issues with this GridView. I update it in design mode, and the update does not make it to the code behind section. For example, I add field "xyz". Gridview says "xyz" fields exist in design mode. In code-behind, it does not exist. when you view the page in browser, ofcoure "xyz" field is not shown. After refresh, even the gridview looses this field in design mode. So ok I got around this problem and managed to add a template field which is now working.
Now the problem is, the checkbox that I added in one of the column is not persistent. I have a button which works on the selected values of checkboxes but each time I click the button, the page refereshes and all the checked values are lost (checked values becomes unchecked).
Does anyone has any idea?
Want to mention, I am working with a bit messy code. But dont want to change a lot at this time.
<asp:TemplateField HeaderText="All" >
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" name="chkAll" />
</HeaderTemplate>
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
This took me a couple of days to figure out myself. Since my code was quite messy, it was hard to troubleshoot.
I put in some code in page_load default event that finally fixed. Don't know if I was putting code in the wrong place first. It definitely took some time though.
For some reason, I can't get the validator to raise a flag when I do things wrong.
<asp:DropDownList ID="ddlTypeList" runat="server" DataSourceID="ods_TypeOptions" DataTextField="name" DataValueField="id" SelectedValue='<%# Bind("Type") %>' AppendDataBoundItems="true">
<asp:ListItem Text="-" Value="-1" Selected="True"></asp:ListItem>
</asp:DropDownList>
The drop down list has nice values, incl the initial dummy.
Neither
<asp:RequiredFieldValidator ID="rfw" runat="server" ControlToValidate="ddlTypeList"
InitialValue="-1" ToolTip="Required">*</asp:RequiredFieldValidator>
Nor
<asp:CompareValidator ID="cv" runat="server" ControlToValidate="ddlTypeList" ValueToCompare="-1" Operator="NotEqual" ToolTip="Required">*</asp:CompareValidator>
Raises any flags to say "hey - u messed up, go fix it". For all the google, searching, reviews, swinging big hammers, I have yet to spot what I am doing wrong.
I just want one solution to fix them all.
Oh yes, I also had a ValidationGroup="myGroup" between the DDL, RFV/CV and the button. No luck.
Your code is correct. You must have some element of code or markup that is obstructing the functionality of the validator. Is it possibly that the "-" item has its value changed from "-1" to something else?
Verify that your button that submits the form has CausesValidation=True.Also, make sure that the parent of the validators is not set to Visible=False or the children will not render on the page.
Verify that the RequiredFieldValidator and CompareValidator are rendering in the markup by searching for "_cv" or possibly "cv" in the outputted markup. If it is not there, then one of the parent elements is not being rendered or the validators are being deleted.