从Gridview获取价值以进行更新

时间:2013-07-26 12:30:06

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

我有这个Gridview与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>");
}

我只是想从我的文本框中获取值以显示警报,但我无法修复它。我已经尝试了各种各样的东西,并像疯了似的google但我无法获得价值

修改

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

3 个答案:

答案 0 :(得分:1)

如果你想从你的gridview获得Id! 如果您已将Bound字段可见性声明为false,则不会呈现您的绑定字段,因此您无法使用

获取其值
  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");

       }

答案 1 :(得分: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 :(得分:0)

这是如何在行Update中获取控制值。

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