Solved

Where to place "create new"- button so it appears also when there are no records in gridview

Posted on 2013-01-10
3
208 Views
Last Modified: 2013-01-11
Hi!
Probably I am just making something easy complicated, so this might be easy.

I have a formview and a gridview which interact to update a accessdatabase. However, when I delete all records from the gridview, I cannot see the gridview (=logical), neither can I see the formview from which I deleted it. Please, see attached picture and code below for more information.

<%@ Page language="VB" ValidateRequest=false%>
<%@ import namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Principal" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- #INCLUDE FILE="admin.inc" -->
<script runat="server">
    
    Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
				
        
        If session("password") <> password Then
            session("referer") = request.servervariables("URL")
            response.redirect("login.aspx")
        End If

        
		
    End Sub

   
    Function InsertLink2(ByVal strIn) As String
        Dim strTmp As String
        strTmp = "<ul><a href='" & strIn & "'>" & strIn & "</a></ul>"
        Return strTmp
	
    End Function

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        FormView1.PageIndex = GridView1.SelectedIndex
        
        
    End Sub
    
    

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Redigera viktigt.aspx</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Redigera</h3>

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
          DataFile="app/viktigt.mdb" 
          SelectCommand="SELECT [ID], Rubrik2,[Beskrivning2], [Todayd], [Lank], viktigt2.[IDNR] FROM [Viktigt2] inner join [Values] on [Viktigt2].idnr=[values].idnr"
           updatecommand="UPDATE [Viktigt2] SET [Beskrivning2] = ?, [Todayd] = ?, [Lank] = ?, [IDNR] = ? WHERE [ID] = ?"
           runat="server" DeleteCommand="DELETE FROM [Viktigt2] WHERE [ID] = ?" 
          
          InsertCommand="INSERT INTO [Viktigt2] ( [Beskrivning2], [Todayd], [Lank], [IDNR]) VALUES ( ?, ?, ?, ?)">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="Beskrivning2" Type="String" />
        <asp:Parameter Name="Todayd" Type="DateTime" />
        <asp:Parameter Name="Lank" Type="String" />
        <asp:Parameter Name="IDNR" Type="String" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="Beskrivning2" Type="String" />
        <asp:Parameter Name="Todayd" Type="DateTime" />
        <asp:Parameter Name="Lank" Type="String" />
        <asp:Parameter Name="IDNR" Type="String" />
    </InsertParameters>
      </asp:AccessDataSource> 
      <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
                      DataFile="app/viktigt.mdb" 
                      SelectCommand="SELECT [IDNR], Rubrik2 FROM [Values]">
                  </asp:AccessDataSource>

      <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
          DataSourceID="AccessDataSource1" AllowPaging="True">
          <EditItemTemplate>
              ID:
              <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
              <br />
              Beskrivning2:
              <asp:TextBox ID="Beskrivning2TextBox" textmode=MultiLine 
					Width=500 Rows=10 runat="server" 
                  Text='<%# Bind("Beskrivning2") %>' />
              <br />
              Todayd:
              <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("Todayd") %>'  ></asp:Calendar>


              <br />
              Lank:
              <asp:TextBox ID="LankTextBox" runat="server" Text='<%# Bind("Lank") %>' />
              <br />
              Rubrik2:
              <asp:DropDownList ID="dropdownlist1"
DataSourceID="accessdatasource2"
DataTextField="Rubrik2"
DataValueField="idnr"
SelectedValue='<%# Bind("IDNR") %>' runat="server" >
</asp:DropDownList>


<br />

              <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                  CommandName="Update" Text="Uppdatera" />
              &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Avbryt" />
          </EditItemTemplate>
          <InsertItemTemplate>
              Beskrivning2:
              <asp:TextBox ID="Beskrivning2TextBox" textmode=MultiLine 
					Width=500 Rows=10 runat="server" 
                  Text='<%# Bind("Beskrivning2") %>' />
              <br />
              Todayd:
             <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("Todayd") %>'  ></asp:Calendar>
              <br />
              Lank:
              <asp:TextBox ID="LankTextBox" runat="server" Text='<%# Bind("Lank") %>' />
              <br />
              Rubrik2:
              <asp:DropDownList ID="dropdownlist1"
DataSourceID="accessdatasource2"
DataTextField="Rubrik2"
DataValueField="IDNR"
SelectedValue='<%# Bind("IDNR") %>' runat="server" >
</asp:DropDownList>
              <br />
              <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                  CommandName="Insert" Text="Lägg till" />
              &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Avbryt" />
          </InsertItemTemplate>
          <ItemTemplate>
              ID:
              <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
              <br />
              Beskrivning:
              <asp:Label ID="Beskrivning2Label" runat="server" 
                  Text='<%# InsertLink2(Eval("Beskrivning2")) %>' />
              <br />
              Todayd:
              <asp:Label ID="TodaydLabel" runat="server" 
                  Text='<%# Bind("Todayd") %>' />
              <br />
              Lank:
              <asp:Label ID="LankLabel" runat="server" Text='<%# bind("lank") %>' />
              <br />
              Rubrik
              <asp:Label ID="IDNRLabel" runat="server" Text='<%# Bind("idnr") %>' />
              <br />
              <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                  CommandName="Edit" Text="Ändra" />
              &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                  CommandName="Delete" Text="Radera" />
              &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                  CommandName="New" Text="Ny post" />
          </ItemTemplate>
      </asp:FormView>
      <asp:GridView ID="GridView1" runat="server" AllowSorting="True" onselectedindexchanged="GridView1_SelectedIndexChanged"
          DataSourceID="AccessDataSource1">
          <Columns>
              <asp:CommandField ShowSelectButton="True" SelectText="Redigera" />
              <asp:TemplateField>
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label1" runat="server"></asp:Label>
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
    </form>
  </body>
</html>

Open in new window


best regards
Problem.pdf
0
Comment
Question by:marcgu
  • 2
3 Comments
 
LVL 11

Assisted Solution

by:madgino
madgino earned 300 total points
ID: 38766265
As strange it might sound this is normal behavior of the controls you are using.
I was surprised too but this is the reality.

You have to define <EmptyDataTemplate> for the gridview and put there the content you want displayed when no rows exists. In my case I created an identical html copy of the normal table and paste it there.
0
 
LVL 11

Accepted Solution

by:
madgino earned 300 total points
ID: 38766274
It looks that if you are on .NET 4.0 you're lucky, otherwise you're on your own:
http://stackoverflow.com/questions/354369/gridview-show-headers-on-empty-data-source

I was on 3.5 so I had to manually do the header.
0
 

Author Closing Comment

by:marcgu
ID: 38766991
Hi!

Thank you, I added this code in the formview:

<emptydatatemplate>
          <table>
            <tr>
              <td>
               <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                  CommandName="New" Text="Ny post" />
              </td>
             
            </tr>
          </table>
        </emptydatatemplate>

and now, the formview appears with a button for "new record" (Ny post") even if the gridview is empty.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now