【问题标题】:Count the selected dropdowns in gridview计算gridview中选定的下拉列表
【发布时间】:2014-02-13 04:24:23
【问题描述】:

我有一个gridview,它在一列中包含下拉列表框,比如说下拉列表有值'A','B' and 'C'。另外,我有一个student names 的专栏。(假设有大约 10 个学生)。

我想知道号码的数量。从下拉列表中选择“A”作为值的学生。

这可能吗?

编辑: 这是我的下拉菜单的代码。我还没有开始验证。

<asp:TemplateField HeaderText="Choose Employer" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="15%">
    <ItemTemplate>
        <asp:DropDownList runat="server" ID="cboEmployer" CssClass="textEntry" AutoPostBack="true" OnSelectedIndexChanged="cboEmployer_SelectedIndexChanged" onclick="javascript:shouldsubmit=false;" Width="80%"></asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

【问题讨论】:

  • 显示你的save按钮的代码
  • 请检查我的编辑。

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


【解决方案1】:

在 GridView 之后创建一个按钮。在提交点击事件中,您可以访问 Gridviews 下拉列表值

protected void btnSubmit_Click(object sender , eventArgs e)
{
  int count=0;
  foreach(GridViewRow row in gridViewId.Rows)
  {
    DropDownList ddl = (DropDownList)row.FindControl("cboEmployer");
    string selectedValue = ddl.SelectedValue.ToString();
    if(selectedValue  == "A")
       count++;
  }
  Response.Write("Number of values selected as A : "+ count);
}

【讨论】:

    【解决方案2】:

    这是我的建议: 如果您在提交表单并将下拉值保存在数据库中之后对此进行解释,那么您始终可以根据使用简单计数(*)查询选择该下拉值的学生的姓名从数据库中提取该值。

    【讨论】:

    • 不,我在保存之前这样做。
    • 那你能把代码显示在 JSfiddle 或 plunker 中吗?
    【解决方案3】:

    在您的代码中,我不明白 onclick="javascript:shouldsubmit=false; 的用法 以及为什么您制作了AutoPostBack="true"

    我认为你应该使用简单的下拉列表

    使用这个,

     <asp:DropDownList runat="server" ID="cboEmployer" CssClass="textEntry" Width="80%">  
     </asp:DropDownList>
    

    不是这个

     <asp:DropDownList runat="server" ID="cboEmployer" CssClass="textEntry" 
      AutoPostBack="true" OnSelectedIndexChanged="cboEmployer_SelectedIndexChanged" 
      onclick="javascript:shouldsubmit=false;" Width="80%"></asp:DropDownList>
    

    以上述方式制作其他下拉菜单。 save按钮计数代码如下:

     protected void btnSave_Click(object sender, eventArgs e)
        {
            int countA = 0, countB = 0, countC = 0;
    
            foreach (GridViewRow row in gridViewId.Rows)
            {
                DropDownList cboEmployer = (DropDownList)row.FindControl("cboEmployer");
    
                string selectedValue = cboEmployer.SelectedValue;
    
                if (selectedValue == "A")
                {
                    countA++;
                }
                else if (selectedValue == "B")
                {
                    countB++;
                }
                else if (selectedValue == "C")
                {
                    countC++;
                }
            }
            //Response.Write("Count A :" + countA + "<br/>" +"Count B :" + countB + "Count C :" + countC);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多