• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

A separate add button when using Gridview/Detailsview

For the first time I'm using the Gridview and Detailsview controls for general database entry/viewing on a web page, and I noticed that I get the 'New' option only when viewing a record in Detailsview.  Worse, when I am looking at a data set with no records, I don't have a record to view and therefore no access to the 'New' button at all.

How can I get the New button to appear all the time -- separated from the DetailsView?

My code similar to the samples posted here:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/detailsview.aspx
0
dparkes
Asked:
dparkes
  • 3
  • 2
1 Solution
 
vs1784Commented:
Put the New Button code outside DetailsView.

If you post your code i can help you out.

Thanks
0
 
dparkesAuthor Commented:
I thought there might be an automated (or drag 'n' drop) method for doing this, but if it has to be coded manually that's okay I suppose.  I believe these are the relevant parts of the code....  

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:OnsiteServicesConnection %>"
ProviderName="<%$ ConnectionStrings:OnsiteServicesConnection.ProviderName %>" SelectCommand="SELECT * FROM Hardware WHERE Location=@Loc" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Hardware] WHERE [ID] = ? AND [Model] = ? AND [Serial] = ? AND [Notes] = ? AND [Location] = ?" InsertCommand="INSERT INTO [Hardware] ([Model], [Serial], [Notes], [Location]) VALUES (?, ?, ?, ?)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Hardware] SET [Model] = ?, [Serial] = ?, [Notes] = ?, [Location] = ? WHERE [ID] = ? ">
<SelectParameters> 
<asp:SessionParameter Name="Loc" SessionField="Location" Type="String" /> 
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_Model" Type="String" />
<asp:Parameter Name="original_Serial" Type="String" />
<asp:Parameter Name="original_Notes" Type="String" />
<asp:Parameter Name="original_Location" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_Model" Type="String" />
<asp:Parameter Name="original_Serial" Type="String" />
<asp:Parameter Name="original_Notes" Type="String" />
<asp:Parameter Name="original_Location" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Location" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OnsiteServicesConnection %>"
DeleteCommand="DELETE FROM [Hardware] WHERE [ID] = ?" 
InsertCommand="INSERT INTO [Hardware] ([Model], [Serial], [Notes], [Location]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:OnsiteServicesConnection.ProviderName %>" SelectCommand="SELECT * FROM [Hardware] where ID=?"
UpdateCommand="UPDATE [Hardware] SET [Model] = ?, [Serial] = ?, [Notes] = ?, [Location] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="defaultText" Name="newparameter"
PropertyName="SelectedValue" />
</FilterParameters>
<UpdateParameters>
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Location" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="?" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
 
 
 
 
 
 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource2"
Font-Names="Arial" Font-Size="Medium" ForeColor="#333333" GridLines="None"
Width="95%" >
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Serial" HeaderText="Serial" SortExpression="Serial" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
 
 
 
 
 
<strong><span style="font-family: Arial"></span></strong><asp:DetailsView
ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" Height="50px"
Width="95%" Font-Names="Arial" OnItemUpdated="DetailsView1_ItemUpdated" OnItemInserted="DetailsView1_ItemInserted">
<Fields>
 
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Model") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox  ID="TextBox3" runat="server" Text='<%# Bind("Model") %>' ></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="TextBox3" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
 
<asp:TemplateField HeaderText="Serial" SortExpression="Serial">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Serial") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox   ID="TextBox2" runat="server" Text='<%# Bind("Serial") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Serial") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
 
<asp:TemplateField HeaderText="Notes" SortExpression="Notes">
<EditItemTemplate>
<asp:TextBox Rows="4" Height="50" Wrap="true" ID="TextBox1" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox  Rows="4" Height="50" Wrap="true" ID="TextBox1" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="True" />
</asp:TemplateField>
 
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<HeaderTemplate>
<strong>DETAILS</strong>
</HeaderTemplate>
</asp:DetailsView>

Open in new window

0
 
vs1784Commented:
Ok add <EmptyDataTemplate> in the grid view and add one link button with below code on its click event

protected void addNew(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
}


Hope it helps

Thanks
0
 
vs1784Commented:
Sorry i mean add <EmptyDataTemplate> for detailsview
0
 
dparkesAuthor Commented:
That's exactly what I needed.  Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now