如何在DropDownList编辑模板上设置选定的值?

时间:2012-03-20 02:31:05

标签: c# asp.net visual-studio-2010 gridview

我在编辑模板中有DropDownList绑定了一些数据。虽然我在项目模板中也有Label与提交的数据绑定。现在,每当我调用以下方法时,我都无法获得Label。计数始终为空/空。任何人都可以帮助我吗?

代码:

protected void ManageStaffGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (ManageStaffGrid.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < ManageStaffGrid.Rows.Count; i++)
        {
            #region get selected items bound
            Label sectionname = ((Label)ManageStaffGrid.Rows[i].Cells[2].FindControl("lblSection"));
            Label functionname = ((Label)ManageStaffGrid.Rows[i].Cells[3].FindControl("lblFunction"));
            Label rolename = ((Label)ManageStaffGrid.Rows[i].Cells[5].FindControl("lblRole"));
            #endregion

            ListBox ListSection = (ListBox)e.Row.Cells[2].FindControl("ListSection");
            ListSection.DataSource = dbmanager.GetAllSection();
            ListSection.DataBind();

            DropDownList ddlFunction = (DropDownList)e.Row.Cells[3].FindControl("ddlFunction");
            ddlFunction.DataSource = dbmanager.GetAllFunction();
            ddlFunction.DataBind();
            ddlFunction.SelectedValue = functionname.ToString();

            DropDownList ddlRole = (DropDownList)e.Row.Cells[5].FindControl("ddlRole");
            ddlRole.DataSource = dbmanager.GetAllRole();
            ddlRole.DataBind();
            ddlRole.SelectedValue = rolename.ToString();
        }
    } 
}

设计

<asp:GridView ID="ManageStaffGrid" runat="server" BorderStyle="Solid" 
        BorderWidth="1px" onrowediting="ManageStaffGrid_RowEditing" 
        onrowdatabound="ManageStaffGrid_RowDataBound" AutoGenerateColumns="False" 
        onrowcancelingedit="ManageStaffGrid_RowCancelingEdit" 
        onrowupdating="ManageStaffGrid_RowUpdating">
        <AlternatingRowStyle BackColor="#CCCCCC" />
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"/>
        <asp:BoundField DataField="Uid" HeaderText="User ID" ReadOnly="True"/>
        <asp:TemplateField HeaderText="Section">
            <EditItemTemplate>
                <asp:ListBox ID="ListSection" SelectionMode="Multiple" runat="server" 
                    onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);" 
                    Height="20px" Width="80px"></asp:ListBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblSection" runat="server" Text='<%# Bind("Section") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Function">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFunction" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFunction" runat="server" Text='<%# Bind("Function") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Sex" HeaderText="Sex" ReadOnly="True"/>
        <asp:TemplateField HeaderText="Role">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlRole" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblDropdown" runat="server" Text='<%# Bind("Role") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True">
        <ControlStyle Font-Size="Medium" />
        </asp:CommandField>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="DeleteBtn" runat="server"

                OnClientClick="return confirm('Are you sure you want to delete this question?');" 
                onclick="DeleteBtn_Click">Delete</asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

行计数范围在 GridView 事件 GridView 中不可用.But,当前行在RowDataBound事件中可用as e.Row 。 因此,请删除for循环。使用以下代码

Label sectionname = ((Label)e.Row.FindControl("lblSection"));

相反

Label sectionname = ((Label)ManageStaffGrid.Rows[i].Cells[2].FindControl("lblSection"));

可能是这篇帖子有帮助...