Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-10
3
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Assisted Solution

by:madgino
madgino earned 1200 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 1200 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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