簡體   English   中英

如果在項目列表中找不到所選值,則不綁定下拉列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM