开发者

Gridview OnRowCreated event gets triggered multiple times - dropdownlist gets populated twice

Attaching the code below - its a simple code that just initializes the dropdown (calling ddl.Clear()) and adds an item with text "select" which has value "-1".

Filldropdowns(ddl) are methods that get specific data and populate it into the dropdown method. The methods individually work fine outside the gridview on normal dropdownlists.

What happens is this eventhandler gets called more than once for each gridview row. As an end result, the dropdowns contain double the values they are supposed to contain (the complete set of values just get repeated).

Anyone knows why this is happening? Turning off appenddatabounditems is not an option because i need the items to be appended below the "select".

Strange thing is, when I debug, even on the second time call for the same row, it shows that the dropdownvalues contain zero items and then contains the required number of items. But by the time I reach grdAccountsMapping_DataBound() handler, it shows twice the number of items.

public void grdAccountsMapping_RowCreated(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;

    if (row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlGrdFeeFormat = (DropDownList)row.FindControl("ddlFeeFormat");
        DropDownList ddlGrdTransactionMode = (DropDownList)row.FindControl("ddlTransactionMode");
        DropDownList ddlGrdFeeParticular = (DropDownList)row.FindControl("ddlFeeParticular");
        DropDownList ddlGrdCompany = (DropDownList)row.FindControl("ddlCompany");
        DropDownList ddlGrdAccounts = (DropDownList)row.FindControl("ddlAccounts");
        DropDownList ddlGrdFeeBook = (DropDownList)row.FindControl("ddlFeeBook");

        InitializeDropdown(ddlGrdFeeBook);
        InitializeDropdown(ddlGrdFeeFormat);
        InitializeDropdown(ddlGrdTransactionMode);
        InitializeDropdown(ddlGrdFeeParticular);
        InitializeDropdown(ddlGrdCompany);

        getDDLValues.FillFeeBooks(ddlGrdFeeBook);
        getDDLValues.FillFeeFormats(ddlGrdFeeFormat);
        getDDLValues.FillPaymentModes(ddlGrdTransactionMode);
        getDDLValues.FillFeeParticulars(ddlGrdFeeParticular);
        getDDLValues.FillAccountingCompanies(ddlGrdCompany);
    }
}

The aspx page grid definition looks like this

<asp:GridView CssClass="Grid" ID="grdAccountsMapping" runat="server" Width="98%"
EmptyDataText="No records found in this section" EmptyDataRowStyle-Height="40px"
AutoGenerateColumns="False" AccessKey="2" DataKeyNames="FAM_MAP_ID" OnRowCommand="grdAccountsMapping_RowCommand"
OnRowCreated="grdAccountsMapping_RowCreated" OnDataBound="grdAccountsMapping_DataBound"
Visible="false">
<AlternatingRowStyle CssClass="alternateGridItem" HorizontalAlign="Left" />
<RowStyle CssClass="gridItem" HorizontalAlign="Left" />
<EmptyDataRowStyle CssClass="gridItem" HorizontalAlign="Center" Font-Bold="True"
    ForeColor="Red" Height="40px" VerticalAlign="Middle" />
<HeaderStyle CssClass="tabledarklabel" />
<FooterStyle CssClass="Grid_Footer" />
<Columns>
    <asp:TemplateField HeaderText="Fee Book">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdFeeBook" CssClass="dropdownwidth3" runat="server" AppendDataBoundItems="True">
            </asp:DropDownList>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Fee Format">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdFeeFormat" CssClass="dropdownwidth3" runat="server" AppendDataBoundItems="True">
            </asp:DropDownList>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Transaction Mode">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdTransactionMode" CssClass="dropdownwidth3" runat="server"
                AppendDataBoundItems="True" >
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Fee Particulars">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdFeeParticular" CssClass="dropdownwidth3" runat="server"
                AppendDataBoundItems="True">
            </asp:DropDownList>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Company">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdCompany" CssClass="dropdownwidth3" runat="server" AppendDataBoundItems="True">
            </asp:DropDownList>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Account">
        <ItemTemplate>
            <asp:DropDownList ID="ddlGrdAccounts" CssClass="dropdownwidth3" runat="server" AppendDataBoundItems="True">
            </asp:DropDownList>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Active">
        <ItemTemplate>
            <asp:CheckBox ID="chkActive" runat="server" />
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
        <HeaderStyle Width="5%" />
    </asp:TemplateField>
</Columns>
<PagerSettings Visible="False" />
<PagerStyle BorderStyle="None" />
</asp:GridView>

The other methods used just retrieve a dataset for the data needed, assign it to the datasourceo of the dropdown and then bind it to the dropdownlist control. Those methods work fine on dropdowns outside the grid, and I have u开发者_如何学Pythonnit tested them - so I am sure the problem is not there.


How are you binding the grid? Are you calling DataBind() and/or Rebind() multiple times by accident?


We just deleted the file and created it again and it seemed to work. I din't have much time to dig into the details since we were on a deadline, but that wierd issue has not happened again!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