【发布时间】:2013-09-23 15:41:01
【问题描述】:
我在 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.DataControlLinkButton”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。
我的表单中有一个必填字段验证器,但是当我在网格视图中启用该验证更新时不起作用。
【问题讨论】:
-
你能显示gridview吗?里面有什么控件?
-
好吧,您的
textbox位置不是您在FindControl代码中描述的,请参阅单元格编号从零开始。检查更新的答案。 -
对此我感到非常抱歉,请参阅您的
Cell包含三个控件,两个label和一个textbox,并计算单元格数,它的控件从零开始,所以我认为您的控件将是 2 而不是 3 或 0(抱歉 :-)),当您声明了在我们的案例中不存在的内容时出现此错误,我们声明了Control[3]并且控件总数为 3 但它为 0, 1,2 所以你的文本框位于第三个位置,即 2。 -
我希望它对您有所帮助,您不能直接将控件转换为字符串,因此您必须选择它的
text属性。 -希望它有效。