ASP.NET WebForms RequiredFieldValidator with Repeater - asp.net

I have a repeater control that has textboxes and buttons for delete/update. Also in my form I have the same looking row as the repeater but this is for new records that the user can fill out. One of the fields is required so I have a RequiredFieldValidator on it. However, because they are all in the same form (because this visually looks correct), I can't update one of the records in the repeater row because the RequiredFieldValidator in the add row won't be filled out, which is fine in this case because they want to alter a different record not add a record.
So how would I be able to use the RequiredFieldValidator to require this field but only when the Add button is clicked and to ignore it if the delete or update button inside the repeater control is pressed?

Set CausesValidation on your delete and update buttons to false.
<asp:Button ID="delete" runat="server" CausesValidation="false" Text="Delete" />

Related

How to avoid the edit mode on a gridview?

I searched for an answer here in SO, but I couldn't find one.
I have a gridview like this one:
<asp:GridView ID="gdvSubEventos" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField HeaderText="Descrição" DataField="Descricao">
</asp:BoundField>
<asp:BoundField HeaderText="Data" DataField="data" DataFormatString="{0:dd/MM/yyyy}">
</asp:BoundField>
<asp:CommandField HeaderText="Opção" EditText="Editar" ShowEditButton="true">
</asp:CommandField>
</Columns>
</asp:GridView>
If the user clicks on edit, another form is opened (above of the grid, in the same page) for editing.
The problem is: When the user clicks on the edit button for the first time, everything works. But if he clicks on the edit button again, after the postback of the first edit, the row in the gridview becomes editable.
How can I always avoid edit mode on gridviews?
If you use the button provided in the command field you will trigger the editing ability of the gridview and the gridview expects you will be using embedded editing. Without getting into too much detail all you should have to do at the bottom of the RowEditEvent is set gdvSubEventos.EditIndex = -1 this should take the gridview out of edit mode.
To avoid the gridview from ever entering editmode at all - disable edit in the command field then
add an edit button to bring up your own form:
Add a <asp:ButtonField> to the column list
Convert it to a template field.
In the GridView RowDataBound event
Find the button control
Assign it's CommandName to something like "MyEdit"
Assign it's CommandArgument the row index so you know which row was selected.
When you select this button it will trigger the button's CommandEvent, not the GridView, so you need the row index reference to grab the data you need from the Gridview to populate your fields.

Change Select Button in GridView

I am building a CRM. It displays information of customers in a GridView. I have Selecting enabled on the GridView. And when Selected, it shows a FormView with more detailed information on the customer that was selected in the GridView. This part is working fine.
I want to make the Customers Name in the Gridview to be the select button that displays the FormView.
My question is, How do I assign the same functionality that the auto generated Select button has, to any other field in the GridView?
You probably want to replace your "Customer Name" column markup with something like this:
<asp:ButtonField DataTextField="customerName" HeaderText="Customer Name" ButtonType="Link" CommandName="Select" />
This will still show your "Customer Name" data (because of the DataTextField attribute), but it is now a LinkButton that has the behavior of your previous "Select" button (because of the CommandName="Select").
For more information, see the MSDN documentation on the ButtonField class.

Setting Visibility of Button Control in GridView Header

