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