在你
DropDownList
您可以指定
OnDataBinding
事件,然后我们的事件来填补
下拉框
使用自定义数据。
例子:
<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();
}
希望有帮助。