繁体   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