我有GridView控件我可以使用一些幫助的數據綁定方法的問題。一些背景...我有一個搜索界面與各種文本框(名字,姓氏,年份,性別等)用戶將輸入一些數據,然後按搜索按鈕。在那個時候,我動態地爲數據源生成SQL並將其綁定到gridview控件。ASP.NET的GridView分頁問題
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
' Check Program Year
If ddYear.SelectedValue = 0 Then
lblStatus.Text = "Please select a program year!"
Exit Sub
Else
lblStatus.Text = ""
End If
ds1.SelectCommand = "SELECT [StudentId], [ProgramYear], [LastName], [Middle], [FirstName], [HighSchoolCode], [Sex], [DateOfBirth] FROM [Student] WHERE [ProgramYear] = " & ddYear.SelectedValue & ""
Dim useCase As Integer = 0
If tbFname.Text <> "" Then useCase = 1
If tbLname.Text <> "" Then useCase = 2
If tbFname.Text <> "" And tbLname.Text <> "" Then useCase = 3
If tbFname.Text <> "" And tbM.Text <> "" Then useCase = 4
If tbID.Text <> "" Then useCase = 5
If tbHSCode.Text <> "" Then useCase = 6
If tbLname.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 7
If tbHSCode.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 8
Select Case useCase
Case 1 'First Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%')"
Case 2 'Last Name
ds1.SelectCommand += " and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 3 'First and Last Name
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
Case 4 'First and Middle
ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([Middle] = '" & tbM.Text & "')"
Case 5 'Student ID
ds1.SelectCommand += " and ([StudentId] = '" & tbID.Text & "')"
Case 6 'HS Code
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "')"
Case 7 'Last Name and Sex
ds1.SelectCommand += " and ([Sex] = '" & ddGender.SelectedValue & "' and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%'))"
Case 8 'HS Code and Sex
ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "' and [Sex] = '" & ddGender.SelectedValue & "')"
Case Else
ds1.SelectCommand += " order by [LastName] desc"
End Select
ds1.DataBind()
GridView1.DataBind()
End Sub
但是當我啓用分頁和下面我碰到麻煩應用OnPageIndexChanging方法這非常適用。
Protected Sub gridview1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
gridview1.PageIndex = e.NewPageIndex
GridView1.DataSource = ds1
gridview1.databind()
End Sub
會發生什麼事是我點擊GridView控件的頁面頁腳更改網頁,直到我再次按下提交按鈕在GridView不適用新的綁定。我不確定爲什麼會發生這種情況,任何建議都會被讚賞。
的GridView:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="StudentId" DataSourceID="ds1" AllowPaging="True" PageSize="15">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id" InsertVisible="False" SortExpression="StudentId">
<ItemTemplate>
<a href="searchDetail.aspx?StudentID=<%# eval("StudentId") %>" >
<asp:Label ID="xyz" runat="server" Text='<%# Bind("StudentId") %>' /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="Middle" HeaderText="Middle" SortExpression="Middle" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
<asp:BoundField DataField="HighSchoolCode" HeaderText="HS Code" SortExpression="HighSchoolCode" />
<asp:BoundField DataField="ProgramYear" HeaderText="Program Year" SortExpression="ProgramYear" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
DS1
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:NMERITEXString %>"></asp:SqlDataSource>
謝謝我嘗試添加,但我仍然有同樣的問題... – user2055729
看到我編輯的答案,我希望它的作品。 – ekad
我試過你編輯的答案,仍然gridview不重新綁定,直到我按下提交按鈕。但是,感謝您的改進。在'LoadGrid'子'; – user2055729