PostBack没有发生单选按钮

时间:2014-01-26 08:03:13

标签: c# asp.net

当我检查Employee时,会发生回发,但它不适用于rbBatch。为什么会这样。

ASPX:

<asp:RadioButton ID="rbBatch" runat="server" Text="Batch" GroupName="OB"
CausesValidation="false" AutoPostBack="true" 
OnCheckedChanged="rbBatch_CheckedChanged"/>

<asp:RadioButton ID="rbEmployee" runat="server" Text="Employee" GroupName="OB"
CausesValidation="false" AutoPostBack="true" 
OnCheckedChanged="rbEmployee_CheckedChanged"/>

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
 <Triggers>
   <asp:AsyncPostBackTrigger ControlID="rbBatch" EventName="CheckedChanged"/>
   <asp:AsyncPostBackTrigger ControlID="rbEmployee" EventName="CheckedChanged"/>
 </Triggers>
 <ContentTemplate>
   <asp:GridView ID="gvBatch" runat="server"></asp:GridView>
   <asp:GridView ID="gvMain" runat="server" visible="false"></asp:GridView>
 </ContentTemplate>
</asp:UpdatePanel>

C#:

protected void rbBatch_CheckedChanged(object sender, EventArgs e)
{
  if(rbBatch.Checked)
   {
     gvBatch.visible=true;
     gvMain.visible=false;
   }
}

protected void rbEmployee_CheckedChanged(object sender, EventArgs e)
{
  if(rbEmployee.Checked)
   {
     gvBatch.visible=false;
     gvMain.visible=true;
   }
}

3 个答案:

答案 0 :(得分:1)

AsyncPostBackTrigger仅适用于UpdatePanel,因此将单选按钮移动到UpdatePanel并重试。

答案 1 :(得分:0)

您是否尝试过CausesValidation =“true”?

答案 2 :(得分:0)

这是因为当您选择A时,B的事件不会触发(除非最近被选中)。

更好的解决方案如下:

protected void rbBatch_CheckedChanged(object sender, EventArgs e)
{
  handle();
}

protected void rbEmployee_CheckedChanged(object sender, EventArgs e)
{
  handle();
}

private void handle()
{
   if(rbBatch.Checked)
   {
     gvBatch.visible=true;
     gvMain.visible=false;
   }
   else if(rbEmployee.Checked)
   {
     gvBatch.visible=false;
     gvMain.visible=true;
   }
}