0

当搜索框返回结果时,我试图绑定 ddl。DDL 绑定如下:

<asp:TemplateField HeaderText="Service Area" SortExpression="ServiceArea">
  <EditItemTemplate>
    <asp:DropDownList ID="drp_Val_ServiceArea" runat="server" DataTextField="ServiceArea"
        DataValueField='<%# Eval("ServiceAreaId") %>'>
    </asp:DropDownList>
  </EditItemTemplate>
  <ItemTemplate>
    <asp:Label ID="lbl_Bind_ServiceArea" runat="server" Text='<%# Bind("ServiceArea") %>'></asp:Label>
  </ItemTemplate>
</asp:TemplateField>

当我使用搜索和网格加载时,页面加载正常。当我按下编辑按钮时,异常是对象未设置为实例。和错误ddl.DataTextField = "ServiceArea"

protected void grd_User_RowEditing(object sender, GridViewEditEventArgs e)
{
grd_User.EditIndex = e.NewEditIndex;


using (var _db = new dbDataContext())
{
    var result = from s in _db.tbl_ServiceAreas
                    where s.Deleted == false
                    select new
                    {
                        s.ServiceAreaId,
                        s.ServiceArea
                    };

    if ((DataControlRowState.Edit) > 0)
    {

        DropDownList ddl = (DropDownList)grd_User.Rows[e.NewEditIndex].Cells[0].FindControl("drp_Val_ServiceArea");

        ddl.DataTextField = "ServiceArea";
        ddl.DataValueField = "ServiceAreaId";
        ddl.DataSource = result;
        ddl.DataBind();

    }
}
 LoadGrid();
}

这是因为网格已经绑定到 ServiceArea 了吗?

以下是我一直在寻找的答案。

        protected void grd_User_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("drp_Val_ServiceArea");

            using (var _db = new dbDataContext())
            {
                var result = from s in _db.tbl_Users
                             where s.Deleted == false
                             select new
                             {
                                 s.ServiceAreaId,
                                 s.tbl_ServiceArea.ServiceArea
                             };

                foreach (var item in result)
                {
                    ddl.DataTextField = item.ServiceArea;
                    ddl.DataValueField = item.ServiceAreaId.ToString();
                    ddl.DataSource = result;
                    ddl.DataBind();
                }

            }
        }
    }
4

0 回答 0