asp.net中的dropdownlist禁用

时间:2013-10-23 17:03:15

标签: c# asp.net

当管理员批准/拒绝任何文档一次然后当管理员再次登录时,他/她将无法再次批准/拒绝文档,并且只有那些曾经批准/拒绝的文档才会禁用dropdownlist,然后当管理员查看任何文档时新文件然后下拉将启用,当管理员批准/拒绝此文件时,它将被禁用下拉列表,不再批准/拒绝

为此我这样做

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row

        DropDownList abc = (e.Row.FindControl("DropDownList9") as DropDownList);
        abc.Enabled = false;
    }
}

但是这段代码显示所有下拉列表都被禁用。

任何解决方案我将如何做到这一点?

3 个答案:

答案 0 :(得分:1)

您需要从每一行访问数据项,我假设它在您绑定到网格的对象上可用,并确定它们是否已被批准/拒绝。如果是这样,那么你应该运行逻辑来禁用:

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                var yo = (YOUR-OBJECT)e.Row.DataItem;

                if(yo.Status !== null OR yo.Status != 'Not Reviewed'){
                //Find the DropDownList in the Row
                DropDownList abc = (e.Row.FindControl("DropDownList9") 
                as  DropDownList);
                abc.Enabled = false;

                }

            }

        }

答案 1 :(得分:1)

根据您的评论,我假设您的DataSource是DataTable或DataSet。

如果是这样,您希望将 DataItem 强制转换为 RowDataBound事件中的 DataRowView ,以获取状态列的值。

enter image description here

<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Status">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="DropDownList9">
                    <asp:ListItem Text="Approve" Value="1" />
                    <asp:ListItem Text="Reject" Value="2" />
                    <asp:ListItem Text="Pending" selected="selected" Value="3">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var table = new DataTable();
        table.Columns.Add("Id", typeof (int));
        table.Columns.Add("Name", typeof (string));
        table.Columns.Add("ApproveID", typeof(string));

        table.Rows.Add(1, "Jon Doe", "1");
        table.Rows.Add(2, "Eric Newton", "2");
        table.Rows.Add(3, "Marry Doe", "3");

        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var item = e.Row.DataItem as DataRowView;

        var dropDownList = e.Row.FindControl("DropDownList9") as DropDownList;

        // Get value from ApproveID column, 
        // and check whehter Approve, Reject or others.
        switch (item["ApproveID"].ToString())
        {
            case "1":
            case "2":
                dropDownList.Enabled = false;
                break;
            default:
                dropDownList.Enabled = true;
                break;
        }
    }
}

答案 2 :(得分:0)

您可以询问使用此代码的行:

if (e.Row.RowIndex == <aRowNumber>)
{
    ...
}