简体   繁体   English

为什么此事件异步触发?

[英]Why does this event fire asynchronously?

I've got this code to prevent two opposite radio buttoms from both being checked: 我有以下代码可以防止同时检查两个相对的无线电按钮:

RadioButton rbUSCitizenOrPermResY = null;
RadioButton rbUSCitizenOrPermResN = null;

. . .

rbUSCitizenOrPermResY = new RadioButton
{
    CssClass = "finaff-webform-field-input"
}; // doesn't allow assignment to CheckedChanged above
rbUSCitizenOrPermResY.CheckedChanged += new EventHandler(rbUSCitizenOrPermResY_Changed);

. . .

private void rbUSCitizenOrPermResY_Changed(object sender, EventArgs e)
{ 
    if (rbUSCitizenOrPermResN.Checked)
    {
        rbUSCitizenOrPermResN.Checked = false;
    }
}

So, if the "Yes" radio button is checked, it unchecks the "No" radio button if the "No" radio button is checked. 因此,如果选中“是”单选按钮,则选中“否”单选按钮则取消选中“否”单选按钮。

Supposedly. 据说。

In actuality, this event handler is entered, but not when the radio button is clicked. 实际上,此事件处理程序输入的,但单击单选按钮时不会输入。 Rather, it fires later, after I click the "Save" button. 而是在我单击“保存”按钮后触发。 Is this an asynchronous event handler, that just wakes up whenever it feels like it? 这是一个异步事件处理程序,它会在需要时立即唤醒吗? If so, how can I get it to "straighten up and fly right"/"shape up or ship out"? 如果是这样,我如何才能“拉直并向右飞” /“整形或装运”?

It's taken me a while to figure out what you are describing, because I'm used to thinking about this stuff client side, not server side. 我花了一些时间弄清楚您要描述的内容,因为我习惯于考虑这种东西,而不是服务器端。 But since you are doing server side, I think you need to introduce some kind of grouping construct for your radio buttons. 但是由于您正在服务器端,所以我认为您需要为单选按钮引入某种分组构造。 I just tried an example app where I used a RadioButtonList containing ListItems (rather unintuitively, they are not called Radiobuttons). 我只是尝试了一个示例应用程序,其中使用了一个包含ListItems的RadioButtonList(直觉上,它们不称为Radiobuttons)。 But using the RadioButtonList to group them, they behave correctly on the client side, ie they are exclusive. 但是,使用RadioButtonList对其进行分组,它们可以在客户端正确运行,即它们是互斥的。 Clicking on one unselects the other. 单击一个取消选择另一个。 Here is the markup I used (does this fit your scenario?): 这是我使用的标记(这适合您的情况吗?):

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:listItem ID="rad1" runat="server"></asp:listItem>
    <asp:listItem ID="rad2" runat="server"></asp:listItem>
</asp:RadioButtonList>

The listitem control got translated in the designer file as a System.Web.UI.WebControls.ListItem listitem控件在设计器文件中转换为System.Web.UI.WebControls.ListItem

Incidentally, I mocked this up as a Web Form application in Visual Studio. 顺便说一句,我在Visual Studio中将其作为Web窗体应用程序进行了模拟。 I don't have any Sharepoint stuff installed, but I think the principle is the same (I hope). 我没有安装任何Sharepoint东西,但我认为原理是相同的(我希望如此)。

The generated HTML ended up looking like this: 生成的HTML最终看起来像这样:

<table id="MainContent_RadioButtonList1">
    <tr>
        <td><span ID="rad1"><input id="MainContent_RadioButtonList1_0" type="radio" name="ctl00$MainContent$RadioButtonList1" value="" /></span></td>
    </tr>
    <tr>
         <td><span ID="rad2"><input id="MainContent_RadioButtonList1_1" type="radio" name="ctl00$MainContent$RadioButtonList1" value="" /></span></td>
    </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM