2017-04-13 8 views
0

私は現在、NorthWIndデータベースを例として使用している雇用主のウェブサイトで作業しています。私はthis(挿入部分)を私のウェブサイトに追加しようとしています。リストビューに表示されている例は、GridViewで同じことをすることができますか?Gridviewの挿入行

使ってC#、ASP.NET、HTML、Javascriptを、SQL必要があるどのような言語

HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Customer_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Customers | NorthWind Maintenance</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:Menu ID="Menu1" runat="server" BackColor="#B5C7DE" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px"> 
      <DynamicHoverStyle BackColor="#284E98" ForeColor="White" /> 
      <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> 
      <DynamicMenuStyle BackColor="#B5C7DE" /> 
      <DynamicSelectedStyle BackColor="#507CD1" /> 
      <Items> 
       <asp:MenuItem Text="Root" Value="Root"> 
        <asp:MenuItem NavigateUrl="~/Customer/Default.aspx" Text="Customers" Value="Customers"></asp:MenuItem> 
        <asp:MenuItem NavigateUrl="~/Products/Default.aspx" Text="Products" Value="Products"></asp:MenuItem> 
        <asp:MenuItem Text="Orders" Value="Orders"></asp:MenuItem> 
        <asp:MenuItem Text="Employees" Value="Employees"></asp:MenuItem> 
       </asp:MenuItem> 
      </Items> 
      <StaticHoverStyle BackColor="#284E98" ForeColor="White" /> 
      <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> 
      <StaticSelectedStyle BackColor="#507CD1" /> 
     </asp:Menu> 
     <asp:Label ID="Label1" runat="server" Text="Customers"></asp:Label> 
     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" GridLines="None" PageSize="25"> 
      <Columns> 
       <asp:CommandField ShowEditButton="True" /> 
       <asp:TemplateField> 
      <ItemTemplate> 
       <asp:HiddenField ID="hfProdID" runat="server" Value='<%#Eval("CustomerID")%>' /> 
       <asp:CheckBox ID="chkRow" runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
       <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" /> 
       <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> 
       <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" /> 
       <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" /> 
       <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /> 
       <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> 
       <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" /> 
       <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /> 
       <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" /> 
       <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" /> 
       <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" /> 
       <asp:BoundField DataField="Inactive" HeaderText="Inactive" SortExpression="Inactive" /> 
      </Columns> 
      <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> 
      <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /> 
      <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /> 
      <RowStyle BackColor="#DEDFDE" ForeColor="Black" /> 
      <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /> 
      <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
      <SortedAscendingHeaderStyle BackColor="#594B9C" /> 
      <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
      <SortedDescendingHeaderStyle BackColor="#33276A" /> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax], [Inactive]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax, @Inactive)" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax, [Inactive] = @Inactive WHERE [CustomerID] = @CustomerID"> 
      <DeleteParameters> 
       <asp:Parameter Name="CustomerID" Type="String" /> 
      </DeleteParameters> 
      <InsertParameters> 
       <asp:Parameter Name="CustomerID" Type="String" /> 
       <asp:Parameter Name="CompanyName" Type="String" /> 
       <asp:Parameter Name="ContactName" Type="String" /> 
       <asp:Parameter Name="ContactTitle" Type="String" /> 
       <asp:Parameter Name="Address" Type="String" /> 
       <asp:Parameter Name="City" Type="String" /> 
       <asp:Parameter Name="Region" Type="String" /> 
       <asp:Parameter Name="PostalCode" Type="String" /> 
       <asp:Parameter Name="Country" Type="String" /> 
       <asp:Parameter Name="Phone" Type="String" /> 
       <asp:Parameter Name="Fax" Type="String" /> 
       <asp:Parameter Name="Inactive" Type="String" /> 
      </InsertParameters> 
      <UpdateParameters> 
       <asp:Parameter Name="CompanyName" Type="String" /> 
       <asp:Parameter Name="ContactName" Type="String" /> 
       <asp:Parameter Name="ContactTitle" Type="String" /> 
       <asp:Parameter Name="Address" Type="String" /> 
       <asp:Parameter Name="City" Type="String" /> 
       <asp:Parameter Name="Region" Type="String" /> 
       <asp:Parameter Name="PostalCode" Type="String" /> 
       <asp:Parameter Name="Country" Type="String" /> 
       <asp:Parameter Name="Phone" Type="String" /> 
       <asp:Parameter Name="Fax" Type="String" /> 
       <asp:Parameter Name="Inactive" Type="String" /> 
       <asp:Parameter Name="CustomerID" Type="String" /> 
      </UpdateParameters> 
     </asp:SqlDataSource> 
     <asp:Button ID="Deactivate" runat="Server" Text="Deactivate Selected" OnClick="Deactivate_Click"/> 
     <asp:Button ID="Activate" runat="server" Text="Activate Selected" Onlick="Activate_Click"/> 
     <br /> 
    </form> 
