locked
how to pass data from webpart to new item form. RRS feed

  • Question

  • i have a webpart on a webpart page which fetcheds the list info of the user and display in table. List name is Employee.

    the url to this webpart page is like http://sitename:1212/sitepages/test.aspx?uid=23,

    for different uid, different data will be populated in the table.

    I have a add details button on this page. on click it opens the add new item form of another list. List name is Salary.

    on this new poped up form, i want to pickup the employee name from my webpart(the table has this data).

    how can i achieve this??

    Saturday, May 28, 2011 6:30 AM

Answers

  • i havent tried your solution yet, but i found a solution, what i did is:

    1st pass whatever data you want the form to pickup in the querystring.

    open form page in edit in advance mode in SP designer.

    use ECMA object model to fetch querystring :

     var UName= getQuerystring('Name'); 
    
     if(UName!= null)
     {
      var pp = getPickerInputElement('NAME OF CONTROL');
      if(pp != null)
      {
      pp.innerHTML = UName;
      }
      }
      }     
     
     function getQuerystring(parameter)
     {
     var loc = window.location.toString();
    var params = loc.split("?");
    params =params [1].split("=");
    params =params [1].split("&");
    return params[0];
    }
    

    Tuesday, May 31, 2011 4:55 AM

All replies

  • Hi,

    You mentioned that you needed to open a pop-up form to add new item to Salary list by clicking a button in the webpart.

    One way to pass parameter from one page to another page is to attach parameter to url as query string as you did in 'http://sitename:1212/sitepages/test.aspx?uid=23'.

    So it depends on how you add click event for the button to open a form.

    e.g. Use visual webpart

    <SharePoint:ScriptLink ID="scriptLinkGridView" runat="server" LoadAfterUI="true" Localizable="false" Name="SPGridViewWP.js"/>
    
    
    <SharePoint:SPGridView runat="server" ID="gridView" AutoGenerateColumns="false" 
       DataSourceID="datasource" 
      ShowFooter="True" OnRowDataBound="gridView_RowDataBound">
      <AlternatingRowStyle CssClass="ms-alternating" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
      <asp:Button ID="btnPopUpDialog" runat="server" Text="Click me to open a pop-up dialog" />
    </ItemTemplate>
    </asp:TemplateField>
    
    <SharePoint:SPBoundField DataField="Name" HeaderText="Name" FooterText="NameFooter"></SharePoint:SPBoundField>
    </Columns>  
      <SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
    </SharePoint:SPGridView>
    <SharePoint:SPDataSource ID="datasource" runat="server">
    </SharePoint:SPDataSource>
    

    SPGridViewWP.js file:

    function ShowModalDialogForSPGridViewWP(id) {
    
     var options = {
       url: '/sites/FirstTestSite/NewDoc/Forms/AllItems.aspx?Id=' + id.toString(),
             tite: 'Edit',
             allowMaximize: false,
             showClose: true,
             width: 800,
             height: 600,
             dialogReturnValueCallback: demoCallback
           };
    
           SP.UI.ModalDialog.showModalDialog(options)
         }
    

    You need to deploy the js file above to the location 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS'.

    Code behind:

     public partial class SPGridView_VisualWP1UserControl : UserControl
      {
        protected int checkCount=0;
        protected void Page_Load(object sender, EventArgs e)
        {
          this.gridView.PagerTemplate = null;
          
          SPSite site = new SPSite("http://kfc1/sites/FirstTestSite");
            
           SPWeb web = site.RootWeb;
    
    
           SPList docList = web.GetListFromUrl("NewDoc/Forms/AllItems.aspx");
            this.datasource.List=docList;
            this.datasource.SelectCommand = "<Query><Where><IsNotNull><FieldRef Name=\"Title\" /></IsNotNull></Where></Query>";
            // this.gridView.RowDataBound += new GridViewRowEventHandler(gridView_RowDataBound);
        }
    
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          if(e.Row.RowType==DataControlRowType.DataRow)
          {
             Button btn = e.Row.Cells[0].FindControl("btnPopUpDialog") as Button;
           btn.Attributes.Add("onclick", "ShowModalDialogForSPGridViewWP(" + ((e.Row.DataItem as Microsoft.SharePoint.WebControls.SPDataSourceViewResultItem).ResultItem as SPListItem).ID.ToString() + ");return false;");
          }
          
        }
    
          
      }
    }
    
    

     

     

     

     


    Microsoft Online Community Support
    Monday, May 30, 2011 8:21 AM
  • i havent tried your solution yet, but i found a solution, what i did is:

    1st pass whatever data you want the form to pickup in the querystring.

    open form page in edit in advance mode in SP designer.

    use ECMA object model to fetch querystring :

     var UName= getQuerystring('Name'); 
    
     if(UName!= null)
     {
      var pp = getPickerInputElement('NAME OF CONTROL');
      if(pp != null)
      {
      pp.innerHTML = UName;
      }
      }
      }     
     
     function getQuerystring(parameter)
     {
     var loc = window.location.toString();
    var params = loc.split("?");
    params =params [1].split("=");
    params =params [1].split("&");
    return params[0];
    }
    

    Tuesday, May 31, 2011 4:55 AM