.


:




:

































 

 

 

 


-




GridView

GridView , , , , , - . , , .

GridView SqlDataSource Users .

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UID" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."> <Columns> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoBaseConnectionString1 %>" DeleteCommand="DELETE FROM [Users] WHERE [UID] = @UID" InsertCommand="INSERT INTO [Users] ([UID], [name], [Comments]) VALUES (@UID, @name, @Comments)" ProviderName="<%$ ConnectionStrings:DemoBaseConnectionString1.ProviderName %>" SelectCommand="SELECT [UID], [name], [Comments] FROM [Users]" UpdateCommand="UPDATE [Users] SET [name] = @name, [Comments] = @Comments WHERE [UID] = @UID"> <InsertParameters> <asp:Parameter Name="UID" Type="Int32" /> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="Comments" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="Comments" Type="String" /> <asp:Parameter Name="UID" Type="Int32" /> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="UID" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource>

AllowSorting , . DataGrid, SortCommand.

- , , . .

. Sorting.

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string oldExpression = GridView1.SortExpression; string newExpression = e.SortExpression; if (oldExpression.IndexOf(newExpression) < 0) { if (oldExpression.Length > 0) e.SortExpression = oldExpression + ", " + newExpression; else e.SortExpression = newExpression; } else { e.SortExpression = oldExpression; } }

.

AllowPaging="True" PageSize="5"

PageSize, 10.

PagerSettings, .

<PagerSettings FirstPageText="" Mode="NextPreviousFirstLast" LastPageText="" NextPageText="-&amp;gt;" /><

.

<PagerSettings Mode="NextPreviousFirstLast" FirstPageImageUrl="~/RW_btn.gif" LastPageImageUrl="~/FF_btn.gif" NextPageImageUrl="~/fwd_btn.gif" PreviousPageImageUrl="~/RW_btn.gif" />

Comments , Null. , .

<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" NullDisplayText="N/A"/>

, .

<asp:GridView ID="GridView1" runat="server"> <EmptyDataTemplate> No records </EmptyDataTemplate> </asp:GridView>

<Columns>

<asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />

Delete, Edit.

ButtonType Image, , .

<asp:CommandField ButtonType="Image" DeleteImageUrl="~/button-delete.gif" ShowDeleteButton="True" /> <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />

. GridView , . , , GridView Checkbox.

, DataGrid, 2 .

CheckBoxField CheckBox.

ImageField , .

HyperLinkField HyperLinkColumn DataGrid. QueryStringParameter, . CustomersByCountry , 7- .

<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Customers] where @Country is null or Country = @Country" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>" CancelSelectOnNullParameter="False"> <SelectParameters> <asp:QueryStringParameter Name="Country" QueryStringField="Country" /> </SelectParameters> </asp:SqlDataSource>

GridView HyperLinkField.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource4" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound" OnRowUpdated="GridView1_RowUpdated" AutoGenerateColumns="False"> <Columns> <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:HyperLinkField HeaderText="Country" DataNavigateUrlFields="Country" DataNavigateUrlFormatString="~/CustomersByCountry.aspx?country={0}" DataTextField="Country" /> </Columns> </asp:GridView> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/CustomersByCountry.aspx">View all</asp:HyperLink>

GridView

, , . , . , RowUpdated , RowUpdating, Update RowUpdated.

RowUpdating , , .

RowUpdating GridViewUpdateEventArgs. RowIndex . , Cancel true.

OldValues NewValues . .

protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) { foreach (DictionaryEntry entry in e.NewValues) { if(entry.Value!=null) e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString()); } }

RowUpdated , . Customers CompanyName . , . ,

<asp:Label ID="ErrorLabel" runat="server" Text=" " Font-Bold="True" ForeColor="Red"></asp:Label><br />

:

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e) { if (e.Exception!= null) { ErrorLabel.Text = e.Exception.Message; e.ExceptionHandled = true; } }

