简体   繁体   中英

dropdownlist disable in asp.net

when admin approve/reject any document once then when admin again login then he/she not be able to approve/reject documents again and dropdownlist will be disabled only for those documents which can be once approve/reject then when admin view any new documents then drop down will be enable and when admin approve/reject this document then it will be disable dropdownlist for not approve /reject again

for this i do this

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;
    }
}

but this code show me all dropdownlist are disable .

any solution how i will do this?

You need to access the data item from each row, I am assuming it is available on the object your are binding to the grid, and determine if they have been approved / rejected. If so then you should run you logic to disable:

        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;

                }

            }

        }

According to your comment, I assume your DataSource is either DataTable or DataSet.

If so, you want to cast DataItem to DataRowView inside RowDataBound Event to get the value of the status column.

在此处输入图片说明

<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;
        }
    }
}

you can ask on which row you are with this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM