2012-03-20 20 views
1

私はDropDownListを編集テンプレートにいくつかのデータでバインドしています。提出されたデータにバインドされたアイテムテンプレートにはLabelもあります。今、私はLabelを以下のメソッドを呼び出すたびに取得できません。カウントは常にnull /空です。誰でも助けてくれますか?DropDownList編集テンプレートで選択した値を設定する方法は?

コード:

protected void ManageStaffGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (ManageStaffGrid.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow) 
    { 
     for (int i = 0; i < ManageStaffGrid.Rows.Count; i++) 
     { 
      #region get selected items bound 
      Label sectionname = ((Label)ManageStaffGrid.Rows[i].Cells[2].FindControl("lblSection")); 
      Label functionname = ((Label)ManageStaffGrid.Rows[i].Cells[3].FindControl("lblFunction")); 
      Label rolename = ((Label)ManageStaffGrid.Rows[i].Cells[5].FindControl("lblRole")); 
      #endregion 

      ListBox ListSection = (ListBox)e.Row.Cells[2].FindControl("ListSection"); 
      ListSection.DataSource = dbmanager.GetAllSection(); 
      ListSection.DataBind(); 

      DropDownList ddlFunction = (DropDownList)e.Row.Cells[3].FindControl("ddlFunction"); 
      ddlFunction.DataSource = dbmanager.GetAllFunction(); 
      ddlFunction.DataBind(); 
      ddlFunction.SelectedValue = functionname.ToString(); 

      DropDownList ddlRole = (DropDownList)e.Row.Cells[5].FindControl("ddlRole"); 
      ddlRole.DataSource = dbmanager.GetAllRole(); 
      ddlRole.DataBind(); 
      ddlRole.SelectedValue = rolename.ToString(); 
     } 
    } 
} 

デザイン:

<asp:GridView ID="ManageStaffGrid" runat="server" BorderStyle="Solid" 
     BorderWidth="1px" onrowediting="ManageStaffGrid_RowEditing" 
     onrowdatabound="ManageStaffGrid_RowDataBound" AutoGenerateColumns="False" 
     onrowcancelingedit="ManageStaffGrid_RowCancelingEdit" 
     onrowupdating="ManageStaffGrid_RowUpdating"> 
     <AlternatingRowStyle BackColor="#CCCCCC" /> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"/> 
     <asp:BoundField DataField="Uid" HeaderText="User ID" ReadOnly="True"/> 
     <asp:TemplateField HeaderText="Section"> 
      <EditItemTemplate> 
       <asp:ListBox ID="ListSection" SelectionMode="Multiple" runat="server" 
        onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);" 
        Height="20px" Width="80px"></asp:ListBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblSection" runat="server" Text='<%# Bind("Section") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Function"> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddlFunction" runat="server"> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblFunction" runat="server" Text='<%# Bind("Function") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="Sex" HeaderText="Sex" ReadOnly="True"/> 
     <asp:TemplateField HeaderText="Role"> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddlRole" runat="server"> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="lblDropdown" runat="server" Text='<%# Bind("Role") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True"> 
     <ControlStyle Font-Size="Medium" /> 
     </asp:CommandField> 
     <asp:TemplateField> 
     <ItemTemplate> 
      <asp:LinkButton ID="DeleteBtn" runat="server" 

       OnClientClick="return confirm('Are you sure you want to delete this question?');" 
       onclick="DeleteBtn_Click">Delete</asp:LinkButton> 
     </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

答えて

0

行としてRowDataBoundイベントに利用できるGridViewの .ButのRowDataBoundイベントに範囲が利用できない現在の行をカウントe.Row。 だから、...

loop.Useについては、以下のコード

Label sectionname = ((Label)e.Row.FindControl("lblSection")); 

代わり

Label sectionname = ((Label)ManageStaffGrid.Rows[i].Cells[2].FindControl("lblSection")); 

ががある可能性がありますポストは助けを削除

関連する問題