mar*_*kiz 10 .net c# asp.net formview drop-down-menu
我想将dropdownlist绑定到List<MyIem>代码后面.
<asp:DropDownList ID="listCategories" runat="server" Height="20px" CssClass="CategoryDropList" SelectedValue='<%# Bind("ParentId") %>' AutoPostBack="false" Width="300px">
Run Code Online (Sandbox Code Playgroud)
不使用ObjectDataSource!
如何将其绑定到下拉列表?在什么情况下?
也SelectedValue='<%# Bind("ParentId") %>'应该工作!(我的意思是dropdownlist绑定应该在此之前发生!)
举了一个将在DataBound事件中设置下拉列表的示例。
这是标记
使用ddl的方法是在DataBound事件期间通过findcontrol()找到它。
当控件具有DataBound事件时,还可以将下拉列表绑定到List <>,
希望对您有所帮助。
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
这是背后的代码:
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"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20910 次 |
| 最近记录: |