On my custom NewForm.aspx I have got
<SharePoint:FormField runat="server" ID="field_Project" ControlMode="New" FieldName="Project" />
Is it possible to replace it with asp net DropDownList and save lookup value using that control?
Marcin,
You can do this by inducing JavaScript on your NewForm.aspx page, as described in the following article:
http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?PageType=4&ListId={72C1C85B-1D2D-4A4A-90DE-CA74A7808184}&pID=740
EDIT
If you want filtered lookup drop downs or Cascaded lookup drop downs, you can have a look at:
SolidQ Filtered Lookup Columns for Cascading Dropdowns in SharePoint 2010
SharePoint Cascaded Lookups - JavaScript based
These should help you achieve what you are looking for!
Customizing new forms sharepint 2010 with asp.net controls
list schema ( ... )
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form UseLegacyForm="TRUE" Type="NewForm" Url="NewForm.aspx" SetupPath="features\$SharePoint.Feature.DeploymentPath$\InvestorAcceptancesFileListDefinition\new2.aspx" WebPartZoneID="Main" />
</Forms>
webpartpage aspx ( ... )
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="190px" valign="top" class="ms-formlabel">
<h3 class="ms-standardheader">
<nobr>Title<span class="ms-formvalidation"> *</span></nobr>
</h3>
</td>
<td width="400px" valign="top" class="ms-formbody">
<SharePoint:FormField runat="server" ID="field_Title" ControlMode="New" FieldName="Title" />
<SharePoint:FieldDescription runat="server" ID="field_Title_Description" FieldName="Title"
ControlMode="New" />
</td>
</tr>
<!-- karstein: END -->
<tr>
<td width="190px" valign="top" class="ms-formlabel">
<h3 class="ms-standardheader">
<nobr>Projekty w realizacji</nobr>
</h3>
</td>
<td width="400px" valign="top" class="ms-formbody">
<asp:DropDownList runat="server" ID="ddlProjects" AutoPostBack="true" DataTextField="Title" DataValueField="ID" OnSelectedIndexChanged="ddlProjects_OnSelectedIndexChanged" AppendDataBoundItems="true">
<asp:ListItem Text="Wybierz" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDdlProjects" runat="server" ErrorMessage="Wybierz projekt" ControlToValidate="ddlProjects" InitialValue="Wybierz"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="190px" valign="top" class="ms-formlabel">
<h3 class="ms-standardheader">
<nobr>Kosztorys ofertowy</nobr>
</h3>
</td>
<!-- field_Project$ctl00$Lookup tak jest wysylana wartosc na serwer -->
<td width="400px" valign="top" class="ms-formbody">
<asp:DropDownList runat="server" ID="ddlEstimates" AutoPostBack="false" DataTextField="Name" DataValueField="ID" AppendDataBoundItems="true">
<asp:ListItem Text="Wybierz" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvddlEstimates" runat="server" ErrorMessage="Wybierz projekt" ControlToValidate="ddlEstimates" InitialValue="Wybierz"></asp:RequiredFieldValidator>
</td>
</tr>
<tr id="idAttachmentsRow">
<td nowrap="true" valign="top" class="ms-formlabel" width="20%">
<SharePoint:FieldLabel ID="FieldLabel1" ControlMode="New" FieldName="Attachments" runat="server" />
</td>
<td valign="top" class="ms-formbody" width="80%">
<SharePoint:FormField runat="server" ID="AttachmentsField" ControlMode="New" FieldName="Attachments" />
<script language="javascript" type="text/javascript">
var elm = document.getElementById("idAttachmentsTable");
if (elm == null || elm.rows.length == 0)
document.getElementById("idAttachmentsRow").style.display = 'none';
</script>
</td>
</tr>
<table width="100%" border="0" cellspacing="0">
<tr>
<td width="99%" class="ms-toolbar" nowrap="nowrap">
<img src="/_layouts/images/blank.gif" width="1" height="18" />
</td>
<td class="ms-toolbar" nowrap="nowrap">
<asp:Button runat="server" Text="Zapisz" ID="btnSave" OnClick="btnSave_OnClick" />
</td>
<td class="ms-separator">
</td>
<td class="ms-toolbar" nowrap="nowrap" align="right">
<SharePoint:GoBackButton runat="server" ControlMode="New" ID="gobackbutton" />
</td>
</tr>
</table>
webpartpage aspx cs ( ... )
public partial class new2 : WebPartPage
{
private WebManager _wm = null;
protected override void AddedControl(System.Web.UI.Control control, int index)
{
base.AddedControl(control, index);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_wm = new WebManager(SPContext.Current.Web);
if (!Page.IsPostBack)
{
ddlProjects.DataSource = _wm.Projects.GetItems(new SPQuery()
{
Query = CAML.Where(CAML.Eq(CAML.FieldRef("ProState"), CAML.Value("Realizacja"))) + CAML.OrderBy(CAML.FieldRef("Title")),
ViewFields = CAML.FieldRef("Title") + CAML.FieldRef("ID")
});
ddlProjects.DataBind();
}
}
protected void ddlProjects_OnSelectedIndexChanged(object sender, EventArgs e)
{
int projectId = int.Parse(ddlProjects.SelectedValue);
SPListItem project = _wm.Projects.GetItemById(projectId);
ProjectNormaFiles pnf = new ProjectNormaFiles(_wm.Web, project["FolderName"].ToString());
ddlEstimates.DataSource = pnf.GetNormaFiles("Umowa", "Zatwierdzone dodatkowe").OrderBy(s=>s.Title);
ddlEstimates.DataBind();
}
protected void btnSave_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
SPListItem itm = _wm.InvestorAcceptancesFiles.AddItem();
itm["Title"] = field_Title.Value;
itm["ProjectID"] = new SPFieldLookupValue(int.Parse(ddlProjects.SelectedValue), ddlProjects.SelectedValue);
itm["NormaFileID"] = new SPFieldLookupValue(int.Parse(ddlEstimates.SelectedValue), ddlEstimates.SelectedValue);
itm.Update();
}
Response.Write("<script language='javascript'>window.frameElement.commitPopup();window.parent.location.reload();</script>");
Response.Flush();
Response.End();
}
}