1.ItemStyle-Wrap="False"
按了Edit后,会出现Update和Cancel,列宽大了,居然给自动换行,这样就不好看了
2.传parameter的时候,DefaultValue=" "字符串中留有空格才行,如果是DefaultValue="",传进去的将是Nothing
<SelectParameters>
<asp:ControlParemeter ControlID="Name" Name="Name" PropertyName="Text" Type="String" DefaultValue=" ">
...
<asp:Parameter Name="maxinumRows" Type="Int32">
<asp:Parameter Name="startRowIndex" Type="Int32">
<asp:Parameter Name="sortBy" Type="String" DefaultValue=" ">
</SelectParameters>
3.GridView1的隐藏列DataKeyNames="Id"
<asp:GridView ID="GridView1" runat="server" ... DataKeyNames="Id">
在后台可以使用GridView1.DataKeys(index).value来得到它
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Select Case e.CommandName
Case "Edit"
Dim index as Int32=Convert.ToInt32(e.CommandArgument)
Dim id as Integer=CInt(GridView1.DataKeys(index).value)
...
Case "Update"
Case "Delete"
Case "Cancel"
End Select
End Sub
3.给界面上的DropDown List或GridView指定数据源时,少使用 DataSourceID,尽量使用DataSource
因为指定DataSourceID是自动绑定数据源,当用Jquery改变了里面的option选项,总会导致被ObjectDataSource覆盖
甚至当GridView点Edit button,也会在后台自动调用
DataSourceID:
<asp:TemplateField HeaderText="test2" SortExpression="Lv2"> <ItemTemplate> <%# Eval("Lv2")%> </ItemTemplate> <EditItemTemplate> <asp:dropdownlist ID="test2" width="148px" runat="server" Text='<%# Bind("Lv2") %>' DataSourceID="dsTest" DataTextField ="Info" DataValueField ="Code"> </asp:dropdownlist> </EditItemTemplate> <ItemStyle Width="150px" /> </asp:TemplateField>
<asp:ObjectDataSource ID="dsTest" runat="server" SelectMethod="getInitDT" TypeName="SettingPage"> <SelectParameters> <asp:Parameter Name="ddl_type" Type="Int32" DefaultValue="0" /> </SelectParameters> </asp:ObjectDataSource>
DataSource
<asp:TemplateField HeaderText="test1" SortExpression="Lv1" > <ItemTemplate> <%# Eval("Lv1")%> </ItemTemplate> <EditItemTemplate> <asp:dropdownlist ID="test1" width="148px" runat="server" Text='<%# Bind("Lv1") %>' DataSource = "<%#getInitDT(0)%>" DataTextField ="Info" DataValueField ="Code"> </asp:dropdownlist> </EditItemTemplate> <ItemStyle Width="150px" /> </asp:TemplateField>
test1.DataSource = getInitDT(0)
test1.DataBind()
4.指定排序列的Style
<asp:GridView>
...
<SortedAscendingCellStyle BackColor="White" /> <SortedAscendingHeaderStyle BackColor="White" ForeColor="Black" /> <SortedDescendingCellStyle BackColor="Yellow" /> <SortedDescendingHeaderStyle BackColor="Yellow" ForeColor="Black" /> </asp:GridView>
5.自動排序
<asp:GridView ID="HistoryGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSource" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True" Width="100%" DataKeyNames="Id,Code" onrowdatabound="HistoryGridView_RowDataBound" > <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" /> <Columns> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="deleteLink" runat="server" Enabled="false" CausesValidation="False" OnClientClick="if(!this.disabled) return confirm('Are you sure to delete this record?')" CommandArgument='<%# Container.DisplayIndex %>' Text="Delete" OnClick="deleteLink_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" /> <asp:BoundField DataField="PeriodDate" HeaderText="Period" ReadOnly="True" SortExpression="PeriodDate" DataFormatString="{0:MM/yyyy}" /> <asp:HyperLinkField Text="View" DataNavigateUrlFields="PeriodDate,Id" DataNavigateUrlFormatString="~/Leave/DownloadFile?d={0:yyyyMMdd}&e={1}&flag=false" /> <asp:CommandField /> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <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:ObjectDataSource ID="ObjectDataSource" runat="server" SelectCountMethod="GetHistoriesCount" SelectMethod="GetHistories" TypeName="Common.BLL.TestBLL" onselecting="HistoryObjectDataSource_Selecting" SortParameterName="SortExpression" > <SelectParameters> <asp:Parameter Name="IdList"/> </SelectParameters> </asp:ObjectDataSource>
7.另類排序
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ResultGridView.Attributes.Add("Sort", "latestupdatedate desc") bind() End Sub Protected Sub ResultGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles ResultGridView.Sorting ResultGridView.EditIndex = -1 If ResultGridView.Attributes("Sort") = e.SortExpression + " desc" Then ResultGridView.Attributes("Sort") = e.SortExpression Else ResultGridView.Attributes("Sort") = e.SortExpression + " desc" End If bind() End Sub
Dim dv As DataView = New DataView(dt)
dv.Sort = ResultGridView.Attributes("Sort")
ResultGridView.DataSource = dv
8.空data和page size
<asp:GridView ID="HistoryGridView" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="ObjectDataSource" ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True" Width="100%" EmptyDataText="Empty Data !"
DataKeyNames="TransferPeriodDate,EmployerId" PageSize = "20" >
...
</asp:GridView>