更新功能出错
本文关键字:出错 新功能 更新 | 更新日期: 2023-09-27 18:03:26
我在我的。aspx文件中创建了这个数据网格。
<asp:datagrid id="Computerchange" runat="server" AllowPaging="True" PageSize="2" AutoGenerateColumns="False" BorderColor="Gainsboro" Height="500px">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Admin Functions" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="com_id" ReadOnly="True" HeaderText="Computer Number"></asp:BoundColumn>
<asp:BoundColumn DataField="company" HeaderText="Company"></asp:BoundColumn>
<asp:BoundColumn DataField="price" HeaderText="Price"></asp:BoundColumn>
<asp:BoundColumn DataField="model" HeaderText="Model"></asp:BoundColumn>
<asp:BoundColumn DataField="description" HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="Category"></asp:BoundColumn>
<asp:BoundColumn DataField="quantity" ReadOnly="False" HeaderText="Quantity"></asp:BoundColumn>
<asp:BoundColumn DataField="imgSrc" HeaderText="Image"></asp:BoundColumn>
</Columns>
</asp:datagrid>
使用这个数据网格,我想编辑-删除-更新数据库中的这些列。当我在数量列中有ReadOnly="False"
时,它工作得很好。我可以编辑-删除-更新所有内容。有时我想编辑除QUANTITY以外的所有列。但是当我将其转换为True
时,当我单击Update按钮
指定的参数超出有效值范围。参数名称:index
堆栈跟踪:
[ArgumentOutOfRangeException:指定的参数超出有效值范围。]参数名称:index]
System.Web.UI.ControlCollection。get_Item(Int32索引)+8750274AdminMainPage。Computerchange_UpdateCommand(对象源,DataGridCommandEventArgs e) +626System.Web.UI.WebControls.DataGrid。OnUpdateCommand(DataGridCommandEventArgs e) +115System.Web.UI.WebControls.DataGrid。OnBubbleEvent(对象源,EventArgs e) +498System.Web.UI.Control。RaiseBubbleEvent(对象源,EventArgs参数)+37System.Web.UI.WebControls.DataGridItem。OnBubbleEvent(对象源,EventArgs) +121System.Web.UI.Control。RaiseBubbleEvent(对象源,EventArgs参数)+37System.Web.UI.WebControls.LinkButton。OnCommand(commanddeventargs e) +125System.Web.UI.WebControls.LinkButton。RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler。RaisePostBackEvent(String eventArgument) +9System.Web.UI.Page。(ipostbackeventandler的sourceControl, String eventArgument) +13System.Web.UI.Page。RaisePostBackEvent(NameValueCollection postData) +176System.Web.UI.Page。ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
你知道为什么我有这个错误,我能做些什么来修复它吗?我为更新命令
编写了这个函数private void Computerchange_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Store updated column values in local variables:
//string updateCOM_ID = e.Item.Cells["com_id"].Text;
string updateCOM_ID = e.Item.Cells[2].Text;
string updateCompany = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
double updatePrice = double.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text);
string updateModel = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
string updateDescription = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
int updateCategoryId = int.Parse(e.Item.Cells[7].Text);
string updateImage = ((TextBox)e.Item.Cells[9].Controls[0]).Text;
string updateQuantity = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
newView.RowFilter = "com_id='" + updateCOM_ID + "'";
if (newView.Count > 0)
{
//Delete the row that is being updated
newView.Delete(0);
}
newView.RowFilter = "";
//Create a new DataRow and populate it with the new data.
DataRow Row = Table.NewRow();
Row["com_id"] = updateCOM_ID;
Row["company"] = updateCompany;
Row["price"] = updatePrice;
Row["model"] = updateModel;
Row["description"] = updateDescription;
Row["id"] = updateCategoryId;
Row["imgSrc"] = updateImage;
//Row["quantity"] = updateQuantity;
//Insert the new DataRow:
Table.Rows.Add(Row);
Computerchange.EditItemIndex = -1;
Computerchange.DataSource = newView;
Computerchange.DataBind();
// Now update the database with the new data
adminCentral1.adminCentral newData = new adminCentral1.adminCentral();
string results;
results = newData.updateItem(updateCOM_ID, updateCompany, updatePrice, updateModel, updateDescription, updateImage, updateCategoryId, updateQuantity);
if (results == "Success")
{
errorLabel.Text = "Computer Updated to database!";
}
else
{
errorLabel.Text = results;
}
因为当ReadOnly="True"
时它只呈现文本,而不是控件,所以Controls[0]抛出异常。使用templatecolle代替:
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<%# Eval("quantity") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbQuantity" Text='<%# Eval("quantity") %>' ReadOnly="False" />
</EditItemTemplate>
</asp:TemplateColumn>
设置tbQuantity's
ReadOnly
属性为true,禁用编辑