2

在此处输入图像描述

我有这个gridview,我试图打印出用户检查的任何列的MMBR_PROM_ID。

(默认.apsx)

Welcome to ASP.NET!

    </h2>

            <div style="width: 700px; height: 370px; overflow: auto; float: left;">
                <asp:GridView ID="GridView1" runat="server" HeaderStyle-CssClass="headerValue" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                <asp:TemplateField HeaderText="Generate">
                    <ItemTemplate>
                        <asp:CheckBox ID="grdViewCheck" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                </Columns>
                </asp:GridView>
                </div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Generate" />

</asp:Content>  

(默认.aspx.cs)

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FrontOffEntities tmpdb = new FrontOffEntities();

            List<MMBR_PROM> newListMMBR_Prom = tmpdb.MMBR_PROM.ToList();


            GridView1.DataSource = newListMMBR_Prom;
            GridView1.DataBind();
        }

    }  

所以我的目标是当我按下生成时,我希望能够将用户检查的所有 MMBR_PROM_ID 作为字符串打印出来。我对 aspnet 有点陌生,所以我很难应付语法

4

1 回答 1

2

根据您提到的要求,您可以尝试以下给定的代码以在“生成按钮单击”上从Gridview1获取MMBR_PROM_ID的值。

     //For every row in the grid
     foreach (GridViewRow r in GridView1.Rows)
        {
            //Find the checkbox in the current row being pointed named as grdViewCheck
            CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");

            //Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
            if (chk!=null && chk.Checked)
            {
                Response.Write(r.Cells[1].Text);
            }
        }

在这里, cells[1] 指的是特定行的单元格索引,在您的情况下,它是您要打印的MMBR_PROM_ID 。希望这可以帮助!

如果您正在寻找MMBR_PROM_ID的逗号分隔值,下面提到的代码将为您工作。

     //Declaration of string variable 
     string str="";

     //For every row in the grid
     foreach (GridViewRow r in GridView1.Rows)
        {
            //Find the checkbox in the current row being pointed named as grdViewCheck
            CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");

            //Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
            if (chk!=null && chk.Checked)
            {
                Response.Write(r.Cells[1].Text);
                //appending the text in the string variable with a comma
                str = str + r.Cells[1].Text + ", ";
            }
        }
      //Printing the comma seperated value for cells[1] which is MMBR_PROM_ID
      Response.Write(str);
于 2013-03-13T03:04:58.403 回答