0

以下是我的列表视图标记

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SNO" 
    DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" OnItemCreated="DateCalculation" OnItemUpdated="Update">

如您所见,我有两个事件DateCalculationUpdateButton。日期计算事件如下:-

 protected void DateCalculation(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.InsertItem)
    {
        TextBox txtbox1 = e.Item.FindControl("DateTakenPlaceTextBox") as TextBox;
        txtbox1.Text = DateTime.Now.ToString("yyyy/MM/dd");

    }
}

Now when i try to add similar event to Update i.e.

protected void Update(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.InsertItem)
    {
        TextBox txtbox2 = e.Item.FindControl("AmountTextBox") as TextBox;
        if (txtbox2.Text == null)
        {

        }
    }
}

我收到以下错误: “更新”没有重载与委托
“System.EventHandler”匹配

这是否意味着我必须编写 eventargs 而不是 Listviewitemeventargs?我想访问 listviewitems 所以我需要 Listviewitemeventargs。任何人都可以建议做什么?

4

1 回答 1

0

您正在寻找ListViewUpdatedEventArgs,其中包含您可以使用的属性,例如NewValuesOldValuesAffectedRows

编辑
为了防止空值和空值,您可以改用ItemUpdating事件。您需要在更新项目之前检查这一点。否则为时已​​晚。

<asp:ListView ID="ListView1" runat="server" DataKeyNames="SNO" 
              DataSourceID="SqlDataSource1" InsertItemPosition="LastItem"
              OnItemCreated="DateCalculation" OnItemUpdating="Updating">
protected void Updating(Object sender, ListViewUpdateEventArgs e)
{
  // Cancel the update operation if any of the fields is empty or null.
  foreach (DictionaryEntry de in e.NewValues)
  {
    // Check if the value is null or empty.
    if (de.Value == null || de.Value.ToString().Trim().Length == 0)
    {
      Message.Text = "Cannot set a field to an empty value.";
      e.Cancel = true;
    }
  }
}

如果您只想检查特定字段是否为空或为空:

var val = e.NewValues["Amount"];
if (val == null || val.ToString().Trim().Length == 0)
{
  Message.Text = "Cannot set a field to an empty value.";
  e.Cancel = true;
}
于 2013-03-26T12:17:09.947 回答