DataBoundイベントのドロップダウンを設定する例を作成しました。
マークアップは次の
とおりです。ddlを使用する方法は、DataBoundイベント中にfindcontrol()を使用してddlを検索することです。
DataBoundイベントを制御できる場合は、ドロップダウンをリストにバインドすることもできます<>
これがお役に立てば幸いです。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FormView ID="FormView1" runat="server" ondatabound="FormView1_DataBound">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
背後にあるコードは次のとおりです。
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Some");
list.Add("Other");
FormView1.DataSource = list; //just to get the formview going
FormView1.DataBind();
}
protected void FormView1_DataBound(object sender, EventArgs e)
{
DropDownList ddl = null;
if(FormView1.Row != null)
ddl = (DropDownList) FormView1.Row.FindControl("DropDownList1");
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Two"));
}
}
}