2013-10-23 40 views
1

當管理員批准/拒絕任何文檔一次,然後當管理員再次登錄,然後他/她不能再次批准/拒絕文檔,dropdownlist將被禁用,只有那些可以一次批准/拒絕再當管理員查看任何新的文件,然後下拉會使得當管理員批准/拒絕該文件,然後它會關閉下拉列表不批准/再在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; 
    } 
} 

但是這段代碼告訴我所有的dropdownlist都是禁用的。

任何解決方案我將如何做到這一點?

+1

你真的應該改寫這一職務。這是一個長時間運行的句子。 –

+0

什麼是您的數據源 - DataSet,DataTable或IEnumerable?請發佈您的數據源。 – Win

+0

我可以顯示錶中的數據.. – user2883796

回答

1

根據你的評論,我假設你的數據源是DataTable或DataSet。

如果是這樣,你要投DataItem的DataRowView的RowDataBound事件要獲得狀態列的值。

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

thanku你的回覆,但它顯示錯誤「狀態既不是DataColumn也不是DataRelation for table表」。 – user2883796

+0

以上代碼僅用於演示目的。 **您將需要根據您的應用程序更換該列。**大多數情況下它可能是** ApproveID **。看到我更新的答案。 – Win

+0

thanku你的回覆,這對我真的很有幫助... b – user2883796

0

你可以問哪一行,你是與此代碼:

if (e.Row.RowIndex == <aRowNumber>) 
{ 
    ... 
} 
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; 

       } 

      } 

     } 
+0

什麼是「(YOUR-OBJECT)」? – user2883796

+0

不管他綁定到這個網格的對象,沒有提供 – PJM