RowDeleting RowDeleted
PageIndexChanged PageIndexChanging
SelectedIndexChanged SelectedIndexChanging
Sorted Sorting

Rows, GridViewRow. GridView RowState RowType. RowState : Normal, Edit, Alternate, Insert, and Selected.

, , DataItem, .

Products.aspx drag-and-drop Products GridView. , RowDataBound . . RowDataBound. Row, GridView.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { switch ((int)((DataRowView)e.Row.DataItem)["CategoryID"]) { case 1: e.Row.BackColor = Color.AliceBlue; break; case 2: e.Row.BackColor = Color.AntiqueWhite; break; case 3: e.Row.BackColor = Color.FloralWhite; break; case 4: e.Row.BackColor = Color.Beige; break; default: e.Row.BackColor = Color.White; break; } } }

, .

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='#FFE4B5'"; e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='#FFFFFF'"; } }

GridView CategoryId SupplierId. , , , . GridView (View), .

AutoGenerateEditButton True CategoryId TemplateField, :

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY [CategoryName]"> </asp:SqlDataSource>

Visual Studio 2005 , :

<asp:TemplateField HeaderText="" SortExpression="CategoryID"> <EditItemTemplate> <asp:DropDownList ID="CatDropDownList" runat="server" DataSourceID="SqlDataSource2" DataValueField="CategoryId" DataTextField="CategoryName" SelectedValue=<%# Bind("CategoryId") %>> </asp:DropDownList > </EditItemTemplate> <ItemTemplate> <asp:Label ID="CatLabel" runat="server" ></asp:Label> </ItemTemplate> </asp:TemplateField>

"" . Categories, RowDataBound.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) { // SqlDataSource2 DataView ds = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty); // DataRowView view = (DataRowView)e.Row.DataItem; int catid = (int)view.Row[3]; // ds.RowFilter = string.Format("CategoryID='{0}'", catid); Label Text1 = (Label)e.Row.FindControl("CatLabel"); Text1.Text = ds[0].Row["CategoryName"].ToString(); } }

SupplierID.

DetailsView

DetailsView 2 , , . , GridView, DetailsView , . CompositeDataBoundControl.

Visual Studio , , SmartTag, , , . InsertItemTemplate, GridView. DetailsView , .

DetailsView, Northwind, Employees.

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource2" Height="50px" Width="561px" CellPadding="4" GridLines="None" ForeColor="#333333" HeaderText=" " AutoGenerateRows="False" DataKeyNames="EmployeeID" AutoGenerateInsertButton="True" <Fields> <asp:BoundField DataField="LastName" HeaderText="" SortExpression="LastName" /> <asp:BoundField DataField="FirstName" HeaderText="" SortExpression="FirstName" /> <asp:BoundField DataField="Title" HeaderText="" SortExpression="Title" /> <asp:BoundField DataField="BirthDate" HeaderText=" " SortExpression="BirthDate" /> <asp:TemplateField HeaderText=" " SortExpression="HireDate"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HireDate") %>'></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:Calendar ID="CalendarHireDate" runat="server" BackColor="#EFF3FB" ForeColor="#003399" ></asp:Calendar> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("HireDate") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="HomePhone" HeaderText="" SortExpression="HomePhone" /> </Fields> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <RowStyle BackColor="#EFF3FB" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" /> <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" /> </asp:DetailsView>

GridView, DetailsView . AutoGenerateInsertButton="True". New. , TextBox-.

DetailsView SqlDataSource, InsertCommand .

DetailsView , , , .

ItemCreated
ItemDeleting ItemDeleted
ItemInserted ItemInserting
ItemUpdated ItemUpdating
ModeChanged ModeChanging

Employees , AllowNulls=false. , , . , ItemInserting . ModeChanged, .

protected void DetailsView1_ModeChanged(object sender, EventArgs e) { switch (DetailsView1.CurrentMode) { case DetailsViewMode.Insert: DetailsView1.HeaderText = ", , . ."; DetailsView1.HeaderStyle.ForeColor = System.Drawing.Color.Purple; DetailsView1.HeaderStyle.BackColor = System.Drawing.Color.AliceBlue; break; case DetailsViewMode.ReadOnly: DetailsView1.HeaderText = " "; DetailsView1.HeaderStyle.ForeColor = System.Drawing.Color.White; DetailsView1.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(0x507CD1); break; } }

DetailsView1 - , 0 1 1 . , .

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) { TextBox textbox1 = (TextBox)DetailsView1.Rows[0].Controls[1].Controls[0]; textbox1.Text.Trim(); TextBox textbox2 = (TextBox)DetailsView1.Rows[1].Controls[1].Controls[0]; textbox2.Text.Trim(); if (textbox1.Text == "" || textbox2.Text == "") e.Cancel= true; }

