Newly added drop down list item attributes do not render

时间:2019-04-17 00:11:12

标签: vb.net drop-down-menu attributes

I have a drop down list where I am trying to add attributes to it's items. By using watch in VS2015, I can see that the attributes have been added but they are not being rendered on the page.

The DDL is in a gridview edititemtemplate. I have tried adding the attributes here with not luck. I was having trouble accessing the drop down list in the row. I ended up using a rowediting handler. I also thought the row might not be fully in edit mode so I tried making the row go into edit mode programmatically before my operations.

     <asp:GridView ID="gvUserDetails" runat="server" DataSourceID="SqlUserDetails" AutoGenerateColumns="False" DataKeyNames="ID,EmpID" >
            <Columns>

                <asp:TemplateField HeaderText="Emp ID" SortExpression="EmpID">
                    <EditItemTemplate>
                         <asp:Label runat="server" Text='<%# Bind("EmpID") %>' ID="LabelEmpIDUpdate"></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Bind("EmpID") %>' ID="LabelEmpID"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Notification Name" SortExpression="NotificationName">
                    <EditItemTemplate>

                        <asp:DropDownList ID="ddNotificationNameUpdt" runat="server" SelectedValue='<%# Bind("NotificationName") %>' DataSourceID="SqlNotificationNames" DataTextField="Name" DataValueField="Name"></asp:DropDownList>

                    </EditItemTemplate>

                    <ItemTemplate>
                        <asp:Label ID="LabelNotificationName" runat="server" Text='<%# Bind("NotificationName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Filter" SortExpression="Filter">
                    <EditItemTemplate>

                        <asp:DropDownList ID="ddNotificationFilterUpdt" runat="server" DataSourceID="SqlNotificationFilters" DataTextField="Filter" DataValueField="Filter" SelectedValue='<%# Bind("Filter") %>'></asp:DropDownList>

                    </EditItemTemplate>

                    <ItemTemplate>
                        <asp:Label ID="LabelNotificationFilter" runat="server" Text='<%# Bind("Filter") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Crew Filter" SortExpression="CrewFilter">
                    <EditItemTemplate>

                        <asp:DropDownList ID="ddCrewFilterUpdt" runat="server" SelectedValue='<%# Bind("CrewFilter") %>'>
                            <asp:ListItem>*</asp:ListItem>
                            <asp:ListItem>A</asp:ListItem>
                            <asp:ListItem>B</asp:ListItem>
                            <asp:ListItem>C</asp:ListItem>
                            <asp:ListItem>D</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>

                    <ItemTemplate>
                        <asp:Label ID="LabelCrewFilter" runat="server" Text='<%# Bind("CrewFilter") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date Begin" SortExpression="DateBgn">
                    <EditItemTemplate>

                        <asp:TextBox ID="TextBoxBgnUpdt" runat="server" Text='<%# Bind("DateBgn") %>' autocomplete="off" ></asp:TextBox>


                        <ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxBgnUpdt_CalendarExtender" TargetControlID="TextBoxBgnUpdt" ID="TextBoxBgnUpdt_CalendarExtender" PopupButtonID="TextBoxBgnUpdt"></ajaxToolkit:CalendarExtender>
                    </EditItemTemplate>

                    <ItemTemplate>
                        <asp:Label ID="LabelDateBgn" runat="server"  Text='<%# Bind("DateBgn") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date End" SortExpression="DateEnd">
                    <EditItemTemplate>

                        <asp:TextBox ID="TextBoxEndUpdt" runat="server" Text='<%# Bind("DateEnd") %>' autocomplete="off" ></asp:TextBox>

                        <ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxEndUpdt_CalendarExtender" TargetControlID="TextBoxEndUpdt" ID="TextBoxEndUpdt_CalendarExtender"></ajaxToolkit:CalendarExtender>
                    </EditItemTemplate>

                    <ItemTemplate>
                        <asp:Label ID="LabelDateEnd" runat="server" Text='<%# Bind("DateEnd") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>

                        <asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" CausesValidation="true"/>&nbsp;
                        <asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="ButtonCancel_Click" />&nbsp;
                        <asp:Button ID="ButtonClearEndDate" runat="server" Text="Clear End Date" OnClick="ButtonClearEndDate_Click" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btnEditNotification" runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" OnClick="btnEditNotification_Click"/>
                        &nbsp;<asp:Button ID="btnDeleteNotification" runat="server" Text="Delete" CommandName="Delete" CausesValidation="False"  OnClientClick = " return confirm('Are you sure you want to delete this notification?');"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <AlternatingRowStyle BackColor="LemonChiffon" />

        </asp:GridView>
    Dim gv As GridView = CType(sender, GridView)
    gv.Rows(e.NewEditIndex).RowState = DataControlRowState.Edit
    For i As Integer = 0 To gv.Rows.Count - 1
                If i <> e.NewEditIndex Then
                    gridRow = gv.Rows(i)
                    For Each cell As Control In gridRow.Cells
                        For Each ctl As Control In cell.Controls
                            If TypeOf ctl Is DropDownList Then
                                foundDropList = CType(ctl, DropDownList)
                                If foundDropList.ID.Equals("ddNotificationNameUpdt") Then
                                    Debug.Print("found the list")
                                    dropListToUse = foundDropList
                                End If
                            End If
                        Next
                    Next
                End If
            Next

    For Each item As ListItem In dropListToUse.Items
                item.Attributes("Title") = NotificationNameTable(item.Text)
            Next

I am expecting the user to click on the Edit button of the gridview and then dropdownlist of the row being edited to have new attributes called "Title" for each of the list items. Any help?

1 个答案:

答案 0 :(得分:1)

我能够弄清楚。如果我使用RowDataBound事件来捕获GridView Edit按钮,并确保RowState和DataControlRowState.Edit都大于0,那么我将以所需的状态运行GridView。然后,我将能够使用e.Row.FindControl()找到我的控件。然后,我遍历列表项并添加在屏幕上正确呈现的属性。