在转发器内设置dropdownlist的值

时间:2016-11-11 20:52:28

标签: c# asp.net

我有一个转发器,里面有一个下拉列表。转发器外还有另一个单独的下拉列表。用户将在此ddl中选择一个值,单击一个按钮,并且所选的值将填充在转发器内的所有空白ddl中。注意:ddl包含selectedvalue(不是int)的文本。

不幸的是我的代码无效。

我的标记:

<asp:Repeater ID="AttendanceRepeater" runat="server" DataSourceID="AttendanceSqlDataSource" OnItemDataBound="AttendanceRepeater_ItemDataBound">
                <ItemTemplate>
                    <br />
                    <table>
                        <tr>
                            <td style="width: 50px"></td>
                            <td style="width: 300px">
                                <asp:Label ID="StudentNameLabel" runat="server" Text='<%# Eval("STUDENT_NAME") %>'></asp:Label>
                            </td>
                            <td style="width: 200px">
                                <asp:DropDownList ID="AttendStatusDropDownList" runat="server"
                                    SelectedValue='<%# Eval("ATTENDANCE_STATUS") %>'>
                                    <asp:ListItem Value="" Text=""></asp:ListItem>
                                    <asp:ListItem Value="ABSENTE" Text="Absent Excused"></asp:ListItem>
                                    <asp:ListItem Value="ABSENTU" Text="Absent Unexcused"></asp:ListItem>
                                    <asp:ListItem Value="CIN" Text="Chk In No (Absent)"></asp:ListItem>
                                    <asp:ListItem Value="CIY" Text="Chk In Yes (Present)"></asp:ListItem>
                                    <asp:ListItem Value="NR" Text="Attend Not Reqd"></asp:ListItem>
                                    <asp:ListItem Value="PRESENT" Text="Present"></asp:ListItem>
                                    <asp:ListItem Value="TM005" Text="Time Missed 005 Min"></asp:ListItem>
                                    <asp:ListItem Value="TM010" Text="Time Missed 010 Min"></asp:ListItem>
                                    <asp:ListItem Value="TM015" Text="Time Missed 015 Min"></asp:ListItem>
                                    <asp:ListItem Value="TM030" Text="Time Missed 030 Min"></asp:ListItem>
                                    <asp:ListItem Value="TM045" Text="Time Missed 045 Min"></asp:ListItem>
                                    <asp:ListItem Value="TM060" Text="Time Missed 1hr"></asp:ListItem>
                                    <asp:ListItem Value="TM075" Text="Time Missed 1hr 15mi"></asp:ListItem>
                                    <asp:ListItem Value="TM090" Text="Time Missed 1hr 30mi"></asp:ListItem>
                                    <asp:ListItem Value="TM105" Text="Time Missed 1hr 45mi"></asp:ListItem>
                                    <asp:ListItem Value="TM120" Text="Time Missed 2hr"></asp:ListItem>
                                    <asp:ListItem Value="TM135" Text="Time Missed 2hr 15mi"></asp:ListItem>
                                    <asp:ListItem Value="TM150" Text="Time Missed 2hr 30mi"></asp:ListItem>
                                    <asp:ListItem Value="TM165" Text="Time Missed 2hr 45mi"></asp:ListItem>
                                    <asp:ListItem Value="TM180" Text="Time Missed 3hr"></asp:ListItem>
                                    <asp:ListItem Value="TM195" Text="Time Missed 3hr 15mi"></asp:ListItem>
                                    <asp:ListItem Value="TM210" Text="Time Missed 3hr 30mi"></asp:ListItem>
                                    <asp:ListItem Value="TM225" Text="Time Missed 3hr 45mi"></asp:ListItem>
                                    <asp:ListItem Value="TM240" Text="Time Missed 4hr"></asp:ListItem>
                                    <asp:ListItem Value="TM255" Text="Time Missed 4hr 15mi"></asp:ListItem>
                                    <asp:ListItem Value="TM310" Text="Time Missed 4hr 30mi"></asp:ListItem>
                                    <asp:ListItem Value="TM325" Text="Time Missed 4hr 45mi"></asp:ListItem>
                                    <asp:ListItem Value="WD" Text="Withdrawn"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="AttendStatusValidator" runat="server"
                                    ErrorMessage="Required field" ControlToValidate="AttendStatusDropDownList">
                                </asp:RequiredFieldValidator>
                            </td>
                            <td style="width: 200px">
                                <asp:TextBox ID="CommentsTextBox" runat="server" TextMode="MultiLine" 
                                    Text='<%# Eval("COMMENTS") %>'></asp:TextBox>
                            </td>
                            <td>
<%--                                <asp:CheckBox ID="ChangeCheckBox" runat="server" Visible="true" />--%>
                                <asp:Label ID="PeopleCodeIdLabel" runat="server" Visible="false" Text='<%# Eval("PEOPLE_CODE_ID") %>'></asp:Label>
                                <asp:Label ID="AcademicYearLabel" runat="server" Visible="false" Text='<%# Eval("ACADEMIC_YEAR") %>'></asp:Label>
                                <asp:Label ID="AcademicTermLabel" runat="server" Visible="false" Text='<%# Eval("ACADEMIC_TERM") %>'></asp:Label>
                                <asp:Label ID="AcademicSessionLabel" runat="server" Visible="false" Text='<%# Eval("ACADEMIC_SESSION") %>'></asp:Label>
                                <asp:Label ID="EventIdLabel" runat="server" Visible="false" Text='<%# Eval("EVENT_ID") %>'></asp:Label>
                                <asp:Label ID="EventSubTypeLabel" runat="server" Visible="false" Text='<%# Eval("EVENT_SUB_TYPE") %>'></asp:Label>
                                <asp:Label ID="SectionLabel" runat="server" Visible="false" Text='<%# Eval("SECTION") %>'></asp:Label>
                                <asp:Label ID="TranAttendanceIdLabel" runat="server" Visible="false" Text='<%# Eval("TranAttendanceId") %>'></asp:Label>
                            </td>
                        </tr>
                    </table>
                    <br />
                </ItemTemplate>
            </asp:Repeater>

按钮代码:(注意下拉列表的命名.AntendanceStatusdropdownlist和AttendStatusAllDropDownList)

    protected void ApplyToAllButton_Click(object sender, EventArgs e)
{
    string defaultStatus = AttendStatusAllDropDownList.SelectedValue;

    foreach (RepeaterItem item in AttendanceRepeater.Items)
    {
        DropDownList ddl = (DropDownList)item.FindControl("AttendanceStatusDropDownList");
        if (string.IsNullOrWhiteSpace(ddl.SelectedValue))
        {
            //set the ddl to the value selected from the other ddl
            ddl.SelectedValue = defaultStatus;

        }
    }
    //Save changes to all
    SaveButton_Click(null, null);
}

值不会在转发器内的下拉列表中填充。我搜索了谷歌,但找不到任何具体到我正在尝试的东西。任何帮助将不胜感激。

提前致谢。 皮特

1 个答案:

答案 0 :(得分:1)

您的RecursiveIteratorIterator::CHILD_FIRST值与Repeater内DropDownList的FindControl存在差异。

转发器:ID

FindControl:AttendStatusDropDownList

请参阅下面的更新代码段。

AttendanceStatusDropDownList

ASPX页面

protected void ApplyToAllButton_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in AttendanceRepeater.Items)
    {
        DropDownList ddl = (DropDownList)item.FindControl("AttendStatusDropDownList");

        if (ddl != null && string.IsNullOrEmpty(ddl.SelectedValue))
        {
            ddl.SelectedValue = AttendStatusAllDropDownList.SelectedValue;
        }
    }    
}