1

GridView 内の TemplateField 内にドロップダウンリストがあります。

リスト項目を動的に追加し、インデックスが変更されたときに処理するコードを書きたいと思います。リストが TemplateField にある場合、DropDownList を直接参照できないため、リストを操作するにはどうすればよいですか。

これが私のコードです:

<asp:TemplateField HeaderText="Transfer Location" Visible="false">
   <EditItemTemplate>
      <asp:DropDownList ID="ddlTransferLocation" runat="server" ></asp:DropDownList>
   </EditItemTemplate>
 </asp:TemplateField>
4

1 に答える 1

0

あなたが何をしたいのかを正しく理解している場合は、次のようにドロップダウンに項目を追加できます。

foreach (GridViewRow currentRow in gvMyGrid.Rows)
{
    DropDownList myDropDown = (currentRow.FindControl("ddlTransferLocation") as DropDownList);
    if (myDropDown != null)
    {
        myDropDown.Items.Add(new ListItem("some text", "a value"));
    }
}

次に、DropDownList のインデックスの変更を処理する場合は、コントロールにイベント ハンドラーを追加するだけです。

<asp:DropDownList ID="ddlTransferLocation" runat="server" OnSelectedIndexChanged="ddlTransferLocation_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>

次に、そのイベントハンドラーで、(sender as DropDownList)必要なものを取得するために使用できます。

protected void ddlTransferLocation_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList myDropDown = (sender as DropDownList);
    if (myDropDown != null) // do something
    {
    }
}
于 2011-05-26T23:18:43.320 に答える