后台代码如下: 先用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);
}
}
}
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);
}
}
}
解决方案 »
- 求救,在线等.在调用“Fill”前,SelectCommand 属性尚未初始化。
- 如何部分匹配关键词进行搜索?像百度一样
- ASP.NET 画趋势图,且每个节点可以点击查询,急................
- 登录的时候为什么会报这种错误。。请帮忙!!!
- repeater值判断显示
- 在那里更改VS2005的默认字体! 速度结帖~
- 调查 各位都是用DIV+CSS开发网站了吗?
- 保存完数据后如何直接获取存储的ID号
- 实现网页界面左边放置树状功能导航功能, 因该用框架集方式还是不用框架集做好?
- 大家看看,这个功能谁能实现??
- 如何获取当前页控件的内容
- 一个很有技术性的问题,ASP.NET中的查询,有挑战性
string text = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
text += ((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.ToString;
}
Response.Write(text); 这样试试
if (!IsPostBack)
{
BindDDL();
BindGridView();
}
如果这样
if (!IsPostBack)
{
BindDDL();
BindGridView();
}
可以获得,输出text的值
但是下拉列表 DropDownList 'ddl' 选择课程名称的时候 Grid View就绑定不了了
怎么解决阿?
//编辑
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);
}
} 贴了很多次
这样是可以输入text的值了
但是Grid View就绑定不了DropDownList所选择的值了
{
string str= ((TextBox)gv.FindControl("Text的名称")).Text;
}
{
BindGridView();
}
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>
是把BindGirdView()放进
if (!IsPostBack)
{
BindDDL();
}
里边
但是可以输 出text的值了
Grid View就绑定不了DropDownList所选择的值了
{
string text = ((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.ToString;
Response.Write(text);
}
toString是方法吧?这里当属性用的?
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); }
}
把DropDownList的AutoPostBack设为True试试.