C# Radiobuttonlist事件不总是触发

C# Radiobuttonlist事件不总是触发,c#,asp.net,radio-button,radiobuttonlist,C#,Asp.net,Radio Button,Radiobuttonlist,我有一个radiobuttonlist,其中包含一个selectedindexchanged事件,用于更新搜索功能。其中一个项是在aspx中指定的,其他项是附加的数据绑定项。无论我将什么设置为默认值,该项都不会触发事件。所有其他物品将触发该事件。此外,似乎在选择了“死亡”项目后,事件根本不会触发 如何跟踪错误并更正?这是当前的代码 编辑:抱歉,如果短版本有误导性。我不知道该包括什么。这是整页 所有aspx: <%@ Page Language="C#" MasterPageFil

我有一个radiobuttonlist,其中包含一个selectedindexchanged事件,用于更新搜索功能。其中一个项是在aspx中指定的,其他项是附加的数据绑定项。无论我将什么设置为默认值,该项都不会触发事件。所有其他物品将触发该事件。此外,似乎在选择了“死亡”项目后,事件根本不会触发

如何跟踪错误并更正?这是当前的代码

编辑:抱歉,如果短版本有误导性。我不知道该包括什么。这是整页

所有aspx:

     <%@ Page Language="C#" MasterPageFile="~/MSDS/MSDS.master" EnableEventValidation="false"
    AutoEventWireup="true" CodeFile="SearchMSDS.aspx.cs" Inherits="MSDS_ByDept" Title="NCLWeb - Search MSDS" %>

<%@ Register Assembly="SqlWhereBuilder" Namespace="UNLV.IAP.WebControls" TagPrefix="cc1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageContent" runat="Server">
    <h2>
        <asp:Label ID="lblTitle" runat="server">Search Active MSDS</asp:Label></h2>
    <table class="style1">
        <tr>
            <td style="width: 435px" valign="top">
                <asp:Panel runat="server" ID="pnlSearch" DefaultButton="btnSearch">
                    <asp:TextBox ID="txtSimpleSearch" runat="server" Width="262px"></asp:TextBox>
                    <asp:Button ID="btnSearch" runat="server" Text="Search" Width="96px" OnClick="btnSearch_Click" />
                    <br />
                    <asp:LinkButton ID="btnAdvSearch" runat="server" OnClick="btnAdvSearch_Click" Font-Size="Small">Show Advanced Search</asp:LinkButton>
                </asp:Panel>
                <asp:Panel ID="pnlAdvSearch" runat="server" Width="635px" DefaultButton="btnRunAdvSearch">
                    <asp:Button ID="btnRunAdvSearch" runat="server" OnClick="btnRunAdvSearch_Click" Text="Advanced Search" />
                    <cc1:SqlWhereBuilder ID="SqlWhereBuilder1" runat="server" ClientCodeLocation="../JavaScripts/SqlWhereBuilder.js"
                        FieldsFile="../ConfigFiles/SearchMSDS.config" OperatorListsFile="../ConfigFiles/SearchMSDS.config"
                        ValueEntryFile="../ConfigFiles/SearchMSDS.config">
                    </cc1:SqlWhereBuilder>
                    <br />
                    <br />
                </asp:Panel>
                <cc2:CollapsiblePanelExtender ID="pnlAdvSearch_CollapsiblePanelExtender" runat="server"
                    CollapseControlID="btnAdvSearch" Collapsed="True" Enabled="True" ExpandControlID="btnAdvSearch"
                    TargetControlID="pnlAdvSearch">
                </cc2:CollapsiblePanelExtender>
            </td>
            <td valign="top">
                <asp:Panel ID="pnlStatus" runat="server">
                    <asp:RadioButtonList ID="rblStatus" runat="server" AppendDataBoundItems="True"
                        AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="DisplayValue"
                        DataValueField="Value" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
                        RepeatDirection="Horizontal" CellPadding="3" CellSpacing="3" 
                        CausesValidation="True" Visible="True">
                        <asp:ListItem Selected="True">All</asp:ListItem>
                    </asp:RadioButtonList>
                </asp:Panel>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NCLWebConnectionString %>"
                    SelectCommand="getOptionList" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="msds_Status" Name="ListName" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:UpdatePanel runat="server" ID="upd2">
                    <ContentTemplate>
                        <asp:Button ID="btnExport" runat="server" Text="Export Results" 
                            OnClick="btnExport_Click1" UseSubmitBehavior="False" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100" DynamicLayout="False">
                <ProgressTemplate>
                    <img src="../images/loading.gif" alt="Loading..." style="text-align: center" />
                    <asp:Label ID="lblProgress" runat="server"></asp:Label></ProgressTemplate>
            </asp:UpdateProgress>
            <asp:GridView ID="gridResults" runat="server" DataSourceID="sqlMSDS" OnRowDataBound="GridView1_RowDataBound"
                AllowPaging="True" PageSize="25" AllowSorting="True" OnSelectedIndexChanged="gridResults_SelectedIndexChanged"
                AutoGenerateColumns="False" EmptyDataText="No matching MSDS Sheets." OnSorted="gridResults_Sorted">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" Visible="false"
                        ReadOnly="True" SortExpression="ID" />
                    <asp:BoundField DataField="ChemicalTitle" HeaderText="ChemicalTitle" SortExpression="ChemicalTitle" />
                    <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer" SortExpression="Manufacturer" />
                    <asp:BoundField DataField="UsageDept" HeaderText="UsageDept" SortExpression="UsageDept" />
                    <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                    <asp:BoundField DataField="Health" HeaderText="Health" visible="false" SortExpression="Health" />
                    <asp:BoundField DataField="Fire" HeaderText="Fire"  visible="false" SortExpression="Fire" />
                    <asp:BoundField DataField="Reactivity" HeaderText="Reactivity"  visible="false" SortExpression="Reactivity" />
                    <asp:BoundField DataField="DateUpdated" HeaderText="DateUpdated" SortExpression="DateUpdated" />
                </Columns>
                <SelectedRowStyle BackColor="Yellow" />
            </asp:GridView>
            <asp:SqlDataSource ID="sqlMSDS" OnSelected="sqlMSDS_OnSelected" runat="server" ConnectionString="<%$ ConnectionStrings:NCLWebConnectionString %>"
                SelectCommand="SELECT [ID]
                                      ,[ChemicalTitle]
                                      ,[Manufacturer]
                                      ,[UsageDept]
                                      ,[Notes]
                                      ,[Health]
                                      ,[Fire]
                                      ,[Reactivity]
                                      ,[DateUpdated]
                                      FROM [msds_Sheets]" OnSelecting="sqlMSDS_Selecting"></asp:SqlDataSource>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="btnRunAdvSearch" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="rblStatus" EventName="SelectedIndexChanged" />
            <asp:AsyncPostBackTrigger ControlID="btnExport" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
