2010-11-25 69 views
0

我一直在这里阅读几篇文章,有些与我的问题有关,但由于某些原因,提供的解决方案不适合我。基本上我有两个带复选框的GridView控件。他们有完全相同的代码,但只是发起不同的事件。当然有不同的ID和他们相关的独特信息。这是奇怪的事情,其中​​一个很好,另一个没有。我将在这里发布这两个,请记住ID =“UnassignElegibilityChk”的那个是不起作用的onte。当我从支票发送回发并且没有任何内容时,我已经设置了断点来测试我是否在函数内部。OnCheckedChange事件不会执行我的事件

注意:这两个treeviews都在同一页面上,所以请假设codebehind指令和所有的设置都正确地分配给页面(因为其中一个正在工作,我会认为这没有问题)

这是不工作的一个:

<table><tr><td class="style1"> 
    <asp:GridView ID="ElegibilitySelectedGridview" runat="server" 
     AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4" 
     ForeColor="#333333" GridLines="None" Width="475px"> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
    <Columns> 
     <asp:TemplateField HeaderText="ID"> 
      <ItemTemplate> 
       <asp:Label ID="lblID0" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Select"> 
      <ItemTemplate> 
       <asp:CheckBox ID="UnassignElegibilityChk" runat="server" AutoPostBack="true" OnCheckedChanged="UnAssignElegibilityRecord"/> 
      </ItemTemplate>     
     </asp:TemplateField> 
     <asp:boundfield datafield="Name" HeaderText="Elegible Item" /> 
     </Columns> 
     <EditRowStyle BackColor="#999999" /> 
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
     <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
     <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
    </asp:GridView> 
    </td></tr></table> 

正在工作的一个低于:

<table><tr><td class="style1"> 
<asp:DropDownList ID="ElegibilityGroupDDL" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ElegibilityGroupDDL_SelectedIndexChanged"> 
    <asp:ListItem Value="0">ROOT</asp:ListItem> 
</asp:DropDownList> 
</td></tr><tr><td class="style1"> 
    <asp:TextBox ID="ElegibilityNameTxt" runat="server" Width="464px"></asp:TextBox> 
    </td></tr><tr><td class="style1"> 
    <asp:Button ID="ElegibilitySaveBtn" runat="server" Text="Save Elegibility" /> 
    &nbsp;</td></tr><tr><td class="style1"> 
    <asp:Label ID="ElegibilityMsgLbl" runat="server" Text="" style="color: #0066CC"></asp:Label> 
    </td></tr><tr style="background:silver"><td class="style1"> 
    &nbsp;</td></tr><tr><td class="style1"> 

    <asp:GridView ID="ElegibilityGridView" runat="server" 
     AutoGenerateColumns="False" DataKeyNames="ID" BackColor="White" 
     BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
     ForeColor="Black" GridLines="Vertical" Width="95%"> 
     <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
     <asp:TemplateField HeaderText="ID"> 
      <ItemTemplate> 
       <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Select"> 
      <ItemTemplate> 
       <asp:CheckBox ID="AssignElegibilityChk" runat="server" AutoPostBack="true" OnCheckedChanged="AssignElegibility"/> 
      </ItemTemplate>     
     </asp:TemplateField> 
     <asp:boundfield datafield="Name" HeaderText="Elegible Item" /> 
     </Columns> 
     <FooterStyle BackColor="#CCCC99" /> 
     <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
     <RowStyle BackColor="#F7F7DE" /> 
     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
     <SortedAscendingCellStyle BackColor="#FBFBF2" /> 
     <SortedAscendingHeaderStyle BackColor="#848384" /> 
     <SortedDescendingCellStyle BackColor="#EAEAD3" /> 
     <SortedDescendingHeaderStyle BackColor="#575357" /> 
    </asp:GridView> 
    </td></tr></table> 

工作gridview的火灾第功能成功:

Public Sub AssignElegibility() 
    If Page.IsPostBack Then 
     For Each row As GridViewRow In ElegibilityGridView.Rows 
      Dim checkbox As CheckBox = CType(row.FindControl("AssignElegibilityChk"), CheckBox) 
      'Check if the checkbox is checked.' 
      'value in the HtmlInputCheckBox Value property is set as the //value of the delete commands parameter.' 
      If checkbox.Checked Then 
       ' Retreive the Employee ID' 
       Dim lblID As Label = CType(row.FindControl("lblID"), Label) 
       Dim elegibilityID As Integer = Convert.ToInt32(lblID.Text) 
       Elegibility.AddElegibilityMapping(CInt(Request("ResID")), elegibilityID) 
       ElegibilitySelectedItems(CInt(Request("ResID"))) 
       ElegibilityGroupDDL_SelectedIndexChanged() 
      End If 
     Next 
    End If 
End Sub 

非工作人员应该触发此代码,事实并非如此。

Public Sub UnAssignElegibilityRecord(ByVal sender As Object, ByVal e As System.EventArgs) 
    PrintLine("Inside the sub") 
    If Page.IsPostBack Then 
     For Each row As GridViewRow In ElegibilitySelectedGridview.Rows 
      Dim checkbox As CheckBox = CType(row.FindControl("UnassignElegibilityChk"), CheckBox) 
      'Check if the checkbox is checked.' 
      'value in the HtmlInputCheckBox Value property is set as the //value of the delete commands parameter.' 
      If checkbox.Checked Then 
       ' Retreive the Employee ID' 
       Dim lblID As Label = CType(row.FindControl("lblID0"), Label) 
       Dim elegibilityID As Integer = Convert.ToInt32(lblID.Text) 
       Elegibility.RemoveElegibilityMapping(elegibilityID) 
       ElegibilitySelectedItems(CInt(Request("ResID"))) 
       ElegibilityGroupDDL_SelectedIndexChanged() 
      End If 
     Next 
    End If 
End Sub 
+1

@Jovanky - 添加“请帮助”不会让人们更快地回答你。 – 2010-11-25 15:37:18

+0

我的朋友,有些事情是作为第二天性来的。请不要关注这样的细节。生活要简单得多。祝您愉快! – 2010-11-25 15:43:38

回答

1

我不知道,如果下面的任何解决您的问题,但...

  1. 为什么你的“工作”了的CheckedChanged的处理程序的签名错误?它需要发件人和eventsargs。

    protected Sub AssignElegibility(ByVal sender As Object, ByVal e As System.EventArgs)

  2. 你为什么迭代Gridrows获得已更改的复选框和行?这个作品也:

    Dim checkbox As CheckBox = DirectCast(sender,CheckBox)

为了让行,你只需要投你的复选框GridViewRow的NamingContainer。

Dim row as GridViewRow =DirectCast(checkbox.NamingContainer,GridViewRow)

编辑:你重新绑定ElegibilitySelectedGridview网格上回发? 您应该只在not Page.IsPostback时这样做,否则事件不会触发。