【问题标题】:Can't find dropdownlist inside gridview在gridview中找不到下拉列表
【发布时间】:2014-06-25 08:58:00
【问题描述】:

我正在尝试将下拉列表绑定到数据集,但在我的 DDL 中得到空值。当我单击编辑模式的一行时,DDL 不会绑定,因为我找不到它。为什么我找不到行下拉列表并绑定它们??

using (var scTyche = new SqlConnection(ConfigurationManager.ConnectionStrings["KondorConnectionConnectionString"].ConnectionString))
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            var ddl = (DropDownList)row.FindControl("DropDownListFolders1");
            var da = new SqlDataAdapter();
            var dt = new DataTable();
            var ds = new DataSet();

            scTyche.Open();
            var cmdTyche = scTyche.CreateCommand();
            cmdTyche.CommandType = CommandType.StoredProcedure;
            cmdTyche.CommandText = "dbo.spGetFolders";
            cmdTyche.CommandTimeout = 60;
            cmdTyche.Parameters.Add("@nBranchId", SqlDbType.Int).Value = intBranchId;

            da.SelectCommand = cmdTyche;
            da.Fill(dt);
            ds.Tables.Add(dt);

            ddl.DataSource = ds;
            ddl.DataTextField = "strShort";
            ddl.DataValueField = "nId";
            ddl.DataBind();

            cmdTyche.Parameters.Clear();

            scTyche.Dispose();
            scTyche.Close();
        }
    }

我的aspx:

<asp:UpdatePanel runat="server" ID="update">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
            AllowSorting="True" AutoGenerateColumns="False" SkinID="gridviewGridlinesSkin"
            OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting"
            EmptyDataText="No positions found">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                         <asp:Label ID="Label4" runat="server" Text='<%# Eval("strPositionId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Folder" SortExpression="strFolderName">
                    <EditItemTemplate>
                        <BrummerComp:SortableDropDownList ID="DropDownListFolders1" Width="141px" runat="server"
                                SkinID="BCdropdownlistSkin"/>
                    </EditItemTemplate>
</asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

【问题讨论】:

  • 您在哪种方法下运行第一个代码部分?

标签: c# asp.net gridview


【解决方案1】:

如果您想从后面的代码手动绑定下拉菜单,我建议您在 GridView.OnDataBinding、GridView.OnDataBoung、GridView.OnRowDataBound 事件或 DropDown.OnDataBinding、DropDown.OnDataBound 数据绑定事件中执行此操作。

现在,您可能在错误的时间尝试数据绑定 - 可能是在将一行更改为编辑模式之前?

还要记住只有 1 行处于编辑模式 - 如果您使用 GridView.OnRowDataBound,您可以使用事件参数 (e.Row.RowState == DataControlRowState.Edit) 来确定当前事件是否正在处理编辑模式与否。这将减少代码中的开销并使其更易于调试(可能会隔离问题)。

【讨论】:

  • 将尝试将其绑定到另一个事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多