1

我有一个用于过滤类别项目的 DDL。它工作完美。我将选定的值存储到这样的会话中:

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Categorie") = DropDownList1.SelectedValue
End Sub

并像这样在 Page_Load 上回忆它:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Cookie()

        If Not Page.IsPostBack Then           
            DropDownList1.SelectedValue = Session("Categorie")
        End If
    End Sub

DDL在它自己的 DLL 上显示值,但它没有将它绑定到 GridView。

您还可以在 txtboxes 上过滤它,我以完全相同的方式保存值:

Protected Sub txtKlant_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Klant") = txtKlant.Text
End Sub

并且还在 page_Load 上调用它们,但是这些值立即激活并显示在Klant(客户)上过滤的数据

我的整个代码:

页面加载

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Cookie()

    If Not Page.IsPostBack Then           
        DropDownList1.SelectedValue = Session("Categorie")
        txtKlant.Text = Session("Klant")
        txtWebsite.Text = Session("Website")
        txtTitel.Text = Session("Titel")
        GridView1.DataBind()
    End If
End Sub

会话

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Categorie") = DropDownList1.SelectedValue
End Sub

Protected Sub txtKlant_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Klant") = txtKlant.Text
End Sub

Protected Sub txtWebsite_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Website") = txtWebsite.Text
End Sub

Protected Sub txtTitel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("Titel") = txtTitel.Text
End Sub

我没有收到任何错误。过滤确实有效,但是当我转到其他页面时,按后退按钮它不会保留会话的选定值。因此,类别的选择丢失了。但它确实保留了以相同方式处理的文本框的值

/编辑//

网格视图:

    <asp:GridView ID="GridView1" 
        runat="server" 
        AutoGenerateColumns="False" 
        DataKeyNames="ID" 
        gridlines="None"
        cellpadding="15"
        width="980px"   
        ItemStyle-backcolor="#ebecf0"
        AlternatingItemStyle-backcolor="#ebecf0" 
        AllowPaging="True" 
        PageSize="4" 
        onpageindexchanging="GridView1_PageIndexChanging" 
        datasourceid="SqlDataSource2"
        > 

//BOUNDED ITEMS//

</GridView>

SQL 数据源

SelectCommand="SELECT * FROM [tbl_Project] INNER JOIN tbl_Cat      ON tbl_Project.CatID = tbl_Cat.Cat_ID 
INNER JOIN tbl_Klant    ON tbl_Project.KlantID = tbl_Klant.Klant_ID 
WHERE (([Titel] LIKE '%' + @Titel + '%') 
AND  ([CatID] = CASE WHEN @CatID = -1 THEN [CatID] ELSE @CatID END) AND ([Bedrijf] LIKE '%' + @Bedrijf + '%') 
AND ([Website] LIKE '%' + @Website + '%'))"  

deletecommand="DELETE FROM [tbl_Project] WHERE [ID] = @original_ID" 
OldValuesParameterFormatString="original_{0}" >

<DeleteParameters>
    <asp:Parameter Name="original_ID" Type="Int32" />
</DeleteParameters>


<SelectParameters>
    <asp:ControlParameter ControlID="txtTitel" DefaultValue="*" Name="Titel" 
    PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />

    <asp:ControlParameter ControlID="txtKlant" DefaultValue="*" Name="Bedrijf" 
    PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />


    <asp:ControlParameter ControlID="txtWebsite" DefaultValue="*" Name="Website" 
    PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />

    <asp:ControlParameter ControlID="DropDownList1" DefaultValue="1" 
    Name="CatID" PropertyName="SelectedValue" Type="Int32"                      ConvertEmptyStringToNull="False" />    
4

2 回答 2

2

我找到了一个非常简单的解决方法。今天早上我有一个清晰的时刻。固定如下:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Cookie()

    If Not Page.IsPostBack Then           
        DropDownList1.SelectedValue = Session("Categorie")
        txtKlant.Text = Session("Klant")
        txtWebsite.Text = Session("Website")
        txtTitel.Text = Session("Titel")
        GridView1.DataBind()
    End If
End Sub

这是我第一个工作的页面加载。

这是我的新的,效果很好。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Cookie()

    If Not Page.IsPostBack Then
        DropDownList1.SelectedValue = Session("Categorie")
        If DropDownList1.SelectedValue = Session("Categorie") = True Then
            GridView1.DataBind()

            txtKlant.Text = Session("Klant")
            txtWebsite.Text = Session("Website")
            txtTitel.Text = Session("Titel")
            GridView1.DataBind()
        End If
    End If
End Sub
于 2013-06-10T11:25:45.257 回答
1

我在您的代码中看不到您为数据绑定创建事件处理程序的位置。

我从 MSDN 中获取了一些示例,以展示您需要的步骤。

Function CreateDataSource() As ICollection

  'put your data source in here

End Function 'CreateDataSource


'Declaration
Public Event ItemDataBound As DataGridItemEventHandler

    Sub Item_Bound(sender As Object, e As DataGridItemEventArgs)

    Label1.Text = Label1.Text & " " & e.Item.ItemIndex
End Sub 'Item_Bound 


  Sub Page_Load(sender As Object, e As EventArgs)

     ' Manually register the event-handling method for the  
     ' ItemDataBound event of the DataGrid control.
     AddHandler ItemsGrid.ItemDataBound, AddressOf Item_Bound

     ' Load sample data only once, when the page is first loaded.
     If Not IsPostBack Then

        ItemsGrid.DataSource = CreateDataSource()
        ItemsGrid.DataBind()

     End If

  End Sub

我建议你看看这个链接,它太详细了,在这里重申一下。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.itemdatabound.aspx

如果这不能解决您的问题,请告诉我。

于 2013-06-07T09:48:15.743 回答