后台代码如下:   先用DropDownList获得课程名称,在绑定Grid View数据,再Grid View里的TextBox输入成绩后点击 btnSubmit 来获取输入的成绩,但是就是显示不出来,也没提示错误,测试了btnSubmit里的语句,也没有错,不知道什么问题,大侠帮忙看看阿?
public partial class teacherSubmitScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null )
        {
            Response.Redirect("Login.aspx");
        }
        if (!IsPostBack)
        {
            BindDDL();
        }
        BindGridView();    
    }
    private void BindDDL()
    {
        string strconnection = ConfigurationManager.ConnectionStrings["zilong"].ConnectionString;
        SqlConnection conn = new SqlConnection(strconnection);
        string SqlStr = "select distinct Course.courseID,Course.courseName from Elect,Course where Elect.teaID=Course.teaID and Elect.teaID='" + Session["userName"].ToString() + "'"; //查询所教授的课程
         DataSet ds = new DataSet();
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
        da.Fill(ds);                       
        conn.Close();
         ddl.DataSource = ds.Tables[0].DefaultView;
        ddl.DataTextField = "courseName";         
         ddl.DataValueField = "courseID";          
         ddl.DataBind();
    }
    private void BindGridView()
    {
        string strconnection = ConfigurationManager.ConnectionStrings["zilong"].ConnectionString;
        SqlConnection conn = new SqlConnection(strconnection);
        GridView1.Visible = true;
        string Sqlstr = "select * from Student,Elect where Student.stuID=Elect.stuID and Elect.courseID='" + ddl.SelectedValue + "'";
        DataSet ds = new DataSet();
               try
                {
                    if (conn.State.ToString() == "Closed")
                        conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
                    da.Fill(ds);
                    GridView1.DataSource = ds.Tables[0].DefaultView;
                    GridView1.DataBind();
                }
                catch{ }
                finally
                { conn.Close();}
            }
        }
}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string text = ((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.ToString;
            Response.Write(text);
        }
    } 
}

解决方案 »

  1.   


    string text = "";
    for (int i = 0; i < GridView1.Rows.Count; i++) 
            { 
                text += ((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.ToString; 
            } 
    Response.Write(text); 这样试试
      

  2.   

    还有就是把BindGridView(); 放到if (!IsPostBack) 里面
      

  3.   

            把BindGridView()方法放在(!IsPostBack) 里面就可以解决了
          if (!IsPostBack) 
            { 
                BindDDL(); 
                BindGridView();  
            } 
      

  4.   

    找到问题了
    如果这样
           if (!IsPostBack)
            {
                BindDDL();
                BindGridView();
            }
    可以获得,输出text的值
    但是下拉列表 DropDownList 'ddl' 选择课程名称的时候  Grid View就绑定不了了
    怎么解决阿? 
      

  5.   


    //编辑
    protected void btnSubmit_Click(object sender, EventArgs e) 
        { 
            foreach (GridViewRow gr in this.GridView1.Rows) 
            { 
                string text = ((TextBox)gr.FindControl("txtScore")).Text.ToString; 
                Response.Write(text); 
            } 
        } 贴了很多次
      

  6.   


    这样是可以输入text的值了
    但是Grid View就绑定不了DropDownList所选择的值了
      

  7.   

    foreach (GridViewRow gv in this.gridView.Rows) 

        string str= ((TextBox)gv.FindControl("Text的名称")).Text;

      

  8.   

    DropDownList 'ddl' 用事件OnSelectedIndexChanged同时设置 AutoPostBack="true" protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
      

  9.   

    测试了一下,没有问题啊,你可以先debug看能不能进去。
    protected void Button3_Click(object sender, EventArgs e)
            {
                string strText = "";
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    strText += ((TextBox)(this.GridView1.Rows[i].FindControl("txtScore"))).Text;
                }
                Response.Write(strText);
            }gridview中的
    <asp:BoundField DataField="Birthday" HeaderText="Birthday" SortExpression="Birthday" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="txtScore" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
      

  10.   

    可以解决输入成绩了
    是把BindGirdView()放进  
      if (!IsPostBack) 
            { 
                BindDDL(); 
            } 
         里边
    但是可以输 出text的值了 
    Grid View就绑定不了DropDownList所选择的值了
      

  11.   

            for (int i = 0; i < GridView1.Rows.Count; i++) 
            { 
                string text = ((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.ToString; 
                Response.Write(text); 
            } 
    toString是方法吧?这里当属性用的?
      

  12.   


       protected void btnSubmit_Click(object sender, EventArgs e) 
        { 
            foreach (GridViewRow gr in GridView1.Rows)
            {
                TextBox text = (TextBox)gr.Cells[texbox所在列].FindControl("txtScore");
                Response.Write(text);         }   
        }  
      

  13.   

    因为你要查找的是在模板里  然后 你直接去findControl 是查不到模板里的控件的  你得给他一个确切的位置  例如 this.gv.Rows[0].cells[0].findControl("id")
      

  14.   

    楼主的意思是根据DropDownList里的值来绑定GridView ?
    把DropDownList的AutoPostBack设为True试试.