0

我正在使用 UpdatePanel 和 Ajax Tabcontainer。我有一个项目模板列,它同时具有复选框和单选按钮。根据一种位类型的字段,它们中的任何一个一次都是可见的。

<asp:GridView ID="gvAutoMatchFund" runat="server" AutoGenerateColumns="False" ClientIDMode="Static"
    AllowPaging="True" PageSize="50" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
    OnRowDataBound="gvAutoMatchFund_RowDataBound" BorderWidth="1px" CellPadding="3"
    onpageindexchanging="gvAutoMatchFund_PageIndexChanging" 
    Width="100%">
    <Columns>
        <asp:BoundField DataField="DYNAMO_FUNDNAME" HeaderText="Dynamo Fund Name" ItemStyle-Width="25%" />
        <asp:BoundField DataField="DYNAMO_FUNDID" HeaderText="Dynamo Fund ID" ItemStyle-Width="25%" />
        <asp:BoundField DataField="INVESTRAN_FUNDNAME" HeaderText="Investran Fund Name" ItemStyle-Width="25%" />
        <asp:BoundField DataField="INVESTRAN_SYSTEMFUNDID" HeaderText="Inv. Sys. Fund ID" />
        <asp:TemplateField ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center">
            <HeaderTemplate>
                <asp:CheckBox ID="chkSelAll" Text="Select" runat="server"  />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkSel" runat="server" Visible='<%#(!(bool)Eval("ISMULTIPLE"))%>' />
                <asp:RadioButton ID="rdoSel" runat="server" Visible='<%#((bool)Eval("ISMULTIPLE"))%>' />                                                                    
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#C6E5F5" Font-Bold="True" ForeColor="#4695BD" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
    <EmptyDataRowStyle ForeColor="Maroon" />
</asp:GridView>

我在检查回发后填充它

 if (!Page.IsPostBack)
 {
    ...
    PopulateMyGV();
 }

当单击按钮时,我试图遍历此网格视图中的每个项目并使用 FindControl 方法查找复选框和单选按钮。到目前为止是有效的,但是即使选中了单选按钮,它也总是错误的。但是,无论是否选中,复选框都可以正常工作(为选中的属性提供正确的值)。请参阅下面的代码

在按钮单击

foreach (GridViewRow gvr in gvAutoMatchFund.Rows)
{   

    CheckBox chkSel = (CheckBox)gvr.FindControl("chkSel");      
    RadioButton rdoSel = (RadioButton)gvr.FindControl("rdoSel");

    if (chkSel != null)
    {
        if (chkSel.Checked)
        {
            ...
        }
    }

    if (rdoSel != null)
    {
        if (rdoSel.Checked)
        {
            ...
        }
    }

}

我错过了什么?

4

1 回答 1

0

花了很多时间后,最后我们发现发生这种奇怪的行为是因为我们在 RowDataBound 事件中以编程方式更改了单选按钮的 ID。请参阅下面的代码。

    protected void gvAutoMatchFund_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult = (USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult)e.Row.DataItem;

            RadioButton rdoSel = (RadioButton)e.Row.FindControl("rdoSel");
            rdoSel.ID = "rdoSel_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.DYNAMO_FUNDID
                        + "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_SYSTEMFUNDID
                        + "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_FUNDID;         

        }
    }

所以我删除了上面的逻辑并使用下面的逻辑来传递各种参数以及单选按钮

    protected void gvAutoMatchFund_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult = (USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult)e.Row.DataItem;

            RadioButton rdoSel = (RadioButton)e.Row.FindControl("rdoSel");
            string sValue = "rdoSel_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.DYNAMO_FUNDID
                        + "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_SYSTEMFUNDID
                        + "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_FUNDID;

            rdoSel.Attributes.Add("value", sValue);     

        }
    }

这解决了我上面描述的两个问题。

于 2013-05-01T15:25:10.513 回答