2013-03-26 51 views
0

以下是我的ListView标记如何将listviewitemeventargs添加为listview的onitemupdated事件中的参数?

<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。任何人都可以建议做什么?

回答

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; 
    } 
    } 
} 

如果你想只检查特定领域null或空:

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; 
} 
+0

我怎么能恢复这些受影响的领域???我在Listview中有一个名为Amount的文本框。现在我不希望这个数量文本框为空,而这意外是用户可以做的。我该怎么办?? Sani Huttunen – 2013-03-26 12:52:43

相关问题