I have a gridview that displays entries from a data table. I am giving users the ability to select a subset of the data in the table by having a textbox and search button in the grid view header. The search button fires the gridview row command, and changes the underlying sqlDataSource's select command, and adds the text value from the text box as a parameter.
This works smoothly.
Also, I have a "Show All" button in the header, that clears out the select parameters, so all entries in the table are shown. Again, this works perfectly.
What is NOT working is controlling the visibility of the "Show All" button control. Below is the html markup for the data grid header template:
<HeaderTemplate>
<asp:Button ID="btnShowAll" runat="server" CausesValidation="False" CommandName="ShowAll" Text="Show All" />
<asp:Button ID="btnSearch" runat="server" CausesValidation="True" CommandName="Search" Text="Search" ValidationGroup="vldSearch" /><br />
<asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vldSearchName" runat="server" ErrorMessage="You have to provide an attorney name to search for." Text="*" ControlToValidate="txtSearchName" ValidationGroup="vldSearch" ForeColor="White"></asp:RequiredFieldValidator>
</HeaderTemplate>
In the Row Command event handler, here is how I am setting the visibility of the button:
If Not Me.dgAttorneys.HeaderRow Is Nothing Then
Dim btnShowAll As Button = Me.dgAttorneys.HeaderRow.FindControl("btnShowAll")
btnShowAll.Visible = Me.sqlAttorneys.SelectParameters.Count > 0
Trace.Write("Show all status is " & btnShowAll.Visible.ToString)
End If
The trace statement is showing the correct visible status - if the "show all" button is clicked, I do a SelectParameters.Clear() on the sqlAttorneys sqlDataSource.
Is my problem due to a misunderstanding of how the "FindControl" method works - I had assumed my new btnShowAll that I define is actually a reference to the "physical" control on the aspx page, so any changes I make to my local object is reflected in the control on the page.
If this is not the case, what is the best way to get a reference to the button control in the header row of the grid view?
I managed to get the button behavior to work - it was all to do with where in the overall process I was setting the button visibility. I moved that code block (setting the button visibility based on the presence of a search parameter) to the DataBound event for the data grid, and the button's visibility was set as it should be.
I suspect this is because during the overall data binding process, based on the state of the overall grid view and each grid row, the appropriate template object is used to render each row. Thus, any changes made to the button's visible property were being overridden during the data binding process. By shifting my code to set the visibility until after the data binding was complete, then it took effect.

Asp.Net datagrid row selected event

In asp.net App I am using datagrid.Is there any event fired when a row is selected?I am not using any select button in grid.
what about : SelectedIndexChanged
"Occurs when a different item is
selected in a data listing control
between posts to the server"
SelectedIndexChanged event will fired when you click on Select Command.
You can use a CommandField or a templateField to implement a Select. if you are using a command field enable SelectCommand and assign somthing to Select Text. And if you are using a template filed put a LinkButton on it and Assign Select toCommand property like this
<asp:LinkButton ID="lnbSelect" runat="server" CommandName="Select" />
You can also use any link button with any command and subscribe to the RowCommand event.

Using Validation controls with a GridView

A typical situation:
In my GridView control, I have a Footer row which contains a Textbox and an "Add" Button. When the button is pushed, the Text entered in the TextBox is added to the grid. I also have a validation control to require that, when the button is pushed, that text has been entered in the TextBox. After a new row is added, the textbox is clear to allow for easy entry of the next item.
The user may also edit the text in previously entered rows by clicking the Edit LinkButton, which puts the row into edit mode. Clicking an Update LinkButton commits the change.
The problem:
When, I click the Update link to commit the changes, if text has not been entered in the Footer row's TextBox (the row used to add a new entry), the validation control returns a "Entry Required" error. It should only require an entry if the Add button is pushed, not if the Update LinkButton is pushed.
It seems that the server side Validation control's validating event fires before the GridView's RowCommand event or the btnAdd_Click event, so I am wondering how, from the server, I can determine what event fired the postback so I can determine whether what edits should be performed for the given situation.
I am using a mix of client side "required" validation edits as well as more complex server sides. Since I probably have to have some server sided validations, I would be happy with just knowing how to handle server sided validations, but really, know how to handle this situation for client validations would also be helpful.
Thanks.
Convert your CommandField into a TemplateField, and in the EditItemTemplate, change the Update LinkButton's CausesValidation property to false.
Update:
Converting to a TemplateField is simple and doesn't require any code changes (just markup):
Changing the CausesValidation property to false in the markup is also straightforward:
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton>
<%--
More controls
--%>
</EditItemTemplate>
<ItemTemplate>
<%--
Controls
--%>
</ItemTemplate>
</asp:TemplateField>
Now, if you want your footer and data rows to be validated separately, you need to use validation groups, which is explained in Microsoft's documentation. All the controls in the same validation group will have their ValidationGroup property set to the same value, like this:
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" ValidationGroup="GridViewDataRowGroup">
</asp:LinkButton>

Resources