0

我在 Gridview 中更新了一行,但它不起作用。

这是我的代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

    //Label lbldeleteid = (Label)row.FindControl("Label1");
    string bname= GridView1.DataKeys[e.RowIndex].Values["manufacturer"].ToString();
    TextBox tbmanu = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[1];
    var myString = tbmanu.ToString();

    SqlCommand cmd = new SqlCommand("manu_upd",con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@manufacturer", SqlDbType.NVarChar,100);
    cmd.Parameters["@manufacturer"].Value = myString;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    GridView1.EditIndex = -1;

    BindData();

我收到以下错误:

无法将“System.Web.UI.LiteralControl”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。

这是我的网格视图:

          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                CellPadding="4" DataKeyNames="manufacturer" ForeColor="#333333" 
                GridLines="None" Width="400px" BorderStyle="Double" 
                CellSpacing="3" Font-Bold="True" Font-Size="Small" ShowFooter="True" 
                ShowHeaderWhenEmpty="True" onrowdeleting="GridView1_RowDeleting" 
                onrowediting="GridView1_RowEditing"     
                onrowupdating="GridView1_RowUpdating" 
                onrowcancelingedit="GridView1_RowCancelingEdit" 
                AutoGenerateEditButton="True">
                <AlternatingRowStyle BackColor="White"/>
                <Columns>
                    <asp:TemplateField HeaderText="Number" ItemStyle- 
                         HorizontalAlign="Center">
                             <ItemTemplate>
                                   <asp:Label ID="lbnaumber" runat="server" Text='<%# 
                                          Container.DataItemIndex + 1 %>'></asp:Label>
                             </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Manufacturer" 
                         SortExpression="manufacturer">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# 
                                Bind("manufacturer") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="tbmanu" runat="server" Text='<%# 
                                Bind("manufacturer") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                    <%--<asp:CommandField ShowEditButton="True" />--%>
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <EditRowStyle BackColor="#2461BF"/><FooterStyle BackColor="#507CD1" 
                    Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"/>
                <PagerStyle BackColor="#2461BF" ForeColor="White" 
                        HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" /><SelectedRowStyle BackColor="#D1DDF1" 
                    Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB"/>

                <SortedAscendingHeaderStyle BackColor="#6D95E1" />

                <SortedDescendingCellStyle BackColor="#E9EBEF" />


                <SortedDescendingHeaderStyle BackColor="#4870BE" />


         </asp:GridView>

当我将文本框 1 的索引更改为 3 时,它显示以下错误:

指定的参数超出了有效值的范围。参数名称:索引

当我将其索引 3 更改为 2 时,它给了我以下错误:

无法将“System.Web.UI.WebControls.DataControlLinkBut​​ton”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。

我的表单中有一个必填字段验证器,但是当我在网格视图中启用该验证更新时不起作用。

4

2 回答 2

0
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox tbmanu1 = (TextBox)row.FindControl("tbmanu"); 
        //or try 
        //TextBox tbmanu1= (TextBox)row.Cells[0].FindControl("tbmanu");
        string  myString = Convert.Tostring(tbmanu1.text);
        cmd.Parameters["@manufacturer"].Value = myString;   
     }
于 2013-09-23T16:03:54.253 回答
0

尝试将您的 gridview 包装在 asp:UpdatePanel 中并将更新模式设置为有条件的。

祝你好运

于 2013-09-23T17:37:55.957 回答