DetailsView "Selected fields" ListBox of "Fields" window shows no fields - asp.net

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>

Related

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.

Dynamically add items to DropDownList but still allow for a field that has no value?

I have populated my DropDownLists with different columns of items from a database but I'm trying to make it so that the first item in the list has no value with text similar to "Select an Item"
For some reason Even though I add the item to the list and make it the selected item, It gets overridden by the items from the database...
How can I accomplish this?
UPDATE:
Everything is done from the .aspx page designer but here is the generated code-
<asp:DropDownList ID="ddlUnits1" runat="server" DataSourceID="UnitsEDS"
DataTextField="unitId" DataValueField="unitId">
<asp:ListItem Selected="True">Select Units</asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="UnitsEDS" runat="server"
ConnectionString="name=UnitsEntity"
DefaultContainerName="UnitsEntity" EnableFlattening="False" EntitySetName="spillunits">
</asp:EntityDataSource>
Use the ListControl.AppendDataBoundItems Property:
AppendDataBoundItems Documentation
From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items."

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: How to assign ID to a field in DetailsView?

I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.

Delete from grid asp.net

i have this template field inside a gridview.
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" ImageUrl="~/images/DeleteRecord.gif" runat="server"
OnClientClick="return ConfirmacionBorrarClausula();" CommandName="BorrarClausula" CommandArgument='<%#Eval("ClausulaID")%>' OnCommand="gvClausulas_OnRowDeleting" CausesValidation="false"
</ItemTemplate>
</asp:TemplateField>
I have another one in the same page but in a different gridview, almost exactly like this one but the second one isnĀ“t working.
So i have two gridviews each one with a template field like the one here, one onRowDeleting working perfectly, the other one not working at all, when i click it, it asks for confirmation (javascript function) but when i click ok to delete, the grid loses it data and the page fires all the validators.
Thank you for your time.
Make sure the control IDs are set right. And Ispostback the control level set to true. And also Try deleting the control and add it again some time that might help. Try add it from design view.
i manage to solve it, the problem was the second gridview was losing its data on the pageload, i managed that but only with the first gridview.

Resources