gpt4 book ai didi

c# - Radiobuttonlist 事件并不总是触发

转载 作者:行者123 更新时间:2023-11-30 15:44:25 29 4
gpt4 key购买 nike

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

我怎样才能找到错误并改正?这是当前代码。

编辑:如果简短的版本误导了您,我们深表歉意。我不确定要包括什么。这是整个页面。

所有 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>

和代码隐藏:

    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();
}
}
}

最佳答案

这是发生在我身上的事情和解决方法。

我有一个单选按钮列表,它不在更新面板中,但被定义为更新面板的触发器。我也定义了 onselectedindexchanged。 Radiobuttonlist 有第一个 listitem 属性 selected="true",所以它会在页面加载时默认被选中。然后选择第二个列表项工作正常,回发并更新更新面板。

但是,再次选择第一项不会触发 onselectedindexchanged 事件。使用 Firefox 强大的 Inspect Element 实用程序,我能够确定服务器正在生成 html 元素(仅检查第一个默认列表项)。但是由于单选按钮列表不在更新面板中,所以第二个项目的 checked="checked"属性永远不会写入浏览器,即使客户端第二个单选按钮在视觉上看起来已被选中。因此,当第二次选择原始项目时,onselectedindexchanged 没有触发服务器端,因为新选择的索引已经在 POST 事件中指示为已选择。

如果列表位于要刷新的面板内,则不会出现此问题,因为回发会导致浏览器在新选择的项目上接收带有 checked="checked"的"new"元素。我的页面布局使得将它们放在同一个面板中很不方便,所以我的解决方法是将单选按钮列表放在它自己的小更新面板中。无论哪个适合您,答案是确保单选按钮列表在某些更新面板中,以便在选择时可以将每个项目的选中属性发送到浏览器。

关于c# - Radiobuttonlist 事件并不总是触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6098432/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com