</body> 
</html> 
+0

したがって、実装のスルーグリッドビューが必要ですか? – Webruster

+0

@Webrusterええ、基本的には – SlothGod

答えて

0

サンプルGridViewのマークアップ

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" 
      CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True"> 
      <Columns> 
       <asp:TemplateField HeaderText="UserID" InsertVisible="False" SortExpression="UserID"> 
        <ItemTemplate> 
         <asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="UserName" SortExpression="UserName"> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label> 
        </ItemTemplate> 
        <FooterTemplate> 
        <asp:TextBox ID="txtfooterUserName" runat="server"></asp:TextBox> 
        <%-- Here we Put the RequiredFieldValidator for textbox (txtfooterUserName) --%> 
        <asp:RequiredFieldValidator ID="rfvUserName" runat="server" ControlToValidate="txtfooterUserName" ErrorMessage="*"></asp:RequiredFieldValidator> 
        </FooterTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Address1" SortExpression="Address1"> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("Address1") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="lblAddress1" runat="server" Text='<%# Eval("Address1") %>'></asp:Label> 
        </ItemTemplate> 
        <FooterTemplate> 
        <asp:TextBox ID="txtfooterAddress1" runat="server"></asp:TextBox> 
        </FooterTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="EmailAddress" SortExpression="EmailAddress"> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("EmailAddress") %>'></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label> 
        </ItemTemplate> 
        <FooterTemplate> 
        <asp:TextBox ID="txtfooterEmailAddress" runat="server"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="rfvEmailAddress" runat="server" ControlToValidate="txtfooterEmailAddress" ErrorMessage="*"></asp:RequiredFieldValidator> 
        <asp:RegularExpressionValidator ID="revEmailAddress" runat="server" ControlToValidate="txtfooterEmailAddress" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
       ErrorMessage="Please Enter Valid Email Address" ></asp:RegularExpressionValidator> 
        </FooterTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="EditRecord"> 
        <EditItemTemplate> 
         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" 
          Text="Update"></asp:LinkButton> 
         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" 
          Text="Cancel"></asp:LinkButton> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" 
          Text="Edit"></asp:LinkButton> 
        </ItemTemplate> 
        <FooterTemplate> 
        <asp:LinkButton ID="lnkInsert" runat="server" Text ="Insert" OnClick="lnkInsert_Click"></asp:LinkButton> 
        </FooterTemplate> 
       </asp:TemplateField> 
      </Columns> 
      <FooterStyle BackColor="Silver" Font-Bold="True" ForeColor="White" /> 
      <RowStyle BackColor="#EFF3FB" /> 
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <EditRowStyle BackColor="#2461BF" /> 
      <AlternatingRowStyle BackColor="White" /> 
     </asp:GridView> 
<asp:Label ID="lblMessage" runat="server" Text ="Insert Record Successfully" Visible ="false" > </asp:Label> 

[挿入クリックで

protected void Page_Load(object sender, EventArgs e) 
{ 
    //check whether page is postback or not 
    //if page is not postback at that time we bind the gridview 
    if (!Page.IsPostBack) 
    { 
     //call the function BindGridView 
     BindGridView(); 
    } 
} 

public void BindGridView() 
{ 
    //here write connection string 
    string strsql = DBConnection.sqlstr; 

    //create object for sqlconnection 
    SqlConnection sqlcon = new SqlConnection(strsql); 

    //here i use the query 
    //create the object of sqlcommand 
    SqlCommand sqlcmd = new SqlCommand("select * from [User]", sqlcon); 

    //create sqldataadapter object and give the sqlcommand as parameter 
    SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); 
    //declare the dataset 
    DataSet ds = new DataSet(); 
    //fill dataset using fill method of SqlDataAdapter 
    adp.Fill(ds); 

    //bind the GridView1 
    GridView1.DataSource = ds.Tables[0]; 

    GridView1.DataBind(); 
} 

の背後にあるコード

01に
+0

私のニーズに合わせて編集し、実際には完璧に動作します。ありがとう – SlothGod