读书人

datagrid 控件绑定!

发布时间: 2012-03-04 11:13:33 作者: rapoo

datagrid 控件绑定!!紧急求助!!
各位朋友们帮忙解决下我的问题吧:

先发下我的 页面的源码
<body>
<form id="form1" runat="server">
<div align=center> <uc2:Header ID="Header2" runat="server" /></div>

<div align=center><asp:Label ID="Label1" runat="server" Width="400px"><font color="#65186d" size=4 > <b> <%=Session["Username"] %>老师您好,欢迎您光临本网站!</b></font> </asp:Label></div>
<br />
<table cellSpacing="1" cellPadding="0" width="800" align=center bgColor="FDF5E6" border="0">

<div align=left><A href="Index.aspx">回到首页</A>-><A href="#"><font color="#00c100"><b>学生向我提问的问题:</b></font></A> </div>

</table>

<table cellSpacing="1" cellPadding="0" width="800" align="center" bgColor="FDF5E6" border="0">


<tr bgColor="#ffffff">
<td colSpan="2">
<asp:DataGrid id="subpro" runat="server" HorizontalAlign="Center" AutoGenerateColumns="False"
PageSize="30" AllowPaging="True" OnPageIndexChanged="Get_Page" OnItemCommand="subpro_ItemCommand" >
<Columns>
<asp:TemplateColumn>

<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="0" width="700" align="center" border="0" >
<TR>
<TD bgColor="FDF5E6" colSpan=2 height=25>
<DIV align="left"><FONT color=green size="3"></font>  <font size=3 color=DarkSlateGray ><%# DataBinder.Eval(Container.DataItem, "Pro_author") %></font>  <STRONG>同学</STRONG>  <FONT size="2"><STRONG>  <font size="3">于</font>  <font size=3 color=DarkSlateGray ><%# DataBinder.Eval(Container.DataItem, "Pro_date") %></font>  <font size="3">向您提交了问题标题:</font>  <font size=3 color=DarkSlateGray ><%# DataBinder.Eval(Container.DataItem, "Pro_title") %></font></STRONG></FONT></FONT></DIV>
</TD>
</TR>
<TR>
<TD style="HEIGHT: 31px" colSpan="2"><FONT style="FONT-SIZE: 12px"><FONT size="3"><strong>内容为</strong> &nbsp<%# DataBinder.Eval(Container.DataItem, "Pro_content") %></FONT><STRONG><FONT size="2"></FONT></STRONG><BR>
</FONT>
</TD>
</TR>

</TABLE>

</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Select" Text="回复" ></asp:ButtonColumn>
</Columns>
<PagerStyle PageButtonCount="20" Mode="NumericPages"></PagerStyle>

</asp:DataGrid></td>
</tr>




<tr bgColor="#ffffff">
<td colSpan="2">

<P> 教 工 号:   
<asp:TextBox id="tx_subid" runat="server" ></asp:TextBox><BR>
回复问题标题:
<asp:TextBox id="tx_subtitle" runat="server"></asp:TextBox><BR>

回复问题内容:<TEXTAREA id="txar_subcont" name="r_content" rows="6" cols="40" runat="server"></TEXTAREA> 
<br>



</P>
</td>
</tr>

<tr bgColor="#ffffff" align=center>
<td colSpan="2">
<uc1:Footer ID="Footer1" runat="server" />
</td>
</tr>
</table>



</form>
</body>



然后是我的后台代码
public partial class Teacher : System.Web.UI.Page
{

OleDbConnection ST_myConn;
string str;

protected void Page_Load(object sender, EventArgs e)
{
check_Login();

tx_subtitle.Text = str;
string ST_dns = ConfigurationSettings.AppSettings["conn"];
ST_myConn = new OleDbConnection(ST_dns);
sub_Bind();
Page.DataBind();

}

private void sub_Bind()
{

string ST_dns = ConfigurationSettings.AppSettings["conn"];
string ST_msg_sql = "select * from dayi_Problem where Pro_reply is NULL and Tea_name= '" + Session["Username"] + "' order by Pro_date desc";

ST_myConn = new OleDbConnection(ST_dns);
OleDbDataAdapter ST_msgCmd = new OleDbDataAdapter(ST_msg_sql, ST_myConn);

DataSet ST_ds = new DataSet();
ST_msgCmd.Fill(ST_ds, "未解答问题");

subpro.DataSource = new DataView(ST_ds.Tables[0]);
subpro.DataBind();


}


public void Get_Page(object sender, DataGridPageChangedEventArgs e)
{

subpro.CurrentPageIndex = e.NewPageIndex;
sub_Bind();

}



private void check_Login()
{

if ((Session["Userid"] == null))
{
Response.Write("<script>alert('还未登录');parent.location.href='Index.aspx';</script>");
Response.End();
}


else
{
tx_subid.Text=Session["Userid"].ToString();
}

}



protected void subpro_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "Select")
{



str = e.Item.Cells[2].ToString();

}
}
}

为什么我取不到值啊,e.Item.Cells[]是0和1都可以,但是没有值显示在tx_subtitle文本框中,用e.Item.Cells[2]就会出现

异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index

源错误:


行 91:
行 92:
行 93: str = e.Item.Cells[2].ToString();


行 94:
行 95:


我到底怎样才能去取得绑定的 某行的某个字段值啊,请大家帮我下吧



[解决办法]
1. 尽量不要用索引去取,容易出错

if(e.Item.Cells.Count > 2) { // 判断!!!
str = e.Item.Cells[2].ToString();
}


2. 使用 FindControl

TextBox tx_subtitle = (TextBox)e.Item.FindControl("tx_subtitle");
if(tx_subtitle != null){
string str = tx_subtitle.Text;
//.....
}

读书人网 >C#

热点推荐