2017-07-17 52 views
0

我有一個問題,一直在推動我瘋了。任何幫助將非常感激。我有一個網格視圖,顯示什麼研究項目分配給什麼人。應該可以使用下拉菜單更改分配。這裏的標記:Asp.net gridview與下拉列表:RowIndex在SelectedIndexChanged事件中始終爲0

<asp:GridView ID="assignmentsGridView" runat="server" AutoGenerateColumns="false" CellPadding="2" Font-Size="Smaller" 
 
OnRowDataBound="assignmentsGridView_RowDataBound" OnRowCommand="assignmentsGridView_RowCommand"> 
 
<Columns> 
 
    <asp:BoundField HeaderText="Ticker" DataField="Item" /> 
 
    <asp:BoundField HeaderText="Name" DataField="Name" /> 
 
    <asp:TemplateField HeaderText="Assignment" ItemStyle-HorizontalAlign="Center" ControlStyle-Font-Size="Small"> 
 
     <ItemTemplate> 
 
      <asp:DropDownList ID="assignmentDropDown" runat="server" Visible="true" 
 
       OnSelectedIndexChanged="assignmentDropDown_SelectedIndexChanged" AutoPostBack="true" > 
 
       <asp:ListItem Text="Joe" Value="Joe"></asp:ListItem> 
 
       <asp:ListItem Text="Aron" Value="Aron"></asp:ListItem> 
 
       <asp:ListItem Text="Frank" Value="Frank"></asp:ListItem> 
 
       <asp:ListItem Text="Ross" Value="Ross"></asp:ListItem> 
 
       <asp:ListItem Text="Alex" Value="Alex"></asp:ListItem> 
 
      </asp:DropDownList> 
 
     </ItemTemplate> 
 
    </asp:TemplateField> 
 
    <asp:BoundField HeaderText="Analyst Complete" DataField="AnalystComplete" ItemStyle-HorizontalAlign="Center" ControlStyle-Font-Size="Smaller"/> 
 
    <asp:TemplateField HeaderText="Mark Complete" ControlStyle-Font-Size="Smaller"> 
 
     <ItemTemplate> 
 
      <asp:Button ID="completeButton" runat="server" Text="Incomplete" CommandArgument='<%# Eval("Item") %>' 
 
       CommandName="Complete" Font-Size="Smaller" /> 
 

 
     </ItemTemplate> 
 
    </asp:TemplateField> 
 

 
</Columns> 
 
</asp:GridView>

我想,當用戶改變下拉的selectedIndex更新數據庫。我處理在這裏:

protected void assignmentDropDown_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (Session["IsCompany"] == null) 
    { 
     Session["IsCompany"] = assignmentsDropDown.SelectedValue == "Company"; 
    } 
    bool? isCompany = Session["IsCompany"] as bool?; 
    int isCo = (isCompany == true) ? 1 : 0; 

    DropDownList ddl = sender as DropDownList; 
    GridViewRow gvr = ddl.NamingContainer as GridViewRow; 
    //ri is always 0! 
    int ri = gvr.RowIndex; 
    gvr = (GridViewRow)(((Control)sender).NamingContainer); 
    //ri is always 0! 
    ri = gvr.RowIndex; 
    gvr = ((GridViewRow)ddl.Parent.Parent); 
    //ri is always 0! 
    ri = gvr.RowIndex; 

    string selAnalyst = ddl.SelectedValue; 
    //selAnalsyt always = initial value! 
    selAnalyst = ddl.SelectedItem.Value; 
    //selAnalsyt always = initial value! 
    string ticker = gvr.Cells[0].Text; 
    //ticker always is first data row 
} 

正如你可以在註釋中看到,不管我點擊哪一行,與發件人參數關聯的GridViewRow永遠是第一行。此外,下拉菜單的選定值始終是其初始值,而不是由用戶選擇的SelectedIndexChanged事件觸發的值。

