为了加入多选
我在页面代码中加入了:
<asp:DataGrid id="DataGrid1" runat="server">
   <Columns>
<asp:TemplateColumn>
<ItemTemplate>
 <input type="hidden" id="SelectedID" runat="server" 
value='<%# Container.ItemIndex%>'  NAME="SelectedID"/>
<asp:CheckBox ID="chkExport" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
  </Columns>
</asp:DataGrid>在cs文件中写入:
foreach(DataGridItem di in this.DataGrid1.Items) 

if(((CheckBox)di.FindControl("chkExport")).Checked==true) 
   { 
  this.Label8.Text+=this.DataGrid1.DataKeys[int.Parse(((HtmlInputHidden)di.FindControl("SelectedID")).Value)]+"<br>"; 
   } 
} 但是选项选中,调试的时候都不进这个if的区域,郁闷很久了!!!!!
求大家救救我
!!

解决方案 »

  1.   

    question: how are you doing your initial databinding?if (!IsPostBack)
    {
      this.DataGrid1.DataSource = ..;
      this.DataGrid1.DataBind();
    }
      

  2.   

    Private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs)
    {
        if(e.Item.ItemIndex <> -1)
        {
           if(((CheckBox)di.FindControl("chkExport")).Checked==true) 
           { 
              this.Label8.Text+=this.DataGrid1.DataKeys[int.Parse(((HtmlInputHidden)di.FindControl("SelectedID")).Value)]+"<br>"; 
           }
        }
    }
    在这里处理
      

  3.   

    databind()和datasource都没问题的
    我加了个按钮,没道理选中也不进条件的呀能告诉我措在那里?
      

  4.   

    用FindControl就难说了.
    这样试试:
    CheckBox cb = (CheckBox)di.Cells[*].Controls[*];
      

  5.   

    你这个按钮在datagrid内部,还是外?
      

  6.   

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim Conn As New OleDbConnection("Data Source=""" & Me.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings("database")) & """;Provider=""Microsoft.Jet.OLEDB.4.0"";User ID=Admin")
            Dim i As Integer
            Me.Label5.Text = ""
            Conn.Open()
            For i = 0 To Me.DataGrid1.Items.Count - 1
                Dim chk As CheckBox = CType(Me.DataGrid1.Items.Item(i).FindControl("checkbox1"), CheckBox)
                Dim chkstr As String = chk.Checked
                Dim mycom As New OleDbCommand("UPDATE T_MailRecommendSmtp SET IsEnabled = " & chkstr & " WHERE (ID = " & Me.DataGrid1.Items.Item(i).Cells(0).Text & ")", Conn)
                If mycom.ExecuteNonQuery > 0 Then                Me.Label2.Text = "操作已经成功!"            End If
            Next
            Conn.Close()
        End Sub
      

  7.   

    Dim chk As CheckBox = CType(Me.DataGrid1.Items.Item(i).FindControl("checkbox1"), CheckBox)
      

  8.   

    用循环 for i  as integer = 0 to datagrid.items.count -1
    CType(Me.DataGrid1.Items.Item(i).FindControl("checkbox1"), CheckBox)
      

  9.   

    贴出PageLoad的时候的代码,确定在PostBack时候没有重新绑定?能跟踪到foreach一行吗
      

  10.   

    同意saucer(思归):if (!IsPostBack)
    {
      this.DataGrid1.DataSource = ..;
      this.DataGrid1.DataBind();
    }
    看看是不是这样,这样的问题很多情况下是绑定问题
      

  11.   

    我在pageload里面写了很多:
    private void Page_Load(object sender, System.EventArgs e) 
    {


    L_tname.Text = Request.Cookies["InfoPlat_UserNo"].Value;
    String command = "select chname from course,opencourse where course.id=opencourse.courseid and opencourse.teachername = "+"'"+L_tname.Text+"'";
    DataSet myDataset = pubsql.sql(command);
    for(int i = 0 ;i<myDataset.Tables[0].Rows.Count;i++)
    {
    D_tCourse.Items.Add(myDataset.Tables[0].Rows[i].ItemArray.GetValue(0).ToString());
    }
    ///////////////////////////////////////////////
    command = "select classtime from opencourse where teachername = "+"'"+L_tname.Text+"'"+" and opencourse.courseid = (select id from course where course.chname = "+"'"+D_tCourse.SelectedItem.Text+"'"+")";
    myDataset.Reset();
    myDataset = pubsql.sql(command);
    for (int i= 0 ;i<myDataset.Tables[0].Rows.Count;i++)
    {
    string [] split=myDataset.Tables[0].Rows[i].ItemArray.GetValue(0).ToString().Split(new Char [] {' '});
    for(int j= 0; j<split.Length;j++)
    {
    D_tCoursetime.Items.Add(split[j]);
    }
    }
    ////////////////////////////////////////
    command = "select term from opencourse,course where opencourse.teachername = "+"'"+L_tname.Text+"'"+" and course.id=opencourse.courseid and course.chname = "+"'"+D_tCourse.SelectedItem.Text+"'";
    myDataset.Reset();
    myDataset = pubsql.sql(command);
    for (int i= 0 ;i<myDataset.Tables[0].Rows.Count;i++)
    {
    string [] split=myDataset.Tables[0].Rows[i].ItemArray.GetValue(0).ToString().Split(new Char [] {' '});
    for(int j= 0; j<split.Length;j++)
    {
    D_tTerm.Items.Add(split[j]);
    }
    }
    /////////////////////////////////////////
    command = "select stuno as '学号',student.chname as '姓名',sex as '性别',student.grade as '年级' from student,stuselcourse,opencourse,course where stuselcourse.stuid=student.id and stuselcourse.opencourseid=opencourse.id and opencourse.teachername = "+"'"+L_tname.Text+"'"+" and course.chname="+"'"+D_tCourse.SelectedItem.Text+"'"+" and opencourse.term="+"'"+D_tTerm.SelectedItem.Text+"'"+" and course.id=opencourse.courseid order by student.stuno";
    // command = "select stuno from student";
    DataGrid1.DataSource = pubsql.sql(command);
    DataGrid1.DataBind();
    }foreach能执行,di.cell[].text也能得到,但是加出来的checkbox就是访问不到!!!
      

  12.   

    楼主好好理解一下IsPostBack吧,你的代码有点乱啊
      

  13.   

    ShowDetail.aspx?id=FF130C7F-3650-4DA6-8943-8AA4AF3E3459你参考下这个例子吧。你的代码太乱了
      

  14.   

    就是思归说的这个问题啊
    修改为
    if(!IsPostBack){
    DataGrid1.DataSource = pubsql.sql(command);
    DataGrid1.DataBind();
    }
    否则在PostBack的时候重新绑定导致你选择的丢失
      

  15.   

    If(!IsPostBack),你在Page_Load 中有过绑定,checkbox里的选择项都是刷新过的了!
    好象是哦!
      

  16.   

    同意saucer(思归)
    我以前也经常遇到这个问题1`
      

  17.   

    我同意以上的看法但是我以前也没isposeback阿,为什么以前可以
      

  18.   

    是不是你的checkbox没有设置autopostback?