简体   繁体   English

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

[英]How to set selected value on DropDownList edit template?

I have a DropDownList in the edit template bound with some data. 我在编辑模板中有一些数据绑定的DropDownList While I also have a Label in the item template bound with the submitted data. 虽然我在项目模板中还具有与提交的数据绑定的Label Now, I am not able to get the Label whenever I called the below method. 现在,无论何时调用以下方法,我都无法获取Label The count is always null / empty. 计数始终为空/空。 Can anyone help me? 谁能帮我?

Code: 码:

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();
        }
    } 
}

Design: 设计:

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

Rows Count Scope not available in RowDataBound event of GridView .But, current Row available in RowDataBound Event as e.Row . GridView的 RowDataBound事件中行 计数范围不可用。但是,在RowDataBound事件中, 当前行作为e.Row提供 So, remove for loop.Use the below code 因此,删除for循环。使用以下代码

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

Instead 代替

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

May be this post helps... 可能这篇文章有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM