如何在解决此错误后将记录插入Datatable(ViewState)“ListView”xxxxx“'引发的事件ItemInserting未处理。”?

时间:2016-02-24 11:47:58

标签: c# asp.net listview

我在asp.net项目中使用listview,我得到错误,如“ListView”xxxxx“'引发事件ItemInserting未处理。”当我从Viewstate插入项目时。

前端编码

 <asp:ListView ID="lsViewInovice" runat="server"  onsorting="lsViewInovice_Sorting" OnItemDataBound="lsViewInovice_OnItemDataBound"  InsertItemPosition="LastItem" onitemcommand="lsViewInovice_ItemCommand">
          <LayoutTemplate>
              <table border="0" cellpadding="1">
                  <tr style="background-color: #E5E5FE" >
                      <th align="center">
                          <asp:Label ID="lblSno" runat="server">S.No</asp:Label>
                      </th>
                      <th align="center">
                          <asp:Label ID="lblDescription" runat="server" >Description</asp:Label>
                      </th>
                      <th align="center">
                          <asp:Label ID="lblAmount" runat="server" >Amount</asp:Label>
                      </th>
                       <th align="center">
                          <asp:Label ID="lblAction" runat="server" >Actions</asp:Label>
                      </th>
                  </tr>
                  <tr id="itemPlaceholder" runat="server">
                  </tr>
              </table>
           </LayoutTemplate>
          <ItemTemplate>
              <tr>
                  <td>
                      <asp:Label runat="server" ID="lblId"><%#Container.DataItemIndex+1 %></asp:Label>
                  </td>
                  <td>
                      <asp:Label runat="server" ID="lblName"><%#Eval("Description")+" "+Eval("Extra") %></asp:Label>
                  </td>
                  <td>
                      <asp:Label runat="server" ID="lblType" runat="server"><%#Eval("Amount") %></asp:Label>
                  </td>
                  <td>
                      <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                  </td>
              </tr>
          </ItemTemplate>

          <EditItemTemplate>
              <td>
                  <asp:Label runat="server" ID="lblId"><%#Container.DataItemIndex+1 %></asp:Label>
              </td>
              <td>
                    <asp:DropDownList ID="ddlEditDescrption" OnSelectedIndexChanged="ddlEditDescrption_SelectedIndexChanged" runat="server" AutoPostBack="true" style="vertical-align:top;"></asp:DropDownList>
                    <asp:TextBox ID="txtEditAdditional" runat="server" Text="" TextMode="MultiLine" Rows="5" Columns="30" Visible="false" ></asp:TextBox>
              </td>
              <td>
                    <asp:TextBox ID="txtEditAmount" runat="server" Text='<%#Eval("Amount") %>' style="text-align:right;" ></asp:TextBox>
              </td>
              <td>
                  <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
                  <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
                  <asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
              </td>
              </tr>
          </EditItemTemplate>
          <InsertItemTemplate>
              <tr runat="server">
                  <td>
                  </td>
                  <td>
                      <asp:DropDownList ID="ddlInsertDescrption" OnSelectedIndexChanged="ddlInsertDescrption_SelectedIndexChanged" runat="server" AutoPostBack="true" style="vertical-align:top;"></asp:DropDownList>
                  </td>
                  <td>
                     <asp:TextBox ID="txtInsertAdditional" runat="server" Text="" TextMode="MultiLine" Rows="5" Columns="30" Visible="false" ></asp:TextBox>
                  </td>
                  <td>
                      <asp:TextBox ID="txtInsertAmount" runat="server" Text="" Width="100px">0.00</asp:TextBox>
                  </td>
                  <td>
                      <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                  </td>
              </tr>
          </InsertItemTemplate>
      </asp:ListView>

代码背后

protected void lsViewInovice_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Insert")
            {
                DropDownList ddlInsDesription = lsViewInovice.InsertItem.FindControl("ddlInsertDescrption") as DropDownList;
                TextBox txtInsExtrd = lsViewInovice.InsertItem.FindControl("txtInsertAdditional") as TextBox;
                TextBox txtInsAmt = lsViewInovice.InsertItem.FindControl("txtInsertAmount") as TextBox;


                DataTable dtTempInsInvoiceDetails = new DataTable();
                //Create DataTable columns
                dtTempInsInvoiceDetails.Columns.Add(new DataColumn("Description", typeof(string)));
                dtTempInsInvoiceDetails.Columns.Add(new DataColumn("Extra", typeof(string)));
                dtTempInsInvoiceDetails.Columns.Add(new DataColumn("Amount", typeof(string)));


                DataRow dtNewRow = dtTempInsInvoiceDetails.NewRow();

                //int iSno = Convert.ToInt32(txtRowNumber.Text.Trim()) + 1;

                dtNewRow["Description"] = ddlInsDesription.SelectedValue;
                dtNewRow["Extra"] = txtInsExtrd.Rows;
                dtNewRow["Amount"] = txtInsAmt.Text;

                dtTempInsInvoiceDetails.Rows.Add(dtNewRow);
                ViewState["vsInvoiceDetais"] = dtTempInsInvoiceDetails;

                lsViewInovice.DataSource = ViewState["vsInvoiceDetais"] as DataTable;
                lsViewInovice.DataBind();

            }
        }
        catch (Exception ex)
        {
            Response.Write("Error Message" + ex.Message);
        }
    }

0 个答案:

没有答案