asp.net 未从后端文件的下拉列表中获取数据

vaqhlq81  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(110)

我一直试图找出我的后端脚本的问题了一天,它一直有点令人沮丧。基本上,后端脚本没有从我的网页上的下拉列表中抓取数据。它可以在按下按钮时注册,并可以保存到SQL数据库,但它不会抓取下拉列表中的数据。像“部门1”我试过几种方法,但似乎总是空白。
我在网页上有一个按钮和两个下拉唐斯,由以下人员发起:

<form id="form1" runat="server">
                <label for="department">Department:<span class="required">*</span></label>
                <asp:DropDownList ID="departmentDropDown" runat="server" EnableViewState="true" required></asp:DropDownList>

                <label for="partNumber">Part Number:<span class="required">*</span></label>
                <asp:DropDownList ID="partNumberDropDown" runat="server" required></asp:DropDownList>

                <asp:Button runat="server" ID="myButton" Text="Click me" OnClick="myButton_Click" />
            </form>

字符串
我有一个后端:

protected void myButton_Click(object sender, EventArgs e)
    {
        // Get selected values from dropdowns
        string selectedDepartment = departmentDropDown.SelectedValue;
        string selectedPartNumber = partNumberDropDown.SelectedValue;

        // Write the selected values to the response
        Response.Write("Selected Department:" +  selectedDepartment + "<br/>");
        Response.Write("sent by:" +  sender + "<br/>");
        Response.Write("sent by:" +  sender + "<br/>");
        Response.Write("Selected Part Number: "+ selectedPartNumber+ "<br/>");

        Response.Write("<br/> NOT SAVED!");
    }


我一直在尝试从后端的几个不同的React,看看发生了什么事,但它似乎总是做同样的事情。我很想使这一工作!
我目前如何填充下拉列表:

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        PopulateDepartmentDropdown();
        PopulatePartNumberDropdown();
    }

    private void PopulateDepartmentDropdown()
    {
        // Replace this with your logic to fetch department data from a data source
        DataTable departmentData = GetStaticDepartmentsData();
        //DataTable departmentData = GetDepartmentsData();

        // Clear existing items in the dropdown
        departmentDropDown.Items.Clear();

        // Add an initial blank item if needed
        departmentDropDown.Items.Add(new ListItem("-- Select Department --", ""));

        // Add items to the dropdown from the DataTable
        foreach (DataRow row in departmentData.Rows)
        {
            // Assuming that 'DepartmentIDColumn' and 'DepartmentNameColumn' are column names in your DataTable
            string departmentID = row["department"].ToString();
            string departmentName = row["department"].ToString();

            departmentDropDown.Items.Add(new ListItem(departmentName, departmentID));
        }
    }

    private void PopulatePartNumberDropdown()
    {
        // Replace this with your logic to fetch part number data from a data source
        //DataTable partNumberData = GetPartNumbersData();
        DataTable partNumberData = GetStaticPartNumbersData();

        // Clear existing items in the dropdown
        partNumberDropDown.Items.Clear();

        // Add an initial blank item if needed
        partNumberDropDown.Items.Add(new ListItem("-- Select Part Number --", ""));

        // Add items to the dropdown from the DataTable
        foreach (DataRow row in partNumberData.Rows)
        {
            // Assuming that 'PartNumberIDColumn' and 'PartNumberNameColumn' are column names in your DataTable
            string partNumberID = row["partnumbers"].ToString();
            string partNumberName = row["partnumbers"].ToString();

            partNumberDropDown.Items.Add(new ListItem(partNumberName, partNumberID));
        }
    }
    private DataTable GetStaticDepartmentsData()
    {
        DataTable departmentsData = new DataTable();
        departmentsData.Columns.Add("department");

        // Add static department data
        departmentsData.Rows.Add("DepartmentA");
        departmentsData.Rows.Add("DepartmentB");
        departmentsData.Rows.Add("DepartmentC");

        return departmentsData;
    }
    
    private DataTable GetStaticPartNumbersData()
    {
        DataTable partNumbersData = new DataTable();
        partNumbersData.Columns.Add("partnumbers");

        // Add static part number data
        partNumbersData.Rows.Add("12345");
        partNumbersData.Rows.Add("67890");
        partNumbersData.Rows.Add("ABCDE");

        return partNumbersData;
    }