DetailsView . , , DropDownList.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>" DeleteCommand="DELETE FROM [Suppliers] WHERE [SupplierID] = @SupplierID" InsertCommand="INSERT INTO [Suppliers] ([CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax], [HomePage]) VALUES (@CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax, @HomePage)" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString2.ProviderName %>" SelectCommand="SELECT [SupplierID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax], [HomePage] FROM [Suppliers] WHERE [SupplierID] = @SupplierID" UpdateCommand="UPDATE [Suppliers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax, [HomePage] = @HomePage WHERE [SupplierID] = @SupplierID" EnableCaching="True"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="SupplierID" PropertyName="SelectedValue" Type="String" /> </SelectParameters> <InsertParameters> <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="HomePage" 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="HomePage" Type="String" /> <asp:Parameter Name="SupplierID" Type="Int32" /> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="SupplierID" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString2.ProviderName %>" SelectCommand="SELECT [SupplierID], [CompanyName] FROM [Suppliers]"> </asp:SqlDataSource> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="CompanyName" DataValueField="SupplierID" AutoPostBack="True"> </asp:DropDownList> <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" Height="86px" Width="452px" DataKeyNames="SupplierID" BackColor="White" BorderColor="#3366CC" BorderWidth="1px" CellPadding="4"> <Fields> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> </Fields> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> <EditRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> <RowStyle BackColor="White" ForeColor="#003399" /> <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> </asp:DetailsView>

AutoPostBack="True" DropDownList1 , DetailsView1 .

FormView