</asp:Content>

搜索活动MSDS

显示高级搜索

全部的
和代码隐藏:

    List<String> safeWords = new List<String>();

protected void Page_Load(object sender, EventArgs e)
{
    pnlStatus.Visible = User.IsInRole("msds_Admin");

    gridResults.DataKeyNames = new String[] { "id" };

    txtSimpleSearch.Focus();

    if (!IsPostBack)
    {
        safeWords.Add("delete");
        safeWords.Add("insert");
        safeWords.Add("update");
        safeWords.Add("set");
        safeWords.Add("exec");
        safeWords.Add("N'");

        sqlMSDS.SelectCommand += " Where status = 0 ";

        Session["Sql"] = sqlMSDS.SelectCommand;

        try
        {
            Session["OriginalSQL"] = sqlMSDS.SelectCommand.Remove(sqlMSDS.SelectCommand.IndexOf("Where"));
        }
        catch (Exception)
        {
            Session["OriginalSQL"] = sqlMSDS.SelectCommand;
        }
    }
}

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((Label)UpdateProgress1.FindControl("lblProgress")).Text = "Searching...";

            if (btnSearch.Visible)
            {
                btnSearch_Click(null, null);

                if (RadioButtonList1.SelectedValue != "All")
                {
                    sqlMSDS.SelectCommand += " And Status = " + RadioButtonList1.SelectedValue;
                }
                else
                {
                    //Somehow force the grid to research using no status parameter
                    sqlMSDS.SelectCommand = Session["Sql"].ToString();
                }
            }
            else
            {
                btnRunAdvSearch_Click(null, null);

                if (RadioButtonList1.SelectedValue != "All")
                {
                    if (sqlMSDS.SelectCommand.Contains("Where"))
                    {
                        sqlMSDS.SelectCommand += " And Status = " + RadioButtonList1.SelectedValue;
                    }
                    else
                    {
                        sqlMSDS.SelectCommand += " Where Status = " + RadioButtonList1.SelectedValue;
                    }
                }
                else
                {
                    //Somehow force the grid to research using no status parameter
                    sqlMSDS.SelectCommand = Session["Sql"].ToString();
                }
            }
        }
