2013-07-26 61 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;'行中顯示「對象引用未設置爲對象的實例」行 –