Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Gridview中获取可编辑字段的值_C#_Asp.net_Gridview_Gridviewrow - Fatal编程技术网

C# 如何在Gridview中获取可编辑字段的值

C# 如何在Gridview中获取可编辑字段的值,c#,asp.net,gridview,gridviewrow,C#,Asp.net,Gridview,Gridviewrow,我有一个带有ObjectDataSource的gridview,我想通过单击来更新它。 因此,gridview将加载可编辑和固定字段。 我通过gridview循环一次更新所有内容。 我可以将固定字段(单元格[0])中的值加载到Products对象(p.ProductID)中,但无法将可编辑字段(单元格[1])中的值加载到Products对象(p.Productname)中 如何在对象中获取此值,以便将对象发送到BLL方法。 为什么行。单元格[0]。文本适用于固定字段,但不适用于可编辑字段

我有一个带有ObjectDataSource的gridview,我想通过单击来更新它。 因此,gridview将加载可编辑和固定字段。 我通过gridview循环一次更新所有内容。 我可以将固定字段(单元格[0])中的值加载到Products对象(p.ProductID)中,但无法将可编辑字段(单元格[1])中的值加载到Products对象(p.Productname)中

如何在对象中获取此值,以便将对象发送到BLL方法。 为什么行。单元格[0]。文本适用于固定字段,但不适用于可编辑字段

    protected void Update_Click(object sender, EventArgs e)
    {
        BLLProducts BLLProducts = new BLLProducts();
        Products p = new Products();
        foreach(GridViewRow row in GridView1.Rows)
        {
            p.ProductID = Convert.ToInt16(row.Cells[0].Text);
         // p.Productname = row.Cells[1].Text;

            BLLProducts.update(p);
        }
    }
标记:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="test">
            <Columns>
                <asp:BoundField DataField="ProductID" 
                    HeaderText="ProductID" SortExpression="ProductID" />
                <asp:TemplateField HeaderText="Productname" SortExpression="Productname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>             
            </Columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="test" runat="server" 
            SelectMethod="SelectAllProducts" TypeName="BLLProducts">
        </asp:ObjectDataSource>
        <asp:Button ID="Button3" runat="server" onclick="Update_Click" 
            Text="Update All" />
    </p>
</asp:Content>


您正在使用哪个可编辑字段

如果使用了文本框,则应获得如下值:

       foreach (GridViewRow gvr in GridView2.Rows)  
       {  
           TextBox tb = (TextBox)gvr.FindControl("TextBox1");  
           string txt = tb.Text;              
       }

如果您包含标记的相关部分,那么对于试图回答您的问题的人来说将非常有用。