How to avoid the edit mode on a gridview? - asp.net

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.

Related

ASP.NET WebForms RequiredFieldValidator with Repeater

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" />

Strange behaviour in framework 4.0 related to gridview - remove update/cancel button

I have a asp.net gridview added with CommandField as below:
<asp:Commandfield buttontype="link" showeditbutton="true" headertext="edit" visible="true" />
This will display edit as first column in the grid.
If i click any of the edit row then i will not get any update cancel row. Consecutively if i click any other button after edit i get update/Cancel row.
I have not added any update cancel template.
I do not want this to happen.I do not want the update/cancel row as i have my own code in Row_Edit event
Observation: this happens in framework 4.0 only

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.

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>

ASP.NET Gridview: How can we find which column was selected in gridview

I have extended the Gridview making it Clickable (i.e. when ever we click on any row it is selected and a detail view is shown according to the row selected).
I then added Selecting / Deselecting all the CheckBoxes in that GridView with the help of javascript. The problem now is that the page is being postbacked every time i click checkbox. I don't want this i don't want postback to happen if this particular column element i.e checkbox is pressed.
Is there any way that i can know which of the column is being selected when when the postback happens so that i can know it is happening form the column having checkbox.
Please let me know if you have soultion to it.
Thanks,
I found the solution for my problem which is as follow:
We have to cancel the Event Bubbling from Checkbox so that it won't bubble up to the Gridview RowClicked event.
The way of canceling the event bubbling form checkbox is as follow:
cb.Attributes("onclick") = "event.cancelBubble=true;"
where cb is the checkbox.
Hope this will help others having same problem as i did,
Thanks,
I'm, not to sure how exactly you add the checkbox to your gridview row but in case you do it server side make sure the AutoPostback property for the (or each) checkbox is set to false. If set to true you'll end up with a postback as soon as somebody checks/unchecks one of your checkboxes.
We can add checkbox in our gridview, below is a sample code that we need inside Columns of GridView:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkBxSelect" runat="server" AutoPostBack ="false" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
<HeaderTemplate>
<asp:CheckBox ID="chkBxHeader" onclick="javascript:HeaderClick(this);" runat="server" />
</HeaderTemplate>
Is it possible in ASP.NET Gridview the we postback if we click in some columns of Gridview and we don't postback when we click in certain selected columns.
e.g. I have 3 columns in a gridview, I want to postback if i click in column 1 and 2 and don't want postback if i click in column 3.

Resources