2013-02-04 52 views
1

我有一些自定義模板一個GridView:GridView不更新回發值?

<asp:GridView ID="gvGroups" runat="server" AutoGenerateColumns="False" 
    CssClass="table table-hover table-striped" GridLines="None" > 
     <Columns> 
      <asp:BoundField DataField="GroupDescription" HeaderText="Name" ReadOnly="True" 
       SortExpression="GroupDescription" /> 
      <asp:TemplateField HeaderText="Administrator"> 
       <ItemTemplate> 
        <asp:CheckBox ID="cbAdmin" runat="server" 
        Checked='<%# Boolean.Parse((Boolean)Eval("IsReadOnly") ? "True" : "False") ? false : true %>'/> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Remove"> 
       <ItemTemplate> 
        <asp:CheckBox ID="cbRemove" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="ID" SortExpression="GroupID" Visible="False"> 
       <ItemTemplate> 
        <asp:Label ID="lblID" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

然後我有一個按鈕,我點擊,並且應該改變分組管理和刪除檢查組。

這裏是按鈕的代碼:

protected void btnSave_Click(object sender, EventArgs e) 
     { 
      foreach (GridViewRow gvr in gvGroups.Rows) 
      { 
       CheckBox cbAdmin = (CheckBox)gvr.FindControl("cbAdmin"); 
       CheckBox cbRemove = (CheckBox)gvr.FindControl("cbRemove"); 
       Label lblID = (Label)gvr.FindControl("lblID"); 
       int id; 
       bool idValid = int.TryParse(lblID.Text,out id); 
       bool isReadOnly = !cbAdmin.Checked; 


       if (idValid) 
       { 
        Group g = SecurityManager.GetGroup(id); 

        if (g.IsReadOnly != isReadOnly) 
        { 
         bool updateSuccess = SecurityManager.ChangeGroupPermissions(id, isReadOnly); 
        } 

        if (cbRemove.Checked) 
        { 
         bool removeEmpSuccess = SecurityManager.RemoveEmployeesFromGroup(id); 
         bool removeSuccess = SecurityManager.RemoveGroup(id); 
        } 
       } 
      } 
     } 

我使用的調試器,甚至當我取消對所有組管理員,當我看着cbAdmin.Checked,它仍然是真實的,這是相同的值,它從開始,因此沒有任何事情發生。

可能是什麼問題?爲什麼我沒有看到按鈕回發中的更新值?

感謝

+0

http://stackoverflow.com/questions/8936633/checkbox-checked-state-inside-gridview – Joe

回答

1

你必須內if(!IsPostBack){ }

還需要設置文本框爲「true」

+0

現在這個工作,但我不得不刪除項目後刷新看到變化。 – jmasterx

+0

多種方式,我的最愛:location.href ='yourpage.aspx' –

+0

我不知道我的理解。 – jmasterx

0

我猜你是數據綁定在回發GridView的AutoPostBack屬性調用GridView#DataBind()。這將再次從數據庫加載數據並防止更改。所以UE的PagePostBack屬性:

protected void Page_Load(Object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     DataBindGridView(); 
    } 
}