在您的中DropDownList
,您可以分配一个OnDataBinding
事件,然后我们使用该事件来填充您DropDownList
的自定义数据。
例子:
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="yourDropDownList" runat="server"
DataTextField="YourTextFieldName" DataValueField="YourValueFieldName"
OnDataBinding="yourDropDownList_DataBinding"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
然后在你的代码后面实现 OnDataBinding:
protected void yourDropDownList_DataBinding(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)(sender);
// GetMyDropDownListData should return cached data so your not hitting your DB
// each time. You can customize the data for each row here. Use the Eval command
// to access the current rows databound values.
ddl.DataSource = GetMyDropDownListData();
ddl.DataBind(); // Now all the options will be loaded
// Set the current field's selected value
ddl.SelectedValue = Eval("YourSelectedValueFieldName").ToString();
}
希望有帮助。