【问题标题】:Get value from Gridview for update从 Gridview 获取值以进行更新
【发布时间】:2013-07-26 12:30:05
【问题描述】:

我有这个带有 AutoGenerateEditButton="true" 的网格视图:

<asp:GridView ID="myGridview" runat="server" 
    ShowFooter="True" 
    AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" 
    AutoGenerateColumns="False" AllowSorting="True" DataKeyNames="id" DataSourceID="odsVolumeSearch" OnRowUpdating="myGridview_RowUpdating">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" Visible="false" InsertVisible="False" ReadOnly="True" SortExpression="id" />
        <asp:BoundField DataField="Date" HeaderText="date" SortExpression="Date" ReadOnly="True" />
        <asp:BoundField DataField="Items" HeaderText="Items" SortExpression="Items" />            
    </Columns> 
</asp:GridView>

这是我的 objectDataSource:

<asp:ObjectDataSource ID="myOds" runat="server" 
    DeleteMethod="myDeleteMethod" SelectMethod="mySelectMethod" 
    TypeName="TheirLocation.sqlDataLayer" UpdateMethod="myUpdateMethod">
    <DeleteParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
    <SelectParameters>
        <asp:Parameter Name="fromDate" Type="DateTime"/>
        <asp:Parameter Name="toDate" Type="DateTime"/>
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="id" Type="Int32" />
        <asp:Parameter Name="volume" Type="Int32" />
    </UpdateParameters>
</asp:ObjectDataSource>

这里的混乱是我的更新事件处理程序:

protected void gvVolumeList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gvVolumeList.Rows[e.RowIndex];
    String debugString = ((TextBox)(row.Cells[1].Controls[0])).Text;
    Response.Write("<script>alert('" + debugString + "');</script>");
}

我只是想从我的文本框中获取值以显示在警报中,但我无法修复它。我尝试了各种方法并疯狂地用谷歌搜索,但我无法获得价值

编辑

我认为问题在于我从单元格中获取文本,而不是单元格内的文本框。还是不知道怎么办

【问题讨论】:

标签: c# asp.net .net gridview webforms


【解决方案1】:
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {    
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    String str = ((TextBox)(row.Cells[2].Controls[0])).Text;
  }

【讨论】:

  • 我的荣幸!谢谢!
【解决方案2】:

如果你想从你的 gridview 中获取 ID !! 如果您已将 Bound 字段可见性声明为 false ,则您的绑定字段将不会被呈现,因此您无法通过使用获取其值

  String debugString = ((TextBox)(row.Cells[1].Controls[0])).Text;

你的单元格索引从 0 开始而不是从 1(如果你试图获取 Id) 。 最好使用 gridview 的 RowCommand 或者让你的 Id 属性visible ="true"

--------------------------或-------- -------- 使用模板字段

 <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="HiddenField1" runat="server" 
                    Value='<%# Eval("Id") %>' />
                 ....
            </ItemTemplate>

代码背后

if (row.RowType == DataControlRowType.DataRow)
      {
       HiddenField Id = (HiddenField)row.Cells[0].FindControl("HiddenField1");

       }

【讨论】:

  • 我没有使用 ID,我正在使用项目。不过谢谢你的回答,我看看能不能用模板字段来做
  • 试试这个... TextBox chk = (TextBox)row.FindControl("Items");字符串值 = chk.Text;希望它的工作方式你的方式也是正确的,你不仅要更改单元格,你正在使用 row.cell[1] 而不是尝试 row.cell[2],因为上面的单元格位置是 3rd,这意味着单元格没有 2 (单元格编号从零开始。)希望它有效。
  • string value = chk.Text; 行上说“对象引用未设置为对象的实例”
  • 我已经尝试更改为不同的单元格。我可以很好地获取日期值,但不能从文本框中获取输入值
  • TextBox chk = (TextBox)row.FindControl("Items");字符串值 = chk.Text;
【解决方案3】:

这是在行更新中获取控制值的方法。

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    //int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
    TextBox tname = (TextBox)row.FindControl("nam");
   //to get value of first cell
    string str = row.Cells[0].Text.ToString();
 }

【讨论】:

  • 从那里获得文本框名称为nam
  • 那根本行不通。得到一个空警报框(所以没有找到值)
  • 这是虚拟名称,您可以根据控件名称更改名称。但是在您的网格中没有任何文本框控件。所以你可以使用 row.Cells[0].text 获取任何单元格的值。
  • 还是什么都没有。只是一个空字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多