【问题标题】:Store ID from a dropdown list下拉列表中的商店 ID
【发布时间】:2015-12-13 21:34:38
【问题描述】:

我已经为此工作了一段时间,但似乎无法弄清楚。因此,我有一个名为 ddStudent 的下拉列表,其中 textField 值来自我创建的名为 'Student 来自名为 'firstName' 的列的 sql 表。现在我使用“studentID”作为值字段。

这是我遇到问题的地方。我想执行以 studentID 作为参数的存储过程,但我不明白如何将实际的学生 ID 存储到参数中。

这是我目前所拥有的-

protected void btnRegister_Click(object sender, EventArgs e)
    {
        int selValClass = Convert.ToInt32(ddClassNames.SelectedValue);
        int selValStudent = Convert.ToInt32(ddStudents.SelectedValue);

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("procRegStudent", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@studentID", selValStudent);
            cmd.Parameters.AddWithValue("@classID", selValClass);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

        }

这是我的数据绑定代码-

 using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("SELECT studentID, firstName, lastName, ssn, address, city, state, zip FROM Student ", con);

            con.Open();

            ddStudents.DataSource = cmd.ExecuteReader();
            ddStudents.DataTextField = "firstName";
            ddStudents.DataValueField = "studentID";
            ddStudents.DataBind();

        }

现在我的问题是,当我选择它时,有没有办法可以从下拉列表中存储实际的 studentID?出于某种原因,当我这样做时,页面会重新加载并为我没有选择的学生选择第一个学生 ID。我不确定发生了什么。

谢谢。

编辑-

我想确保我澄清-

我的学生下拉列表中有 3 名学生。当我选择学生 3(学生 ID 为 3)时,下拉列表中的第一个学生被作为参数传递,我不知道为什么。

【问题讨论】:

  • 能否请您将 ddStudent DropDownList 的设计视图发给我,我想在回答之前先检查一下:)!
  • @FreedomDeveloper 你指的是与之关联的表吗?
  • 不,我的意思是你的下拉列表控件的 HTML 视图,类似于:
  • @FreedomDeveloper 给你。见here
  • @FreedomDeveloper 这是aspx。

标签: c# sql dropdown


【解决方案1】:

这个问题对于 Dropdownlist 控件是合乎逻辑的,因为您的下拉列表在从服务器返回时错过了要呈现的默认项。灵魂非常简单,您应该添加一个默认列表项作为页面加载时的默认值

在这里我做了一个简单的例子来说明我们如何解决这个问题:

在 Page.aspx 中:

    <asp:DropDownList ID="ddStudents" runat="server" DataTextField = "firstName" DataValueField = "studentID" AppendDataBoundItems="true">
        <asp:ListItem Text="===Select Student===" Value="0"></asp:ListItem>

    </asp:DropDownList> 

    <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" />
    <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>

在 Page.aspx.cs 中:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();

        }

    }

    private void BindDropDown()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("studentID", typeof(int));
        dt.Columns.Add("firstName", typeof(string));

        dt.Rows.Add(dt.NewRow());
        dt.Rows[0]["studentID"] = 1;
        dt.Rows[0]["firstName"] = "Markus";


        dt.Rows.Add(dt.NewRow());
        dt.Rows[1]["studentID"] = 2;
        dt.Rows[1]["firstName"] = "Arthur";


        ddStudents.DataSource = dt;
        ddStudents.DataBind();
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (ddStudents.SelectedIndex > 0)
        {
            lblMessage.Text = String.Format("You selected {0} with Number : {1}", ddStudents.SelectedItem.Text,ddStudents.SelectedValue);
        }
    }

提示:我将下拉列表的 AppendDataBoundItems 属性标记为 true 只是为了告诉下拉列表在数据绑定时包含默认列表项..

我希望我的代码可以帮助您解决问题:)

【讨论】:

    【解决方案2】:

    试试这个

    int selValStudent = int.Parse(this.ddStudents.SelectedValue);

    把你的

    BindDropDown();

    进入您的回发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      相关资源
      最近更新 更多