当前位置:编程学习 > C#/ASP.NET >>

同样的两句话取值,为什么一个可以取到,另一个确是空。

同样的两句话, 取GridView中的单元格中的值,为什么一个可以取到,另一个确是空。
代码如下:
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            model.FlightID = GV.Rows[e.RowIndex].Cells[0].Text;   这一句可以取到。
            string test = GV.Rows[e.RowIndex].Cells[1].Text;   这一句取不到
         }

事实上,GridView中的值,只有第一列可以用这种方法取到,而2—9列都取不到?请问这是什么原因? --------------------编程问答-------------------- 其他列你是不是用了模版列?
可以用查找里面的控件取值 --------------------编程问答-------------------- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
  GridView1.EditIndex = e.NewEditIndex;
  bind();
  }

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
   //((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text
   //((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text
   //GridView1.DataKeys[e.RowIndex].Value
}
} --------------------编程问答-------------------- 如果你把前台代码贴出来有助于更好地帮你判断 --------------------编程问答-------------------- 前台代码如下:
<asp:GridView ID="GV" runat="server" CellPadding="2" 
        ForeColor="#333333" AutoGenerateColumns="False" CellSpacing="2" 
        onrowcancelingedit="GridView_RowCancelingEdit" 
        onrowdeleting="GridView_RowDeleting" onrowediting="GridView_RowEditing" 
        onrowupdating="GridView_RowUpdating">
    <FooterStyle  BackColor="#990000" Font-Bold="true"  ForeColor="White" />
    <Columns>
    <asp:BoundField  DataField="FlightID"  HeaderText="航班号"  ReadOnly="true"/>
    <asp:BoundField DataField="FlightTime" HeaderText="航班时间" />
    <asp:BoundField  DataField="FlightStart" HeaderText="起始地点"/>
    <asp:BoundField  DataField="FlightEnd" HeaderText="到达地点"/>
    <asp:BoundField  DataField="Launch" HeaderText="起飞时间"/>
    <asp:BoundField  DataField="Land" HeaderText="到达时间"/>
    <asp:BoundField DataField="MidStation" HeaderText="中间站" />
    <asp:BoundField DataField="Capacity" HeaderText="乘客量" />
    <asp:BoundField DataField="Remain" HeaderText="空票数" />
    <asp:BoundField DataField="Price" HeaderText="票价" />
    <asp:CommandField  HeaderText="选择"  ShowSelectButton="true" />
    <asp:CommandField HeaderText="编辑" ShowEditButton="true" />
    <asp:CommandField HeaderText="删除" ShowDeleteButton="true" />
    </Columns>
    <RowStyle ForeColor="#000066"  />
    <SelectedRowStyle  BackColor="#669999" Font-Bold="true"  ForeColor="White"/>
    <PagerStyle  BackColor="White"  ForeColor="#000066" HorizontalAlign="Left"/>
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    </asp:GridView> --------------------编程问答-------------------- string test = GV.Rows[e.RowIndex].Cells[1].Text;
这样的改下:参考如梦的写法
string  text=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
--------------------编程问答--------------------   我一般取值都是直接找控件的。。 --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
  GridView1.EditIndex = e.NewEditIndex;
  bind();
  }

  protected void GridView1_RowUpdating(object sender, GridV……


就是这种可以,((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text
            ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text


问题一:可是为什么呢?好像不是模板列啊,怎么会转换为“TextBox”?


问题二:为什么第一列可以这样,model.FlightID = GV.Rows[e.RowIndex].Cells[0].Text;
而其它几列就得“((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text
”。越来越糊涂了,这两种形式分别在什么用?

问题三:GridView1.EditIndex = e.NewEditIndex;这一句是什么意思?有什么作用?
--------------------编程问答--------------------
引用 6 楼 intcry 的回复:
  我一般取值都是直接找控件的。。


因为他没有使用模板列。 --------------------编程问答--------------------
引用楼主 sssolweb_009 的回复:
同样的两句话, 取GridView中的单元格中的值,为什么一个可以取到,另一个确是空。
代码如下:
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            model.FlightID = GV.Rows[e.RowIndex].Cells[0].……


既然是在Updating里,那么就是说触发回发时这一行是在Edit状态了!

可以看看你自己的定义
<asp:BoundField DataField="FlightID" HeaderText="航班号" ReadOnly="true"/>
<asp:BoundField DataField="FlightTime" HeaderText="航班时间" />

在Edit状态,第一列既然定义是ReadOnly,它还是Cell中简单的文本。可是第二列已经是Cell中的一个可编辑内容的TextBox了,此时Cell的Text肯定什么都没有,它的Controls内部含了一个TextBox控件。

如果要了解这个老掉牙的asp.net2.0的编辑机制,我还是建议认真从基本的教程模范这做起:
1. http://www.cnblogs.com/eddie005/archive/2006/08/18/EventsOfDataWebControl.html
2. http://guobaoguo.blog.163.com/blog/static/10916258200781323953258/
3. http://blog.csdn.net/wanghr74/archive/2007/04/03/1550835.aspx
4. http://www.cnblogs.com/sijin/articles/1435230.html
5. http://blog.csdn.net/heker2007/archive/2007/04/01/1548229.aspx

6. http://www.wewill.cn/n317c13.aspx

等等。
--------------------编程问答-------------------- 在RowUpdating事件中:

你的GV.Rows[e.RowIndex].Cells[0].Text;
是非模版自曾列,不是编辑列,即不是TextBox控件!

((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text
是非模版列,控件是TextBox

GridView1.EditIndex = e.NewEditIndex;这一句是什么意思?
进入你点选的那一行进行编辑! --------------------编程问答--------------------
引用 7 楼 sssolweb_009 的回复:
引用 2 楼 wuyq11 的回复:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}

protected void GridView1_RowUpdating(object sen……


问题1:你写如下事件
 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        //重现绑定数据
        Loaddata();
    }
再你单击 编辑 之后,就会出现许多文本框了。所有要采用文本框的形式获取

问题2:因为你第一列的属性是redonly的(见你前台代码),所以单击 编辑后 它不会变成文本框 ,故GV.Rows[e.RowIndex].Cells[0].Text;能获取数据

总之添加了编辑取消修改模板列之后要用到的3个方法如下:(我的程序的例子)
 #region //取消编辑
    protected void GvBILU_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            this.GvBILU.EditIndex = -1;
            LoadBILV();
        }
        catch (Exception ee)
        {
            Alert(ee.Message.ToString());
            return;
        }
    }
    #endregion 

    #region //编辑
    protected void GvBILU_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            this.GvBILU.EditIndex = e.NewEditIndex;
            LoadBILV();

            ((TextBox)(this.GvBILU.Rows[e.NewEditIndex].Cells[1].Controls[0])).Width =90;
            ((TextBox)(this.GvBILU.Rows[e.NewEditIndex].Cells[2].Controls[0])).Width = 90;
            ((TextBox)(this.GvBILU.Rows[e.NewEditIndex].Cells[3].Controls[0])).Width = 90;
            ((TextBox)(this.GvBILU.Rows[e.NewEditIndex].Cells[4].Controls[0])).Width = 90;
            ((TextBox)(this.GvBILU.Rows[e.NewEditIndex].Cells[5].Controls[0])).Width = 90;
            
        }
        catch (Exception ee)
        {
            Alert(ee.Message.ToString());
            return;
        }
    }
    #endregion

    #region //修改
    protected void GvBILU_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string gccode, bfb,bfb1,bfb2,bfb3;
        double bfbN, bfbN1, bfbN2, bfbN3;
        string uptstr;
        gccode = ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        bfb = ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
        bfb1 = ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
        bfb2 = ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
        bfb3 = ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
        if (gccode == "")
        {
            Alert("请设置工程CODE!");
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[1].Controls[0])).Focus();
            return;
        }
        if (bfb == "")
        {
            Alert("请设置当前月N比率!");
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[2].Controls[0])).Focus();
            return;
        }
        if (bfb1 == "")
        {
            Alert("请设置N+1月比率!");
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[3].Controls[0])).Focus();
            return;
        }
        if (bfb2 == "")
        {
            Alert("请设置N+2月比率!");
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[4].Controls[0])).Focus();
            return;
        }
        if (bfb3 == "")
        {
            Alert("请设置N+3月比率!");
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[5].Controls[0])).Focus();
            return;
        }
        //判断百分比是否是数字(不能含字符串)
        try
        {
            bfbN = Convert.ToDouble(bfb);
            bfbN1 = Convert.ToDouble(bfb1);
            bfbN2 = Convert.ToDouble(bfb2);
            bfbN3 = Convert.ToDouble(bfb3);
        }
        catch (Exception ee)
        {
            Alert(ee.Message.ToString());
            ((TextBox)(this.GvBILU.Rows[e.RowIndex].Cells[2].Controls[0])).Focus();
            return;
        }

        //修改
        try
        {
            uptstr = "update BILV set GC='" + gccode + "',BL='" + bfb + "',BL1='" + bfb1 + "',BL2='" + bfb2 + "',BL3='" + bfb3 + "' where ID=" + Convert.ToInt32(this.GvBILU.DataKeys[e.RowIndex].Value.ToString());
            BILV_DataCls.getcmd(uptstr);
            this.GvBILU.EditIndex = -1;
            LoadBILV();
            Alert("成功修改当前比率!");
            return;
        }
        catch (Exception ee)
        {
            Alert(ee.Message.ToString());
            return;
        }
        
    }
    #endregion
   --------------------编程问答-------------------- NewEditIndex属性可确定所编辑行的索引
