0

我有 2 个名为 DDLTOC 和 DDLCase 的网格视图。我已使用 appenddatabounditems 将默认值插入 DDL。我的默认值为 ("Select Member Report ID")

<asp:DropDownList ID="DDLTOC" runat="server" style="margin-top: 0px;" OnSelectedIndexChanged="DDLTOC_SelectedIndexChanged" DataTextField="typeofcrime" DataValueField="typeofcrime" AutoPostBack="True" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Select Member Report ID</asp:ListItem>
</asp:DropDownList>

DDLC酶

<asp:DropDownList ID="DDLCase" runat="server" AutoPostBack="True" DataTextField="memberreportid" DataValueField="memberreportid" Height="16px" OnSelectedIndexChanged="DDLCase_SelectedIndexChanged" AppendDataBoundItems="true" >
<asp:ListItem Value="">Select Case</asp:ListItem>
</asp:DropDownList>

我已经在页面加载时将数据绑定插入到我的 DDLTOC 中,一旦 webapp 运行,它将显示从数据库中选择的值。

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            SqlConnection conn = new SqlConnection("Data Source=localhost;" +
                "Initial Catalog=project; Integrated Security = SSPI");

            SqlDataAdapter da = new SqlDataAdapter("SELECT distinct typeofcrime FROM MemberReport where handle='handled' AND caseprogress='settled'", conn);
            conn.Open();

            DataSet ds = new DataSet();
            da.Fill(ds);

            DDLTOC.DataSource = ds;
            DDLTOC.DataTextField = "typeofcrime";
            DDLTOC.DataValueField = "typeofcrime";
            DDLTOC.DataBind();                   

            conn.Close();

        }

    }

我还在 DDLTOC 上添加了另一个绑定

protected void DDLTOC_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"))
        {
            connAdd.Open();

            var sql = "SELECT memberreportid FROM MemberReport Where typeofcrime ='" + DDLTOC.SelectedValue + "' AND caseprogress='settled'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet ds2 = new DataSet();
                cmdAdd.Fill(ds2);

                DDLCase.DataSource = ds2;
                DDLCase.DataTextField = "memberreportid";
                DDLCase.DataValueField = "memberreportid";
                DDLCase.DataBind();

            }

            sql = "Select username, memberreportid, location, crdatetime, citizenreport, image1, image2, image3, image4, image5 from MemberReport where typeofcrime ='" + DDLTOC.SelectedItem.Text + "' and handle='handled'";
            using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
            {
                DataSet dsSel = new DataSet();
                cmdAdd.Fill(dsSel);
                GVCR.DataSource = dsSel;
                GVCR.DataBind();
            }

            connAdd.Close();
        }


    }

DDLTOC_SelectedIndex 中的第一个绑定,基本上是让 DDLCase 的值根据 DDLTOC 中选择的值显示在下拉列表中。第二个绑定会将数据库中的必要值显示到网格视图中。我有 2 个数据库值将显示在 DDLTOC、Gang & Robbery 中。因此,如果我要随机选择帮派,然后选择回我的默认值,然后选择帮派并返回选择默认值,它将在我的 DDLCase 上显示帮派的 DDLCase 值两次。

为什么会出现重复数据?

4

1 回答 1

1

在数据绑定之前DDLCase,只需清除其项目集合。DDLCase将您的数据绑定代码部分修改为:

DDLCase.Items.Clear();
DDLCase.DataSource = ds2;
DDLCase.DataTextField = "memberreportid";
DDLCase.DataValueField = "memberreportid";
DDLCase.DataBind();
DDLCase.Items.Insert(0, new ListItem("Select Case", ""));
DDLCase.SelectedIndex = 0;

或者,您可以EnableViewState="False"DDLCase标记中设置;在这种情况下,如果页面上发生任何其他回发(DDLToc例如,来自不会DDLCase再次填充的按钮),则数据DDLCase将丢失。它ViewStateDDLCase保持它以前的状态,以便在交叉请求中下拉项不会丢失。你只是添加到它的项目集合中,除非它的 ViewState 被禁用。

于 2013-05-30T01:05:27.640 回答