Question

J'ai lu quelques messages ici et certains sont liés à mon problème, mais pour une raison quelconque les solutions proposées pour moi le travail Do not. Fondamentalement, j'ai deux contrôles GridView avec des cases à cocher. Ils ont exactement le même code, mais le feu juste différents événements. Et bien sûr avoir différents identifiants et leur information unique pertinente. Voici la chose bizarre, l'un d'entre eux fonctionne très bien, et l'autre ne fonctionne pas. Je signalerai à la fois ici, gardez à l'esprit que celui avec ID = « UnassignElegibilityChk » est le onte qui ne fonctionne pas. J'ai mis des points d'arrêt pour vérifier si je suis dans la fonction quand j'envoie la postback du chèque et rien.

Note: ces deux arborescences sont sur la même page, donc s'il vous plaît assumer la directive codebehind et tous les paramètres sont correctement assignés à la page (puisque l'un d'entre eux travaille, je suppose qu'il n'y a pas de problème avec ça)

Ceci est celui qui ne fonctionne pas:

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

Celui qui travaille est ci-dessous:

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

Le gridview travail tire cette fonction avec succès:

 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

L'un non de travail est censé tirer ce code et ce n'est pas.

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
Était-ce utile?

La solution

Je ne sais pas si quelque chose de ce qui suit permet de résoudre votre problème, mais ...

  1. pourquoi votre « travail » gestionnaire du CheckedChanged a une signature erronée? Il a besoin de l'expéditeur et les eventsargs.

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

  2. Pourquoi itérer-vous les Gridrows pour obtenir la case correspondante et la ligne qui a changé? Cela fonctionne aussi:

    Dim checkbox As CheckBox = DirectCast(sender,CheckBox)

Pour la ligne que vous suffit de lancer le NamingContainer de votre Checkbox à GridViewRow.

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

EDIT : Avez-vous REBIND la ElegibilitySelectedGridview-Grid sur Postback? Vous ne devriez faire que lorsque not Page.IsPostback, sinon les événements ne se déclenche pas.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top