Change Select Button in GridView - asp.net

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.

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.

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

DetailsView "Selected fields" ListBox of "Fields" window shows no fields

On a web form we have a DetailsView bound to a dataSource.
The property called "AutGenerateRows" is also set to true.
When the web form is displayed in a web browser, the data from the fields are shown.
We want to change the properties of some of the fields. When we display the "Fields" window, I noticed that the "Selected fields" ListBox only contained a CommandField and no other fields.
Can you tell me how to set up the DetailsView so we can change the properties of the fields?
The attached screen shots will show what is happening. There are many fields and we want to avoid typing in all the code needed to make the fields as template fields if possible.
These screen shots show what is happening:
Update: Thanks for your replies. With your help, we are now on the correct path. Here is what the finished web form looks like:
For deparatment ID add a template field column
From the fields window, add a templateField Column. Then go to aspx source code place a dropdownlist. Something like this:
<asp:TemplateField >
<ItemTemplate>
<asp:DropDownList ID="DDL" runat="server" DataTextField='<%# Eval("FieldName") %>' />
</ItemTemplate>
</asp:TemplateField>

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.
I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.
These are full Hyperlinks such as "Http://stackoverflow.com/"
Originally the GridView column was a simple BoundField like this:
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
So I started trying to convert it into a hyperlink field.
<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
DataTextField="JobDescription"
HeaderText="JobDescription"
SortExpression="JobDescription"
Target="_blank"
NavigateUrl="{0}" />
This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.
Either way will work...
I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.
I appreciate the help.
Use a Template Field. So your can define your normal view and editing view.
Grrr found the answer:
<asp:BoundField DataField="JobDescription" HeaderText="Job Description"
SortExpression="JobDescription"
DataFormatString="<a target='_blank' href='{0}'>Text</a>"
HtmlEncode="False" />
You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...
The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

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.

Resources