locked
Get the value in Newform.aspx RRS feed

  • Question

  • Hello,

    I would like to know how to get the value of the item just before the submitting in NewForms.aspx form ?
    By example, I use this code to save and redirect to another page :

    <script runat="server" type="text/c#">
    void SaveButton(object sender, System.EventArgs e)
    {
    string vacationID = Request.QueryString["ID"];
    string redirectUrl = SPContext.Current.Web.Url.ToString() + "/Lists/vacations/TeamLeaderForm.aspx?fisrname=" + firstname;
    SPContext.GetContext(this.Context).Item.Update();
    Response.Redirect( redirectUrl );
    }
    </script>
    In this example, I want to get the firstname value.

    Thanks in advance !! :-)
    Monday, February 1, 2010 3:53 PM

Answers

  • Hello,

    I find my error !

    The good code is the following :

    string titre = Convert.ToString(SPContext.Current.ListItem["Title"]);
    Response.Write("Title: " +  titre );
    
    I got the error because my save button was in <table> with an ID....

    So my whole code :

    Create a button :

    <table cellpadding=0 cellspacing=0 width="100%">
     <tr>
     	<td> 	
    		<table width="100%">
    			<tr>
    				<td width="99%" class="ms-toolbar" nowrap=""><IMG SRC="/_layouts/images/blank.gif" width="1" height="18"/>
    				</td>
    				<td class="ms-toolbar" nowrap="">
    					<asp:Button runat="server" Text="OK" id="Save" OnClick="SaveButton" Font-Size="9pt" Width="82px" Height="22px"/>
    				</td>
    				<td class="ms-separator">&nbsp;</td>
    				<td class="ms-toolbar" nowrap="" align="right">
    					<asp:Button runat="server" Text="Cancel" id="Cancel" OnClick="CancelButton" Font-Size="9pt" Width="82px" Height="22px"/>
    				</td>
    			</tr>
    		</table> 	
     	</td>
     </tr>
    
    The function :

    void SaveButton(object sender, System.EventArgs e)
    {
    ...
    }
    Thanks to Jevgeni et Karine !! :-)
    • Marked as answer by Nekrofage Wednesday, February 3, 2010 1:20 PM
    Wednesday, February 3, 2010 1:20 PM

