c#asp.net,事件后动态下拉列表,在动态Gridview中保存数据

时间:2016-01-21 10:51:08

标签: c# asp.net gridview drop-down-menu

我制作了一个简单的代码来向您展示我的问题。

我有一个带有各种控件的动态网格视图。

我希望第二个下拉列表能够填充特定数据,这些数据取决于第一个下拉列表的选定索引。

例如,让我们说我们有droplist1有4个选项,所以droplist1.SelectedIndex是1,2,3,4。

当我选择(选项)2时,在select和droplist2上触发的事件会根据droplist1.SelectedIndex 进行droplist2.datasource = ,(我希望你理解)。

我已设法在droplist1.SelectedIndex事件之后填充droplist2但是我无法获取要保存的值...

我在调试器上看到它一直持续到一点但在此之后我的屏幕上显示为空行...

这是我的HTML



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" ClientIDMode="Static" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="height: 337px">
            <asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                    <asp:TemplateField HeaderText="Column1">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Category" DataTextField="ExpenseCategoryName" DataValueField="ExpenseCategoryName" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                                <asp:ListItem Value="-1">Select</asp:ListItem>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Category" runat="server" ConnectionString="<%$ ConnectionStrings:Expenses_NewConnectionString %>" SelectCommand="SELECT [ExpenseCategoryName] FROM [ExpenseCategories]"></asp:SqlDataSource>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Column2">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                                OnClick="ButtonAdd_Click" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>


            <br />

            <br />
            <br />
        </div>
        </form>
</body>
</html>
&#13;
&#13;
&#13;

和我的C#代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            setInitialRow();
        }
    }



    protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }



    private void setInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;


        dt.Rows.Add(dr);


        ViewState["CurrentTable"] = dt;
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }







    private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {

                    DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = ddl1.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Column2"] = ddl2.SelectedValue;

                    rowIndex++;

                }

                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
            else
            {
                Response.Write("Viewstate is null");
            }
            SetPreviousData();
        }
    }






    private void SetPreviousData()
    {

        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {

            DataTable dt = (DataTable)ViewState["CurrentTable"];

            if (dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
                    DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");

                    try
                    {

                        ddl1.Text = dt.Rows[i]["Column1"].ToString();
                        ddl2.Text = dt.Rows[i]["Column2"].ToString();

                    }
                    catch (Exception ex)
                    { }
                    rowIndex++;
                }
            }
        }
    }




    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ViewState["CurrentTable"] != null)
        {

            DataTable dt = (DataTable)ViewState["CurrentTable"];

            if (dt.Rows.Count > 0)
            {
                DropDownList ddl1 = (DropDownList)Gridview1.Rows[(dt.Rows.Count - 1)].Cells[1].FindControl("DropDownList1");
                DropDownList ddl2 = (DropDownList)Gridview1.Rows[(dt.Rows.Count - 1)].Cells[2].FindControl("DropDownList2");

                FillDropDownList(ddl2);

            }
        }
    }


    private ArrayList GetData()
    {

        ArrayList arr = new ArrayList();
        arr.Add(new ListItem("Item1", "1"));
        arr.Add(new ListItem("Item2", "2"));
        arr.Add(new ListItem("Item3", "3"));
        arr.Add(new ListItem("Item4", "4"));
        arr.Add(new ListItem("Item5", "5"));
        return arr;

    }



    private void FillDropDownList(DropDownList ddl)
    {
        ArrayList arr = GetData();
        foreach (ListItem item in arr)
        {
            ddl.Items.Add(item);
        }
    }

}
}

0 个答案:

没有答案