FormView DetailsView, , .

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString1 %>" DeleteCommand="DELETE FROM [authors] WHERE [au_id] = @au_id" InsertCommand="INSERT INTO [authors] ([au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract]) VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip, @contract)" ProviderName="<%$ ConnectionStrings:pubsConnectionString1.ProviderName %>" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]" UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zip] = @zip, [contract] = @contract WHERE [au_id] = @au_id" EnableCaching="true" CacheDuration="120"> <InsertParameters> <asp:Parameter Name="au_id" Type="String" /> <asp:Parameter Name="au_lname" Type="String" /> <asp:Parameter Name="au_fname" Type="String" /> <asp:Parameter Name="phone" Type="String" /> <asp:Parameter Name="address" Type="String" /> <asp:Parameter Name="city" Type="String" /> <asp:Parameter Name="state" Type="String" /> <asp:Parameter Name="zip" Type="String" /> <asp:Parameter Name="contract" Type="Boolean" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="au_lname" Type="String" /> <asp:Parameter Name="au_fname" Type="String" /> <asp:Parameter Name="phone" Type="String" /> <asp:Parameter Name="address" Type="String" /> <asp:Parameter Name="city" Type="String" /> <asp:Parameter Name="state" Type="String" /> <asp:Parameter Name="zip" Type="String" /> <asp:Parameter Name="contract" Type="Boolean" /> <asp:Parameter Name="au_id" Type="String" /> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="au_id" Type="String" /> </DeleteParameters> </asp:SqlDataSource> &nbsp; &nbsp; <asp:FormView ID="FormView1" runat="server" DataKeyNames="au_id" DataSourceID="SqlDataSource1"> <EditItemTemplate> au_id: <asp:Label ID="au_idLabel1" runat="server" Text='<%# Eval("au_id") %>'></asp:Label><br /> au_lname: <asp:TextBox ID="au_lnameTextBox" runat="server" Text='<%# Bind("au_lname") %>'> </asp:TextBox><br /> au_fname: <asp:TextBox ID="au_fnameTextBox" runat="server" Text='<%# Bind("au_fname") %>'> </asp:TextBox><br /> phone: <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'> </asp:TextBox><br /> address: <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'> </asp:TextBox><br /> city: <asp:TextBox ID="cityTextBox" runat="server" Text='<%# Bind("city") %>'> </asp:TextBox><br /> state: <asp:TextBox ID="stateTextBox" runat="server" Text='<%# Bind("state") %>'> </asp:TextBox><br /> zip: <asp:TextBox ID="zipTextBox" runat="server" Text='<%# Bind("zip") %>'> </asp:TextBox><br /> contract: <asp:CheckBox ID="contractCheckBox" runat="server" Checked='<%# Bind("contract") %>' /><br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"> </asp:LinkButton> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton> </EditItemTemplate> <InsertItemTemplate> au_id: <asp:TextBox ID="au_idTextBox" runat="server" Text='<%# Bind("au_id") %>'> </asp:TextBox><br /> au_lname: <asp:TextBox ID="au_lnameTextBox" runat="server" Text='<%# Bind("au_lname") %>'> </asp:TextBox><br /> au_fname: <asp:TextBox ID="au_fnameTextBox" runat="server" Text='<%# Bind("au_fname") %>'> </asp:TextBox><br /> phone: <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'> </asp:TextBox><br /> address: <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'> </asp:TextBox><br /> city: <asp:TextBox ID="cityTextBox" runat="server" Text='<%# Bind("city") %>'> </asp:TextBox><br /> state: <asp:TextBox ID="stateTextBox" runat="server" Text='<%# Bind("state") %>'> </asp:TextBox><br /> zip: <asp:TextBox ID="zipTextBox" runat="server" Text='<%# Bind("zip") %>'> </asp:TextBox><br /> contract: <asp:CheckBox ID="contractCheckBox" runat="server" Checked='<%# Bind("contract") %>' /><br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert"> </asp:LinkButton> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton> </InsertItemTemplate> <ItemTemplate> au_id: <asp:Label ID="au_idLabel" runat="server" Text='<%# Eval("au_id") %>'></asp:Label><br /> Author's name: <asp:Label ID="au_fnameLabel" runat="server" Text='<%# Bind("au_fname") %>'></asp:Label> &nbsp; <asp:Label ID="au_lnameLabel" runat="server" Text='<%# Bind("au_lname") %>'></asp:Label><br /> phone: <asp:Label ID="phoneLabel" runat="server" Text='<%# Bind("phone") %>'></asp:Label> &nbsp;&nbsp; address: <asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>'></asp:Label><br /> city: <asp:Label ID="cityLabel" runat="server" Text='<%# Bind("city") %>'></asp:Label> &nbsp; state: <asp:Label ID="stateLabel" runat="server" Text='<%# Bind("state") %>'></asp:Label> &nbsp;&nbsp; zip: <asp:Label ID="zipLabel" runat="server" Text='<%# Bind("zip") %>'></asp:Label><br /> contract: <asp:CheckBox ID="contractCheckBox" runat="server" Checked='<%# Bind("contract") %>' Enabled="false" /><br /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"> </asp:LinkButton> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"> </asp:LinkButton> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New"> </asp:LinkButton> </ItemTemplate> </asp:FormView>

 

  1. -, GridView
  2. -, GridView
  3. -, DetailsView
  4. -, GridView
  5. -, GridView




:


: 2016-09-03; !; : 468 |


:

:

, ; , .
==> ...

1572 - | 1362 -


© 2015-2024 lektsii.org - -

: 0.034 .