List safeWords=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
pnlStatus.Visible=User.IsInRole(“msds_Admin”);
gridResults.DataKeyNames=新字符串[]{“id”};
txtSimpleSearch.Focus();
如果(!IsPostBack)
{
添加(“删除”);
安全词语。添加(“插入”);
添加(“更新”);
添加(“集合”);
safeWords.Add(“exec”);
添加(“N”);
sqlMSDS.SelectCommand+=“其中状态为0”;
会话[“Sql”]=sqlMSDS.SelectCommand;
尝试
{
会话[“OriginalSQL”]=sqlMSDS.SelectCommand.Remove(sqlMSDS.SelectCommand.IndexOf(“Where”);
}
捕获(例外)
{
Session[“OriginalSQL”]=sqlMSDS.SelectCommand;
}
}
}
受保护的无效RadioButtonList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
((Label)UpdateProgress1.FindControl(“lblProgress”).Text=“搜索…”;
如果(btnSearch.Visible)
{
b搜索\单击(空,空);
如果(RadioButtonList1.SelectedValue!=“全部”)
{
sqlMSDS.SelectCommand+=”和Status=“+RadioButtonList1.SelectedValue;
}
其他的
{
//以某种方式强制网格不使用状态参数进行研究
sqlMSDS.SelectCommand=Session[“Sql”].ToString();
}
}
其他的
{
btnRunAdvSearch\u单击(空,空);
如果(RadioButtonList1.SelectedValue!=“全部”)
{
if(sqlMSDS.SelectCommand.Contains(“Where”))
{
sqlMSDS.SelectCommand+=”和Status=“+RadioButtonList1.SelectedValue;
}
其他的
{
sqlMSDS.SelectCommand+=“Where Status=“+RadioButtonList1.SelectedValue;
}
}
其他的
{
//以某种方式强制网格不使用状态参数进行研究
sqlMSDS.SelectCommand=Session[“Sql”].ToString();
}
}
}

此RadioButton列表是否指定为另一个UpdatePanel的AsyncPostBackTrigger?如果是,请检查以下链接:

我已复制了该行为,并使用以下脚本修复此问题:

 $(function () {
        $("input[type='radio']:first", $("#<%= RadioButtonList1.ClientID %>")).attr("checked", true);
    });
$(函数(){
$(“input[type='radio']:first”,$(“#”).attr(“checked”,true);
});
如果无法使用jQuery,请尝试以下javascript:

 window.onload = function () {
        window.document.getElementById("<%= RadioButtonList1.ClientID %>_0").checked = true;
    };


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "RadioButtonListDefaultValue", String.Format("window.document.getElementById('{0}_0').checked = true;", RadioButtonList1.ClientID), true);
        }
    }
window.onload=函数(){
window.document.getElementById(“\u 0”).checked=true;
};
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
ClientScript.RegisterStartupScript(this.GetType(),“RadioButtonListDefaultValue”,String.Format(“window.document.getElementById({0}0')。checked=true;”,RadioButtonList1.ClientID),true);
}
}
全部
单击“全部”不会更改所选索引,因为该项目已被选中,因此事件不会触发。选择任何其他选项将更改所选选项并引发事件。我相信您的目标是在单击“全部”时显示所有状态的结果。您应该执行以下操作之一:

  • 当页面初始加载时显示这些结果,因为已选择“全部”
  • 添加一个“搜索”按钮以启动回发,而不是使用单选按钮的自动回发
  • 将RadioButtonList替换为DropDownList,并为其提供两个初始列表项:“选择状态”-其值为空字符串,以及“全部”其值为“全部”。与RadioButtonList相比,“选择状态”虚拟条目在DropDownList中看起来不太合适
在上面列出的选项中,我个人更喜欢“搜索”按钮,因为AutoPostBack让我作为一个用户感到烦恼。我讨厌让网页运行的自动回邮下拉列表
<asp:ListItem Selected="True">All</asp:ListItem>