【问题标题】:How-Update-Data-In-Asp-Net-GridView如何在 Asp-Net-GridView 中更新数据
【发布时间】:2015-09-16 10:01:00
【问题描述】:

无法将“System.Web.UI.LiteralControl”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。当我更新记录时显示此错误 我想通过 datagridview 更新记录,具体取决于 id。

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection conn = new SqlConnection(conctiioon);
    int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    //TextBox textName = (TextBox)row.Cells[0].Controls[0];
    TextBox textName = (TextBox)row.Cells[1].Controls[0];
    TextBox textprice = (TextBox)row.Cells[2].Controls[0];//here the error display 

    GridView2.EditIndex = -1;
    conn.Open();
    //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
    SqlCommand cmd = new SqlCommand("update items set name='" + textName.Text + "',price='" + textprice.Text + "' where id='" + userid + "'", conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    gvbind();
}

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating"
            CellPadding="4" ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="S.No." />
                <asp:BoundField DataField="name" HeaderText="Name" />
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Price") %>'
                            CssClass="stock">
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="#" id='<%# Eval("id") %>' class="updatebutton">
                            <img border="0" src="Images/update.png" alt="Delete" />
                        </a>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField   ShowEditButton="true" />
                <asp:CommandField ShowDeleteButton="true" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

【问题讨论】:

  • 您正在将单元格值转换为文本框,这实际上是一个文字控件。在下面一行 TextBox textName = (TextBox)row.Cells[1].Controls[0];
  • 是的,如何处理这个
  • 将名称字段控件转换为文字并像使用文本框控件一样获取文字文本。
  • 这个“textprice”的错误显示我怎样才能把它转换成文字
  • 您确定在投射 cell[2] 控件时出现错误吗?

标签: c# asp.net gridview


【解决方案1】:

请表示 NameSapce(UperCase) 中的 System.Web.UI.WebControl

【讨论】:

    【解决方案2】:

    我模拟了您的 gridview 并实现了一个简单的数据源。当该单元格中有 3 个控件时,您假设价格文本框的索引为 0。该单元格的第一个控件不是您的文本框,因此转换失败。

    不要假设索引是什么,而是使用您已经找到的 GridViewRow 找到控件。

    TextBox textprice = (TextBox)(row.FindControl("TextBox1"));
    

    【讨论】:

      【解决方案3】:

      会的

      TextBox textprice = (TextBox)Gridview2.Rows[e.Rowindex].FindControl("TextBox1");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        相关资源
        最近更新 更多