基本上,我有一个显示从数据库中提取的数据的GridView,该页面还有一个过滤器按钮,单击列表上的工作人员将更改SQL语句,以便只选择该工作人员。 / p>
在GridView
内,每一行都有一个按钮,它实际上将AppointmentID导出到变量中,以便显示更多信息。
但是,按下过滤器按钮后,GridView
内容会更改GridView
中的按钮,不再“导出”正确的ID。
过滤器按钮的标记:
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown" id="dropdownstaff"><asp:Label runat="server" ID="lblDropdownstaff" Text="All Stylists"></asp:Label>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" id="ulStaffSelect">
<li><asp:Button runat="server" Text="All Stylists" ID="lstStaffSelect1"/></li>
<li><asp:Button runat="server" Text="Elaine" ID="lstStaffSelect2"/></li>
<li><asp:Button runat="server" Text="Dawn" ID="lstStaffSelect3"/></li>
</ul>
</div>
GridView的标记(注意注释掉的LinkedButton - 它的行为与普通按钮的行为相同,“UseSubmitBehaviour”设置为false):
<asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" OnRowCommand="Staffgv_RowCommand">
<HeaderStyle CssClass="gvHeadStyle" />
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="<<" PreviousPageText="<" NextPageText=">" LastPageText=">>" />
<AlternatingRowStyle BackColor="#ffffff" CssClass="gvAlternatingClass"/>
<Columns>
<asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart">
<HeaderStyle Width="70px" CssClass="hdrGvStart"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeStart" runat="server" Text='<%# Bind("DateTimeStart", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd">
<HeaderStyle Width="70px" CssClass="hdrGvFinish"/>
<ItemTemplate>
<asp:Label ID="lblDateTimeEnd" runat="server" Text='<%# Bind("DateTimeEnd", "{0:t}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Forename" SortExpression="Forename">
<HeaderStyle Width="140px" CssClass="hdrGvForename"/>
<ItemTemplate>
<asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname" SortExpression="Surname">
<HeaderStyle Width="140px" CssClass="hdrGvSurname"/>
<ItemTemplate>
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle CssClass="gvHeaderEdit" />
<ItemTemplate>
<asp:Button ID="Btnapptid" runat="server" Text="____" CssClass="btnGVEdit" CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>' UseSubmitBehavior="false"/>
<%--<asp:LinkButton ID="Btnapptid" runat="server" Text="____" CssClass="btnGVEdit" CommandName="FillStaffTables" CommandArgument='<%# Bind("AppointmentID") %>' Width="48px" Height="48px"/>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB代码:
Dim connString As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\me\Documents\Visual Studio 2015\Datebases\DB.mdf';Integrated Security=True;Connect Timeout=30"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblStaffHead.Text = "You are logged in as " & StaffMemberName
selectedStaffMember = "Salon"
generatePage(selectedStaffMember)
End Sub
Protected Sub lstStaffSelect1_Click(ByVal sender As Object, e As EventArgs) Handles lstStaffSelect1.Click
lblDropdownstaff.Text = "All Stylists"
selectedStaffMember = "Salon"
generatePage(selectedStaffMember)
End Sub
Protected Sub lstStaffSelect2_Click(ByVal sender As Object, e As EventArgs) Handles lstStaffSelect2.Click
lblDropdownstaff.Text = "Elaine"
selectedStaffMember = "Elaine"
generatePage(selectedStaffMember)
End Sub
Protected Sub lstStaffSelect3_Click(ByVal sender As Object, e As EventArgs) Handles lstStaffSelect3.Click
lblDropdownstaff.Text = "Dawn"
selectedStaffMember = "Dawn"
generatePage(selectedStaffMember)
End Sub
Function findIndex(ByVal selectedStaffMember As String)
Select Case selectedStaffMember
Case "Salon"
staffdropdownindex = 0
Case "Elaine"
staffdropdownindex = 1
Case "Dawn"
staffdropdownindex = 2
Case Else
staffdropdownindex = 3
End Select
staffindex = staffdropdownindex
Return staffindex
End Function
Protected Sub generatePage(ByVal selectedStaffMember As String)
staffindex = findIndex(selectedStaffMember)
Dim sqlCommandString As String
If staffindex <> 0 Then
sqlCommandString = "SELECT [Appointments].[DateTimeStart], [Appointments].[DateTimeEnd], [Appointments].[AppointmentID], [CustomerData].[Forename], [CustomerData].[Surname] " &
"FROM [Appointments] " &
"INNER JOIN [CustomerData] " &
"ON [CustomerData].[CustomerID] = [Appointments].[CustomerID] " &
"WHERE [Appointments].[HairdresserID] = " & staffindex &
"ORDER BY [Appointments].[DateTimeStart] ASC"
ElseIf staffindex = 0 Then
sqlCommandString = "SELECT [Appointments].[DateTimeStart], [Appointments].[DateTimeEnd], [Appointments].[AppointmentID], [CustomerData].[Forename], [CustomerData].[Surname] " &
"FROM [Appointments] " &
"INNER JOIN [CustomerData] " &
"ON [CustomerData].[CustomerID] = [Appointments].[CustomerID] " &
"ORDER BY [Appointments].[DateTimeStart] ASC"
End If
ds_estrella.Clear()
GridViewConnection(sqlCommandString)
End Sub
Protected Sub GridViewConnection(ByVal sqlCommandString As String)
Staffgv.DataSource = Nothing
Staffgv.DataBind()
Dim dbConn As New SqlConnection(connString)
Try
dbConn.Open()
da_estrella = New SqlDataAdapter(sqlCommandString, dbConn)
da_estrella.Fill(ds_estrella, "StaffView")
Dim icount As Integer = ds_estrella.Tables(0).Rows.Count
Staffgv.DataSource = ds_estrella
If ds_estrella.Tables(0).Rows.Count > 0 Then
Staffgv.DataSource = ds_estrella
Staffgv.DataBind()
Else
Staffgv.DataSource = ds_estrella
Staffgv.DataBind()
Dim columncount As Integer = Staffgv.Rows(0).Cells.Count
'No data found in this case
End If
dbConn.Close()
Catch ex As Exception
lblDropdownstaff.Text = "FAILED"
End Try
End Sub
Protected Sub Staffgv_PageIndexChanging(ByVal sender As Object, e As GridViewPageEventArgs) Handles Staffgv.PageIndexChanging
Staffgv.PageIndex = e.NewPageIndex
End Sub
Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
If (e.CommandName = "FillStaffTables") Then
txtAppointmentID.Text = e.CommandArgument
End If
End Sub
答案 0 :(得分:0)
花了一些工作,但我使用了你的代码并构建了一个正在运行的web应用程序,所以我可以帮助调试它。您可能要做的第一件事是将以下行添加为Page_Load
If Me.IsPostBack Then Exit Sub
这将使您的整个Page_Load
块仅运行一次,以便gridview不会继续返回其原始状态。
例如,请点击&#34; Elaine&#34;按钮,然后单击&#34; ____&#34; gridview内的按钮。这将导致整个gridview返回其原始状态,但现在它应该保持过滤的gridview存在。
您可以在Me.IsPostBack
内向上/向下移动上述Page_Load
行,只需知道其下方的任何代码只会运行一次:首次加载页面时。
您应该做的第二件事是在Page_Load
txtAppointmentID.Text = String.Empty
应将放在 Me.IsPostBack
行之上。当页面重新加载时,这将清除文本框,以便当单击另一个按钮时,所述文本框不会保留其中的先前/陈旧值。
最后,在vb
文件的最顶部,添加以下行(如果您还没有)是一个好习惯
Option Strict On
Option Explicit On
这些选项会带来更多错误,这些错误通常很容易修复,如果保持不变,它们有时会导致非常微妙的错误/问题很难追查。这些选项通常可以帮助人们编写更好的代码。