非模版列,控件是TextBox
--------------------编程问答--------------------  看看 gridview72例
--------------------编程问答-------------------- 基础很重要。 --------------------编程问答--------------------
引用 9 楼 sp1234 的回复:
引用楼主 sssolweb_009 的回复:
同样的两句话, 取GridView中的单元格中的值,为什么一个可以取到,另一个确是空。
代码如下:
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
model.FlightID = GV.Rows[e.RowIndex].Cells……

--------------------编程问答--------------------
引用 10 楼 koukoujiayi 的回复:
在RowUpdating事件中:

你的GV.Rows[e.RowIndex].Cells[0].Text;
是非模版自曾列,不是编辑列,即不是TextBox控件!

((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text
是非模版列,控件是TextBox

GridView1.EditIndex = e.……

讲的很清楚了 --------------------编程问答-------------------- 先用用别人的 --------------------编程问答-------------------- --------------------编程问答--------------------  你在行编辑时间触发后,不就是把你的编辑行替换为了文本框供你编辑么?
所以取的时候要做个转换! --------------------编程问答--------------------
引用 19 楼 wangxingny 的回复:
你在行编辑时间触发后,不就是把你的编辑行替换为了文本框供你编辑么?
所以取的时候要做个转换!


谢谢,各位小弟明白了。。。吼吼。。。
--------------------编程问答-------------------- 9楼正解。编辑状态下,第一列是readonly,所以可以直接取。其他的是编辑状态下的,用找control的方法取。 --------------------编程问答-------------------- sp1234出手,果然不同凡响 --------------------编程问答-------------------- 编辑的时候不就出来了 textbox了吗 --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,