gstyhher

gstyhher1#

在你的标记中有一些奇怪的地方已经被清理掉了。带有EnableViewState = "true"的标签已经被删除了,因为这里不需要它。还有几个地方的工作required只是在你的asp标签中。这可能是有效的,但是我以前从来没有见过它,而且在短时间内找不到它的参考。最后,作为个人喜好的问题,如果你没有在标签里面写东西,让它们自动关闭以便于阅读。

<form id="form1" runat="server">
    <label for="department">Department:<span class="required">*</span></label>
    <asp:DropDownList ID="departmentDropDown" runat="server" />

    <label for="partNumber">Part Number:<span class="required">*</span></label>
    <asp:DropDownList ID="partNumberDropDown" runat="server" />

    <asp:Button runat="server" ID="myButton" Text="Click me" OnClick="myButton_Click" />
</form>

字符串
你的Page_load写东西的方式是错误的。而不是检查isPostback = true,你需要isPostBack = false,因为我们正在寻找初始页面加载。我也清理了你如何数据绑定你的控件一点。你不需要循环数据表。简单地分配它,设置文本/值字段,并调用数据绑定,如你所见。
值得注意的一点是,在构建临时数据源时,请使其与您希望从主数据源获得的格式保持一致。我调整了列名,并根据您在相应方法中的注解向您正在创建的数据表添加了一列。如果这不是您的列的名称,则可以进行调整。

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //do postback logic here
        } else {
            // do initial page load logic here
            PopulateDepartmentDropdown();
            PopulatePartNumberDropdown();
        }
    }

    private void PopulateDepartmentDropdown()
    {
        // Replace this with your logic to fetch department data from a data source
        DataTable departmentData = GetStaticDepartmentsData();
        //DataTable departmentData = GetDepartmentsData();

        departmentDropDown.Items.Clear(); // Clear existing items in the dropdown

        departmentDropDown.Items.Add(new ListItem("-- Select Department --", "")); // Add an initial blank item if needed

            // Assuming that 'DepartmentID' and 'DepartmentName' are column names in your DataTable
            departmentDropDown.DataValueField = "departmentID";
            departmentDropdown.DataTextField = "departmentName";
            departmentDropdown.DataSource = departmentData;
            departmentDropdown.DataBind();
    }

    private void PopulatePartNumberDropdown()
    {
        // Replace this with your logic to fetch part number data from a data source
        //DataTable partNumberData = GetPartNumbersData();
        DataTable partNumberData = GetStaticPartNumbersData();

        partNumberDropDown.Items.Clear(); // Clear existing items in the dropdown

        partNumberDropDown.Items.Add(new ListItem("-- Select Part Number --", "")); // Add an initial blank item

            // Assuming that 'PartNumberID' and 'PartNumberName' are column names in your DataTable
        partNumberDropDown.DataSource = partNumberData;
        partNumberDropDown.DataTextField = "partNumberName";
        partNumberDropDown.DataValueField = "partNumberID";
        partNumberDropDown.DataBind();
    }

    private DataTable GetStaticDepartmentsData()
    {
        DataTable departmentsData = new DataTable();
        departmentsData.Columns.Add("departmentID");
        departmentsData.Columns.Add("departmentName");

        // Add static department data
        departmentsData.Rows.Add("DepartmentA","DepartmentA");
        departmentsData.Rows.Add("DepartmentB","DepartmentB");
        departmentsData.Rows.Add("DepartmentC","DepartmentC");

        return departmentsData;
    }
    
    private DataTable GetStaticPartNumbersData()
    {
        DataTable partNumbersData = new DataTable();
        partNumbersData.Columns.Add("partNumberID");
        partNumbersData.Columns.Add("partNumberName");

        // Add static part number data
        partNumbersData.Rows.Add("12345","12345");
        partNumbersData.Rows.Add("67890","67890");
        partNumbersData.Rows.Add("ABCDE","ABCDE");

        return partNumbersData;
    }

相关问题