All replies

  • you can get it from SPContext.Current.ListItem["Your first name field title"]
    it will return you object type reference, so you should convert it into string or something else (depends on your actual type of First name field)
    Monday, February 1, 2010 4:15 PM
  • Hello,

    I use your tips like that :

    string titre = Convert.ToString(SPContext.Current.ListItem["Title"]);
    Response.Write("Title: " +  titre );
    
    But I got an error...
    Tuesday, February 2, 2010 8:14 AM
  • Any details on your error?
    Tuesday, February 2, 2010 8:19 AM
  • I put my previous code in SaveButton function.

    My error when I click on my "OK" custom button :

    An unexpected error has occurred. 
    Troubleshoot issues with Windows SharePoint Services. 
    
    Tuesday, February 2, 2010 8:44 AM
  • You can find more details on your error in SharePoint LOGS:
    "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS"

    You can also setup your web.config to display stacktrace - more details on your error directly in browser
    You can also debug your code to see more info on your error
    You can also wrap you code info try-catch block and inside catch put Reponse.Write(ex.ToString()) to display it directly in browser
    Tuesday, February 2, 2010 9:08 AM
  • To get Item Title you don't need it get from Item indexer, you can get it directly as string following way:
    SPContext.Current.ListItem.Title;
    Tuesday, February 2, 2010 9:09 AM
  • In the log, I got this message :

    02/02/2010 10:43:02.23 	w3wp.exe (0x0DD0)                       	0x0E20	Windows SharePoint Services   	
    General 8l1n High
    An SPRequest object was not disposed before the end of this thread.
    To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed. Allocation Id: {C9FDFA1E-2AA9-442D-A8D9-5B5A402A5B01} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.
    Tuesday, February 2, 2010 9:58 AM
  • I think this log entry is not about your problem, but it is about disposing SPSite and SPWeb and other disposable objects.
    if you are creating new instance of SPWeb or SPSite object, then best practice is to dispose them.
    you can use following construction to ensure this type of objects will be disposed:

    using (SPSite site = new SPSite("http://.."))
    {
        // here you are using site object
    }
    Tuesday, February 2, 2010 10:20 AM
  • And what about this code?

    <script runat="server" type="text/c#">

    void SaveButton(object sender, System.EventArgs e)

    {

      string vacationID = Request.QueryString["ID"];

                    SPListItem currentItem = SPContext.Current.Web.Lists["Vacations"].Items.GetItemById(vacationID);
                    string redirectUrl = SPContext.Current.Web.Url.ToString() + "/Lists/vacations/TeamLeaderForm.aspx?fisrname=" + currentItem["FirstName"].ToString();

      SPContext.GetContext(this.Context).Item.Update();

      Response.Redirect( redirectUrl  );

    }

    </script>
    Karine Bosch
    Tuesday, February 2, 2010 12:43 PM
  • But how to get the ID item of the NewForm.aspx form ?
    I don't want to get it by url parameter.
    Tuesday, February 2, 2010 1:16 PM
  • In fact, when you create a new item you are presented with the NewForm.aspx form. At that moment you don't have an ID yet for the new item. I think you can better develop an event receiver for the ItemAdded event. At that time you have the necessary properties set for that item.

    Karine Bosch
    Tuesday, February 2, 2010 1:32 PM
  • Hello,

    I find my error !

    The good code is the following :

    string titre = Convert.ToString(SPContext.Current.ListItem["Title"]);
    Response.Write("Title: " +  titre );
    
    I got the error because my save button was in <table> with an ID....

    So my whole code :

    Create a button :

    <table cellpadding=0 cellspacing=0 width="100%">
     <tr>
     	<td> 	
    		<table width="100%">
    			<tr>
    				<td width="99%" class="ms-toolbar" nowrap=""><IMG SRC="/_layouts/images/blank.gif" width="1" height="18"/>
    				</td>
    				<td class="ms-toolbar" nowrap="">
    					<asp:Button runat="server" Text="OK" id="Save" OnClick="SaveButton" Font-Size="9pt" Width="82px" Height="22px"/>
    				</td>
    				<td class="ms-separator">&nbsp;</td>
    				<td class="ms-toolbar" nowrap="" align="right">
    					<asp:Button runat="server" Text="Cancel" id="Cancel" OnClick="CancelButton" Font-Size="9pt" Width="82px" Height="22px"/>
    				</td>
    			</tr>
    		</table> 	
     	</td>
     </tr>
    
    The function :

    void SaveButton(object sender, System.EventArgs e)
    {
    ...
    }
    Thanks to Jevgeni et Karine !! :-)
    • Marked as answer by Nekrofage Wednesday, February 3, 2010 1:20 PM
    Wednesday, February 3, 2010 1:20 PM
  • But how to get the ID item of the NewForm.aspx form ?
    I don't want to get it by url parameter.
    I find a solution here :
    http://www.codedigest.com/Articles/Sharepoint/279_How_do_I_tell_what_the_next_list_item_ID_is.aspx

    By example, I do this :

    	SPSite vacationsSiteCollection = new SPSite(siteVacation);
    	SPWeb vacationsSite = vacationsSiteCollection.OpenWeb();
    	SPList vacationsList = vacationsSite.Lists["vacations"];
    	vacationsSite.AllowUnsafeUpdates = true;
    	SPListItem vacationsListItem = vacationsList.Items.Add();
    		        			
    	vacationsListItem["daystaken"] = daysTaken;
    	vacationsListItem["hourstaken"] = daysTaken * 8;			
    	vacationsListItem.Update();
    	
    	lastVacationRequest = vacationsList.Items[vacationsList.ItemCount - 1].ID + 1;
    

    Wednesday, February 17, 2010 12:38 PM