2013-07-26 60 views
0

我有这样的GridView与AutoGenerateEditButton属性= “真”:获取值更新

<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>"); 
} 

我只是想从我的文本框中获取值以显示在警报上,但我只是可以t修复它。我曾尝试过各种东西,一派疯狂,但我不能获取的价值

编辑

我认为问题是,我得到从CELL,而不是文本框在细胞内的文本。仍然不知道该怎么办,但

+0

代替'绑定fie ld'我更喜欢'Template field',这里是一个使用模板字段的示例http://satindersinght.blogspot.in/2012/08/how-to-addupdate-record-using-gridview.html –

回答

1
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    {  
    GridViewRow row = TaskGridView.Rows[e.RowIndex]; 
    String str = ((TextBox)(row.Cells[2].Controls[0])).Text; 
    } 
+0

这次它的工作!十分感谢你的帮助! –

+0

我的荣幸!谢谢 ! –

0

这是如何获得行更新中的控制值。

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(); 
} 
+0

从您获取文本框的名称作为'nam' –

+0

这根本不起作用。有一个空的警报箱(所以没有发现任何值) –

+0

这是虚拟名称你可以根据你的控件名称更改名称。但是在你的网格中没有任何文本框控件。所以你可以使用row.Cells [0] .text得到任何单元格的值。 – Raghubar

1

如果您试图从您的gridview获取ID! 如果你已经声明了绑定字段知名度假的,那么UR绑定字段将不会被渲染,所以你不能使用

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

得到它的价值和你的细胞指数从0开始不是从1(如果您正在尝试得到Id)。 更好地使用GridView的RowCommand或者使你的Id属性为visible ="true"

-------------------------- OR ------ ---------------------- 使用模板字段

<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"); 

     } 
+0

我不是usnig Id,我正在使用Items。但感谢您的回答,我会看看我是否可以使用模板字段 –

+0

尝试此... TextBox chk =(TextBox)row.FindControl(“Items”); string value = chk.Text;希望它的工作方式,你的方式也是正确的你不得不改变单元格,你正在使用row.cell [1],而不是尝试row.cell [2],因为你的代码上面的单元格位置是3rd,这意味着单元格2号(单元格编号从零开始。)希望它有效。 –

+0

在'string value = chk.Text;'行中显示“对象引用未设置为对象的实例”行 –