I have a page which consists of a form on top to add new records into table1 and below that a GridView which shows records in table1 with the ability to edit.
Now the form above is simply one text box which allows you to enter names into table1, this textbox has a required field validator. The validator causes issues however when the gridview is in edit mode as when the user presses 'update' nothing is saved because the validator is triggers (as there is nothing in the form above)
How can I fix this?
You have to put CausesValidation="False" in GridView
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="False"/>
You can set the RequiredFieldValidator Control and the Add button have the same ValidationGroup name. Something like this:
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" ValidationGroup="addValidation" />
<asp:RequiredFieldValidator ID="nameRequired" ControlToValidate="txtName" runat="server" ValidationGroup="addValidation"></asp:RequiredFieldValidator>
...
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" />
So that, the Update button won't be impacted.
Use cause validation to false in the gridview button.
<asp:Button ID="Button2" runat="server"
CausesValidation="False"
Text="Cancel - Will Not Validate!" />
Here is an example:
http://www.java2s.com/Code/ASP/Asp-Control/CausesValidationFalse.htm
Related
This is my aspx code:
<asp:TemplateField HeaderText="Enter_Quantity" >
<ItemTemplate>
<asp:TextBox ID ="TextBox1" runat="server" DataField="Total_Quantity" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please add a quantity"> </asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID ="Button7" runat="server" OnClick="Button7_Click" CommandArgument="Button7" CommandName="Select" Text="Add To Cart" CausesValidation ="true" />
</ItemTemplate>
</asp:TemplateField>
Kindly note that the Button is in every Gridview Row and also the textbox column is in every Gridview row.
The problem I'm facing is that when I keep the textbox empty and then click on the button. It is showing the error message "Please add a quantity". But it is showing it in every Gridview row. I want to show this error message for that specific GridView row.
How do I solve this ?
Can someone correct my aspx code ?
Set CauseValidation = "false".
It allows you to remove validation from specific control. Suppose you're using two buttons in your page but you want only one button to validate. But as per ASP rules all button will validate your form because of CausesValidation = true by default.
I have a FormView with View and Edit modes, in an UpdatePanel:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:FormView runat="Server" DefaultMode="ReadOnly" DataSourceID="_myDataSource" >
<ItemTemplate>
<!-- View controls omitted -->
<asp:Button runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<!-- Edit controls omitted -->
<asp:Button runat="server" CommandName="Update" Text="Save" />
<asp:Button runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</EditItemTemplate>
</asp:FormView>
</ContentTemplate>
</asp:UpdatePanel>
<!-- Data source (may attributes omitted) -->
<asp:ObjectDataSource ID="_myDataSource" runat="server" />
The Edit and Update buttons / commands work fine, but the Cancel button does nothing.
Any ideas?
The page may not seems to flicker but on clicking Cancel button 2 events are raised by FormView and post back indeed happens. You may refer MSDN. ( Table under Remarks section)
What a 'Cancel' button does is:
Cancels an edit or insert operation and returns the FormView control to the mode specified by the DefaultMode property. Raises the ModeChanged and ModeChanging events.
So you need to handle events for ModeChanged and ModeChanging as shown below:
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID"
onmodechanged="EmployeeFormView_ModeChanged"
onmodechanging="EmployeeFormView_ModeChanging"
runat="server">
So, any code that you want to run after ' cancel' button click should utilize these two events as per the requirements.
I had placed debugger in Page_Load, modeChanging() & modechanged(), all of the three events were fired in sequence on clicking 'Cancel' button.
I have the following button on GridView and I want to display a message box to the user for confirming whether he is sure or not for buying the item.(I'm familiar with OnClientClick= return confirm('xxxxx'); ) but on Grid View it is different I can't use this method though.
<asp:ButtonField CommandName="buynow" ImageUrl="~/images/buy.png" HeaderText="BUY" ButtonType="Image"/>
Use itemtemplate field in column node in gridview markup
eg.
<asp:TemplateField HeaderText="Save">
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Do you want to save?')" />
</ItemTemplate>
</asp:TemplateField>
Aspx, Entity, on my gridView I made a footer for a new button. It shows when there are items, but not when data is empty. gridView's ShowFooter = True. Not sure why it won't show when empty, is there another property I maybe missing? I know a workaround could be place a dummy row in the database, but think there should be a better way to start off at 0. If you have any more questions, please ask.
<asp:TemplateField HeaderText="New" >
<FooterTemplate >
<asp:Button ID="btnNew" CssClass="DDButton" runat="server" Text="New" CommandName="New" OnClick="new_item" />
</FooterTemplate>
</asp:TemplateField>
The footer-template is not shown when no data is present, only the EmptyDataTemplate.
<EmptyDataTemplate>
<asp:Button ID="btnNew" runat="server"
Text="New" CommandName="New"
CssClass="DDButton"
OnClick="new_item" />
</EmptyDataTemplate>
.Net 4.0 added the ShowHeaderWhenEmpty property on the GridView but unfortunately no ShowFooterWhenEmpty. However there is still the EmptyDataTemplate:
<asp:GridView runat="server" ID="myGridView">
<EmptyDataTemplate>
<asp:Button ID="btnNew" CssClass="DDButton" runat="server" Text="New" CommandName="New" OnClick="new_item" />
</EmptyDataTemplate>
</asp:GridView>
As this will not be in a GridViewRow I don't think your RowCommand event will fire but you seem to be handling the click of the button in a separate method anyway.
Reference for GridView.EmptyDataTemplate: http://msdn.microsoft.com/en-gb/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
I am using a multi page gridview to display bunch of data. Here is the code of the gridview.
<asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" OnRowDataBound="unverifiedlist_RowDataBound" style="font-size:12px" >
<Columns><asp:BoundField HeaderText="Surname" DataField="Surname" ReadOnly="true"/></Columns>
<Columns><asp:BoundField HeaderText="Firstname" DataField="Givenname" ReadOnly="true"/></Columns>
......
<Columns><asp:TemplateField HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="2">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server" OnTextChanged ="reason_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="false"/>
</asp:GridView>
<asp:DropDownList ID="PageSelect" AutoPostBack="true" runat="server" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList>
there is a column called "option" which contains a dropdownlist, and it will postback everytime when the selected index was changed to save the edited row. I am trying to avoid this frequent postback. So i think if I can save the whole page before user turn to the next page.
The problem is the "PageSelect" dropdownlist's postback will trigger the page reload first, then onselectedindexchanged event, at this point, the gridview already turned to the next page. So can anyone give me some advice?
You can set EnableSortingAndPagingCallBacks property of the grid view to true , but that wont work if you are using template fields in your gridview, for maintaining the state of the page, you will have to keep the value of current page in session and on page load you can check the session..
I used the gridview itself's paging navigation control instead of my dropdownlist at last. Then I am able to use the pageindexchanging event to save the current page into a session before it goes to the next page