簡體   English   中英

帶下拉菜單的Asp.net gridview:在SelectedIndexChanged事件中,行索引始終為0

[英]Asp.net gridview with dropdown: Rowindex is always 0 in SelectedIndexChanged event

我遇到一個困擾我的問題。 任何幫助將非常感激。 我有一個網格視圖,顯示將哪些研究項目分配給哪些人。 應該可以使用下拉菜單更改分配。 這是標記:

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

正如您在注釋中看到的那樣,無論我單擊哪一行,與sender參數關聯的GridViewRow始終是第一行。 同樣,下拉菜單的選定值始終是其初始值,而不是由觸發了SelectedIndexChanged事件的用戶選擇的值。

我在網上搜索了此問題的解決方案,發現它通常是由(1)在回發時觸發databind事件和(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回發之后運行。 我也使用了更新面板,但是在嘗試調試時將其刪除。

我現在都沒主意了,所以我願意接受任何建議。 提前致謝

看來我發現了asp.net的奇怪特性。 我在RowDataBound方法中有以下代碼:

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

我刪除了此代碼,並且下拉列表SelectedIndexChanged方法現在可以正常工作。 更改RowDataBound方法后,獲取下拉列表的父級GridViewRow的所有3種方法都可以使用,以便它不會刪除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中的所選索引更改事件出現問題,我將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM