1

ASP.NETWebアプリケーションにListViewがあります。ユーザーが編集ボタンをクリックすると、アイテムの特定の値に依存するテキストフィールドがポップアップ表示されます。ただし、ListView1_ItemEditing()関数内にコントロールが見つからないようです。

私はインターネット上のMicrosoftのドキュメントとさまざまなヘルプスレッドを読みましたが、それらの提案は私にはうまくいかないようです。これは一般的に私が見るものです:

ListViewItem item = ProductsListView.Items[e.NewEditIndex];
Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");

簡単にするために、ListView1_ItemEditing()でラベルを選択できるようにしたいだけです。これはListView1_ItemEditing()のコードです:

protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    DataBind(); //not sure if this does anything
    ListViewItem item = ListView1.Items[e.NewEditIndex];
    Label debugLabel = (Label)item.FindControl("label_editing");
    debugLabel.Text = "Works";
}

これがASPです

<EditItemTemplate>
   <asp:Label ID="label_editing" runat="server" Text="hello world"></asp:Label>
</EditItemTemplate>

デバッグ時には、itemとdebugLabelは両方ともNULLです。

更新:ロジックをItemDataBoundに移動し、tr(テキストボックスを含む)がその特定のデータアイテムにあるかどうかを確認することで、この問題を解決しました。以下のコード:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Control tr_verizon = e.Item.FindControl("tr_verizonEdit");
            Control tr_att = e.Item.FindControl("tr_attEdit");
            if (tr_verizon != null)
            {
                //Control tb_meid = e.Item.FindControl("TextBox_Meid");
                Label lbl_carrierId = (Label)e.Item.FindControl("lbl_carrierId");
                if (lbl_carrierId == null)
                {
                    Message.Text = "lbl_carrierId is null!";
                }
                else if (lbl_carrierId.Text.Equals(""))
                {
                    Message.Text = "lbl_carrierId is empty!";
                }
                else
                {
                    string recordId = lbl_carrierId.Text;

                    if (tr_verizon != null && tr_att != null)
                    {
                        if (lbl_carrierId.Text.Equals("1"))
                        {
                            tr_verizon.Visible = false;
                            tr_att.Visible = true;
                        }
                        else
                        {
                            tr_verizon.Visible = true;
                            tr_att.Visible = false;
                        }
                    }
                }
            }
        }
    }
4

3 に答える 3

2

このItemEditingイベントは、アイテムの[編集]ボタンがクリックされたときに発生しますが、ListViewアイテムが編集モードになる前に発生します。したがってEditItemTemplate、現時点ではのコントロールは使用できません。

詳細と例

于 2012-04-23T16:45:45.277 に答える
2

次のように、最初にDataBind()を実行する必要があります。

    ListView1.EditIndex = e.NewEditIndex;
    ListView1_BindData(); // a function that get the DataSource and then ListView1.DataBind()

//前と同じようにコントロールを見つけます

于 2012-11-12T09:57:33.653 に答える
0

senderインデックスでListViewItemにアクセスする代わりに、オブジェクトをキャストしようとしましたか?

protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    var item = sender as ListViewItem;
    var debugLabel = item.FindControl("label_editing") as Label;
    debugLabel.Text = "Works";
}
于 2012-04-23T16:15:42.483 に答える