2014-05-07 147 views
1

我有一个模块弹出窗口有一个gridview,这个gridview有很多行,我只希望用户能够选择一行。所以如果他们选择另一个,它将取消选择前一个。只允许在modalpopup中的gridview中选择一个复选框

我已经尝试了一些方法,但无法获取oncheckedchanged事件触发。 可以请别人帮助 干杯

<asp:button id="btnShowPopupOW" style="display: none" runat="server" /> 
<asp:modalpopupextender id="mpeOW" behaviorid="mpeOW" runat="server" targetcontrolid="btnShowPopupOW" 
    popupcontrolid="pnlpopupOW" cancelcontrolid="imgOWCancel" backgroundcssclass="modalBackground" /> 
<asp:panel id="pnlpopupOW" runat="server" width="600px" style="display: none;" class="ModalPanel"> 

      <div style="position: relative; min-height: 490px;"> 
       <asp:UpdatePanel ID="upExisting" runat="server" ChildrenAsTriggers="true"> 
        <ContentTemplate> 
         <table style="width: 600px;"> 
          <tr height="25px"> 
           <td> 
            <asp:Panel ID="pnlPrev" runat="server" Height="200px" ScrollBars="Auto" HorizontalAlign="Center"> 
             <asp:GridView ID="grdPrevious" runat="server" ClientIDMode="Static" AutoGenerateColumns="false" Width="100%" 
              ShowFooter="false" ShowHeaderWhenEmpty="false" DataKeyNames="ID" > 
              <Columns> 
               <asp:BoundField DataField="dates" HeaderText="Dates" /> 
               <asp:BoundField DataField="Prev" HeaderText="Previous" /> 
               <asp:TemplateField> 
                <ItemTemplate> 
                 <asp:CheckBox ID="ChkSelect" runat="server" OnCheckedChanged="ChkSelect_OnCheckedChanged" /> 
                </ItemTemplate> 
               </asp:TemplateField> 
              </Columns> 
             </asp:GridView> 
            </asp:Panel> 
           </td> 
          </tr> 
         </table> 
        </ContentTemplate> 
        <Triggers> 

        </Triggers> 
       </asp:UpdatePanel> 
       </div> 
       </asp:panel> 

与代码隐藏

protected void ChkSelect_OnCheckedChanged(object sender, EventArgs e) 
     { 
      CheckBox activeCheckBox = sender as CheckBox; 

      foreach (GridViewRow rw in grdPrevious.Rows) 
      { 
       CheckBox chkBx = (CheckBox)rw.FindControl("ChkSelect"); 
       if (chkBx != activeCheckBox) 
       { 
        chkBx.Checked = false; 
       } 
       else 
       { 
        chkBx.Checked = true; 
       } 
      } 
     } 
+0

您是否尝试过的AutoPostBack = “真”? – FodderZone

回答

0

你需要的大概就是下面的一个(单选)http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton(v=vs.110).aspx],而不是一个复选框。

您可以将RadioButton组合在一起,然后允许用户只选择一个项目并自动取消选择先前选择的项目 - 或者:确切地说是您的应用程序的行为。

此示例代码取自链接的MSDN文档。注意GroupName属性

<%@ Page Language="C#" AutoEventWireup="True" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title>RadioButton Example</title> 
    <script language="C#" runat="server"> 

    void SubmitBtn_Click(Object Sender, EventArgs e) { 

     if (Radio1.Checked) { 
      Label1.Text = "You selected " + Radio1.Text; 
     } 
     else if (Radio2.Checked) { 
      Label1.Text = "You selected " + Radio2.Text; 
     } 
     else if (Radio3.Checked) { 
      Label1.Text = "You selected " + Radio3.Text; 
     } 
    } 

    </script> 

</head> 
<body> 

<h3>RadioButton Example</h3> 
<form id="form1" runat="server"> 
    <h4>Select the type of installation you want to perform:</h4> 
    <asp:RadioButton id="Radio1" Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /> 
    <br /> 
    This option installs the features most typically used. <i>Requires 1.2 MB disk space.</i><br /> 
    <asp:RadioButton id="Radio2" Text="Compact" GroupName="RadioGroup1" runat="server"/> 
    <br /> 
    This option installs the minimum files required to run the product. <i>Requires 350 KB disk space.</i> 
    <br /> 
    <asp:RadioButton id="Radio3" runat="server" Text="Full" GroupName="RadioGroup1" /> 
    <br /> 
    This option installs all features for the product. <i>Requires 4.3 MB disk space.</i> 
    <br /> 
    <asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server"/> 
    <asp:Label id="Label1" font-bold="true" runat="server" /> 
</form> 

3

你可以不喜欢这样。使用单个复选框进行选择的jQuery ....

<ItemTemplate> 
    <asp:CheckBox ID="ChkSelect" runat="server" onclick="CheckOne(this)" /> 
</ItemTemplate> 
function CheckOne(obj) { 
    var grid = obj.parentNode.parentNode.parentNode; 
    var inputs = grid.getElementsByTagName("input"); 
    for (var i = 0; i < inputs.length; i++) { 
     if (inputs[i].type == "checkbox") { 
      if (obj.checked && inputs[i] != obj && inputs[i].checked) { 
       inputs[i].checked = false; 
      } 
     } 
    } 
} 
相关问题