我在页面上放置了一个GridView控件,然后在page_load事件里通过编程为GridView绑定了数据然后在CS文件里写了一个方法    protected string getId()
    {
        string pid = "";
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
            if (chkBox.Checked)
            {
                if (pid.Equals(""))
                    pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
                else
                    pid +=","+GridView1.DataKeys[row.RowIndex].Value.ToString(); 
            }
        }
        return pid;
    }
这个方法在page_load事件中的ispostback中可以获取到GridView所有选择行的主键值,然而在按钮的点击事件里却一直获取不到,这个原因一直很令我郁闷,请教各位大侠了,GridView里的选择列是通过添加一个模板列,然后在模板项中加入一个CheckBox而形成的截图如下:

解决方案 »

  1.   

    建议为模板行添加一个hiddenfield控件保存主键值,在通过findcontrol读取出来,
      

  2.   

    protected   string   getId() 
            { 
                    string   pid   =   ""; 
                    foreach   (GridViewRow   row   in   GridView1.Rows) 
                    { 
                            CheckBox   chkBox   =   row.FindControl("CheckBox1")   as   CheckBox; 
                            if   (chkBox.Checked) 
                            { 
                                    if   (pid.Equals("")) 
                                            pid   =   GridView1.DataKeys[row.RowIndex].Value.ToString(); 
                                    else 
                                            pid   +=","+GridView1.DataKeys[row.RowIndex].Value.ToString();   
                            } 
                    } 
                    return   pid; 
            } 
    又是写在什么事件里的叱?
      

  3.   

    更正:绑定时才需要辨别 RowType,回发时不用,晕了楼主能保证指定了GridView上的DataKeys属性,而且只有一个?!把GridView绑定代码贴出来看看!
      

  4.   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string IdList = this.GetID();
                Response.Write(IdList); //可以获取主键值并显示出来
            }
            this.GetData();
        }   /// <summary>
       /// 为GridView绑定数据
       /// </summary>
        protected void GetData()
        {
            this.GridView1.DataSource = Manage.BLL.VisitGroups(3);
            this.GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string IdList = this.GetID();
            Response.Write(IdList);    //显示出来的是空白的
        }    /// <summary>
        /// 获取GridView中的主键行
        /// </summary>
        /// <returns></returns>
        protected string getId()
        {
            string pid = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkBox = row.FindControl("CheckBox1") as CheckBox;
                if (chkBox.Checked)
                {
                    if (pid.Equals(""))
                        pid = GridView1.DataKeys[row.RowIndex].Value.ToString();
                    else
                        pid += "," + GridView1.DataKeys[row.RowIndex].Value.ToString();
                }
            }
            return pid;
        } 
      

  5.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            string IdList = this.GetID();
            Response.Write(IdList);    //显示出来的是空白的
        }
    问题出在this引用上,在按钮事件中,this引用的是sender.也就是button,怎么可以获取GetID呢
    而在protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string IdList = this.GetID();
                Response.Write(IdList); //可以获取主键值并显示出来
            }
            this.GetData();
        }中,sender为page 对象,是可以获取的.
    楼主要获得IDlist何不用hiddenfield储存呢,string pid = "";
      

  6.   

    不好意思,不才 刚刚学用NET,C#也是刚学会点儿皮毛我的意思是说想把获取GridView每行的ID字段值获取出来,直接写成一个方法GetID,然后在点击事件里调用这个方法,效果应该也是一样的吧非常感谢提刑官在8楼的回答,我再试一试,也希望各位朋友能够继续给予解答
      

  7.   

    首先要给gridview设置主键,主键是你的数据库的主键
    然后,取值的时候要判断rowtype
      

  8.   

    直接取搜索每行的CHECKBOX控件不行吗?可不可以给个示例啊
      

  9.   

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.ClickDim i As Integer = 0 For Each r As GridViewRow In GridView1.Rows 
    If r.RowType = DataControlRowType.DataRow ThenIf CType(r.FindControl("chkRow"), CheckBox).Checked = True ThenMsgBox(GridView1.DataKeys(r.RowIndex).Values.Item(0))i = i + 1MsgBox(CType(r.FindControl("lnkedit"), LinkButton).CommandArgument)End IfEnd IfNextGetchkValue()If (MsgBox(i & " records will be delete !", MsgBoxStyle.YesNo) = MsgBoxResult.Ok) ThenTrace.Write(i & " Records deleted") 
    End IfEnd Sub
      

  10.   

    全选:
    js:
    var elems = document.getElementById("<%= Yourgridview.ClientID %>").getElementsByTagName("input");
    for(var i=0;i<elems.length;i++){
      if(elems[i].type=="checkbox")elems[i].checked = true;

      

  11.   

    看看你的程序在post之后是否能维持其操作状态?
      

  12.   

    你获取主键应该是在GridView绑定数据之后吧,但从你的代码看来你似乎是反了。
      

  13.   

    这个一直是很令人头痛的问题,ASPX文件源码如下:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="选择">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        
        </div>
        </form>
    </body>
    </html>
      

  14.   

    添加一个,asp:HiddenField ID="hiddenField1" runat="server" Value='<%# Eval("主键") %>' />然后,参考下面函数:protected string GetAllCheckedRowDataKeyNames(GridView gv, string checkBoxId, string hiddenFieldId)
            {
                string strTempValue = string.Empty;
                foreach (GridViewRow gvr in gv.Rows)
                {
                    CheckBox cb = (CheckBox)gvr.FindControl(checkBoxId);
                    if (cb.Checked)
                    {
                        strTempValue = strTempValue + "," + ((HiddenField)gvr.FindControl(hiddenFieldId)).Value;
                    }
                }
                return strTempValue;
            }
      

  15.   

    楼主注意一下 ispostback,当你提交之后,首先进入page_load ,这样对页面是一个初始化  设置断点跟踪一下,看看
    问题就再ispostback