GridView1.Rows[e.NewIndex].Cells[1].Controls得到的是空值
前台部分代码:<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowUpdated="GridView1_RowUpdated"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="ui_id" HeaderText="编号" ReadOnly="True">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ui_username" HeaderText="昵称">
<HeaderStyle HorizontalAlign="Center" Width="20px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ui_password" HeaderText="密码">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ui_type" HeaderText="权限">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ui_name" HeaderText="姓名">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
后台部分代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//此句中得到的是null而不是Control实例,这样我就不知道怎么取单元格里编辑的值了呜//呜
object o= GridView1.Rows[e.RowIndex].Cells[1].Controls;
//Text得到的是一个空字符串
String text = GridView1.Rows[e.RowIndex].Cells[2].Text;
UserInfo.EditIndex = -1;
this.bind();
}
才开始学ASP.NET,求各位高手指点迷津 --------------------编程问答-------------------- 一点一点来,看是第一个下标没有值还是第二个下标没有值 --------------------编程问答--------------------
顶撸上 调试看看 --------------------编程问答-------------------- 你不用管是不是控件的,如下就可以得到值:
string text= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
--------------------编程问答-------------------- GridViewRow gr = this.GridView1.Rows[e.RowIndex];
gr.FindControl("TextBox2") as TextBox).Text.ToString() --------------------编程问答--------------------
这么写就直接报索引值ArgumentOutOfRange异常,我试过了T^T --------------------编程问答--------------------
你怎么写的 --------------------编程问答-------------------- 后台是这么写的 前台还是和提问的一样
protected void UserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
object o = ((TextBox)UserInfo.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
UserInfo.EditIndex = -1;
this.initial();
} --------------------编程问答-------------------- 我刚才又调试了一下竟然解决了,我给GridView配置了数据源控件SqlDataSource后就能够通过GridView1.Rows[e.RowIndex].Cells[1].Controls[0]获取其中的值了。但是数据源控件面对大容量的数据表时可能会由于一次取出所有值导致系统不稳定,所以肯定还要考虑其他的解决方法的,谢谢大家的热心帮助,给了我很有用的启发!
补充:.NET技术 , ASP.NET