我在網上搜索了這個問題的解決方案,發現它通常是由於(1)數據綁定事件在回發中被觸發和(2)源數據中的重複行造成的。我檢查了重複的數據並解決了這個問題,並將主鍵放在適當的表上。我還確保不要在回發中重新綁定數據。這裏的Page_Load方法:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.User.Identity.IsAuthenticated) 
    { 
     FormsAuthentication.RedirectToLoginPage(); 
    } 
    else 
    { 
     string userName = Page.User.Identity.Name; 
     if (Session["UserMod"] == null) 
     { 
      this.userMod = new userModel(userName); 
     } 
     else 
     { 
      this.userMod = Session["UserMod"] as userModel; 
     } 

     if (!IsPostBack) 
     { 
      getPeopleList(userName); 
      getTickerList(userName); 


      if (this.userMod.IsResearchAdmin) 
      { 
       getAdminList(); 
      } 
      else 
      { 

       assignmentsDropDown.Visible = false; 
       assignmentsGridView.Visible = false; 
       assignmentsButton.Visible = false; 
       assignmentsLabel.Visible = false; 
       addTextBox.Visible = false; 
       Image1.Visible = true; 
       analystDropdown.Visible = false; 
      } 
     } 
    } 
} 

我有一個方法的RowDataBound以及其中設置下拉每一行的選定值。在調試時,我已驗證該方法僅在初始頁面加載時運行,而不是在從SelectedIndexChanged回發之後運行。我也在使用更新面板,但在嘗試調試時刪除了該面板。

我現在都不在了,因此我願意接受任何建議。在此先感謝

+0

您是否調試過代碼?我在我的機器上試過了你的代碼,它工作的很完美。 'gvr.RowIndex;'給我正確的索引。如果您在第一行的下拉列表中更改值,則它將爲零。嘗試改變其他行的值並調試...你應該得到正確的索引。 –

+0

是的,我只是再次調試,並遇到完全相同的問題。我非常難過。 –

回答

0

它看起來像我發現asp.net奇怪的特質。我曾在的RowDataBound方法如下代碼:

if (IsCompany) 
    { 
     e.Row.Cells.Remove(e.Row.Cells[1]); 
    } 

我刪除了這些代碼,並在下拉的SelectedIndexChanged方法現在可以正常工作。在更改RowDataBound方法後,所有3種獲取下拉列表的父GridViewRow的方法都在工作,以便它不會刪除gridview中的任何單元格。

我現在相信在數據綁定期間添加或刪除單元格導致GridViews中下拉菜單的SelectedIndexChanged事件出現意外問題。這裏是RowDataBoundMethod的全碼:

protected void assignmentsGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (Session["IsCompany"] == null) 
    { 
     Session["IsCompany"] = entityDropDown.SelectedValue == "Company"; 
    } 
    bool? isCompany = Session["IsCompany"] as bool?; 
    bool IsCompany = (bool)isCompany; 

    TableCell cell; 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 

     cell = e.Row.Cells[0]; 
     HyperLink link = new HyperLink(); 
     if (IsCompany) 
     { 
      link.NavigateUrl = "~/CompanyPage.aspx?Ticker=" + cell.Text; 
     } 
     else 
     { 
      link.NavigateUrl = "~/PeoplePage.aspx?PersonId=" + cell.Text; 
     } 
     link.Text = cell.Text; 
     cell.Controls.Clear(); 
     cell.Controls.Add(link); 
     /* with this code included, I experience problems with the SelectedIndexChanged event of the dropdown 
     if (IsCompany) 
     { 
      e.Row.Cells.Remove(e.Row.Cells[1]); 
     } 
     */ 

     cell = e.Row.Cells[2]; 

     var dd = e.Row.FindControl("assignmentDropDown") as DropDownList; 
     string assignment = ((DataRowView)e.Row.DataItem)["Assignment"].ToString(); 
     foreach (ListItem li in dd.Items) 
     { 
      if (li.Value == assignment) 
      { 
       li.Selected = true; 
       break; 
      } 
     } 
     cell = e.Row.Cells[4]; 
     if (cell.Text == "False") 
     { 
      //"completeButton" 
      var cb = e.Row.FindControl("completeButton") as Button; 
      cb.Enabled = false; 
      cb.Visible = false; 
     } 
     else 
     { 
      ((Button)cell.FindControl("completeButton")).CommandArgument = e.Row.RowIndex.ToString(); 
     } 
    } 
} 

如果有人可以證實,刪除裏面的RowDataBound細胞會導致在asp.net選定的索引更改事件的問題我將不勝感激。

+0

您還需要分享rowdatabound事件的代碼。 –

相關問題