Solved

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

Posted on 2013-01-10
3
206 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What .NET website keeps me current? 9 58
Why don't I see this table in EDMX file? 2 24
Split in Javascript 5 31
ASP.NET gridview select textbox on focus 2 15
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

862 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

23 Experts available now in Live!

Get 1:1 Help Now