[英]Do not bind drop down list if selected value not found in list of items
如果AccID
不在項目列表中,如何告訴SelectedValue='<%# Bind("AccID") %>'
不執行綁定?
<EditItemTemplate>
<asp:ObjectDataSource ID="ObjectDataSourceAccount" runat="server" SelectMethod="GetUsableAccountByUser"
TypeName="t_MT_AccCode" OnSelected="ObjectDataSourceAccount_Selected" OnSelecting="ObjectDataSourceAccount_Selecting">
<SelectParameters>
<asp:Parameter Name="companyCode" />
<asp:Parameter Name="departmentCode" />
<asp:Parameter Name="badgeNumber" />
<asp:Parameter Name="userRole" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownListAccount" runat="server" DataSourceID="ObjectDataSourceAccount"
DataTextField="accountDesc" DataValueField="id"
SelectedValue='<%# Bind("AccID") %>'
ondatabinding="DropDownListAccount_DataBinding"
ondatabound="DropDownListAccount_DataBound">
</asp:DropDownList>
</EditItemTemplate>
這就是我解決此問題的方法。
但是我希望可以通過SelectedValue='<%# Bind("AccID") %>'
來解決它。
protected void DropDownListAccount_DataBound(object sender, EventArgs e)
{
DropDownList dropDownListAccount = (DropDownList)sender;
DataRowView currentDataRowView = (DataRowView)((GridViewRow)dropDownListAccount.Parent.Parent).DataItem;
int currentRowDataKeyItemId = int.Parse(currentDataRowView["ID"].ToString());
int accountId = t_MT_MTItem.GetAccountIdByItemId(currentRowDataKeyItemId);
dropDownListAccount.SelectedValue = accountId.ToString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.