none
How to display a default value in dropdown list in Data form webpart

    問題

  • Hi I have a dataForm webpart in that I inserted a datasource control for one sharepoint list and also inserted a ASP dropdown list . Next i configured that datasource control to dropdown list . Now am getting the values from sharepoint list to Dropdownlist in my Dataformwebpart. By selecting any value in dropdown list the data is populated . In dataform webpart i used some filter and sorting to display the data for particular city. But how to set the default value in that drop down list.

    Ex: In my share point list  i have data like Chennai, Bangalore, Kolkata, Pune

      Now in my dropdown list the 4 values are comining and data for particular city. But if the user is belong to Pune the First value  in dropdown list must be pune and the data of pune should be display !!

     Thanks in Advance ..
    2012年2月15日 上午 07:13

解答

  • Hi sandy,

    First we should make the data form web part can be filtered by the dropdown list in SharePoint Designer. Please follow the steps:

    1. You have combined the dropdown list to list2, so just make sure the Enable AutoPostBack is checked in Common DropDownList Tasks pane.
    2. In Common Data View Tasks pane of data form web part, select Parameters. In Data View Parameters page, click New Parameter > type in a parameter name under Name > select Control in Parameter Source > select the dropdown list in Control ID. Click OK.
    3. Select Filter in Common Data View Tasks pane, set the City equals the parameter you created in step 2. Click OK.
    4. Now save change in Designer and preview, you can find the data form web part changes along with the change in dropdown list.

    Now we need to get the current login user’s name and find the city relevant in list1, then set the city to the dropdown list selected value. But first, we need insert a button beside the dropdown list in Designer. Then follow this:

    Add a Content Editor Web Part to the page, copy the code below into Source Editor of it.

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>

    <script type="text/javascript" src="http://come:100/sites/collection1/sub1/Shared Documents/jquery.SPServices-0.6.2.min.js"></script>

     

    <script type="text/javascript">

         $(document).ready(function(){

        var city1;

       var CurrUser=$().SPServices.SPGetCurrentUser({

         fieldName: "Name",

          debug: false

        });

     

    $().SPServices({

             operation: "GetListItems",

             async: false,

             listName: "list1",

          completefunc: function(xData,Status){

            $(xData.responseXML).find("[nodeName='z:row']").each(function() {

             var user=$(this).attr('ows_Name');

             if(user==CurrUser)

             {

                city1=$(this).attr('ows_city');

                return false;

             }

           });

         }

      });

     

      $('#ctl00_PlaceHolderMain_DropDownList1').children().each(function(){

        var option1=$(this).text();

        if(option1==city1)

        {

          var test1="option[text="+option1+"]";

         $('#ctl00_PlaceHolderMain_DropDownList1').find(test1).attr("selected","selected");

        }

      });

     

      $('#ctl00_PlaceHolderMain_Button1').onclick=(function(){

         __doPostBack('ctl00_PlaceHolderMain_DropDownList1','');

      });

    });

    </script>

    Note: 1. you can download jquery.SPServices-0.6.2.min.js here: http://cdiscportal.digitalinfuzion.com/CT/Scripts/Forms/DispForm.aspx?ID=1, then upload it to your site and change the path above. 2. The list1, Name, city in bold are the list or column name according to your description. 3. The ctl00_PlaceHolderMain_DropDownList1 is the id of the dropdown list, and the ctl00_PlaceHolderMain_Button1 is the id of the button we insert, please find the two id in your own page. 4. The user name in list1 must be the same as the user login name.

    After all finished above, open your site page you will find the dropdown list is populated with the login user’s city, then press the button besides to do a PostBack, the data form web part will display the data you want.

    Best regards,
    Emir


    Emir Liu

    TechNet Community Support

    2012年2月16日 上午 10:19

所有回覆

  • Hi Sandy735,

    We can achieve this via JavaScript. Could you describe more details about what and where you want to display the data of Pune for example? It’s related to code.

    Thanks.


    Come on!

    2012年2月15日 上午 10:38
  •  sharepoint list1:      city    Name     No

                                     pune   san       983  

                                     Kol      san2     456

                                     pune    san3    123

    Sharepoint list2:       city

                                      pune

                                     chennai

                                      kolkata


    Am binding the city values (List 2 values ) in dropdown  list in dataform webpart  through sharepoint designer .In dropdown list  am getting list 2 values chennai, pune ,kolkata . but Suppose the user belongs to pune city na while loading the page first time the drop down list  shows pune in dropdown list and data(List 1 data ) should be display .

    Suppose if i belongs to pune, by default value in dropdown list be pune  ,in the view it should be displayed below  

    pune   san       983

    pune    san3    123

    Thanks ,

    sandy

    • 已編輯 Sandy735 2012年2月15日 上午 11:01
    2012年2月15日 上午 10:57
  • Hi sandy,

    First we should make the data form web part can be filtered by the dropdown list in SharePoint Designer. Please follow the steps:

    1. You have combined the dropdown list to list2, so just make sure the Enable AutoPostBack is checked in Common DropDownList Tasks pane.
    2. In Common Data View Tasks pane of data form web part, select Parameters. In Data View Parameters page, click New Parameter > type in a parameter name under Name > select Control in Parameter Source > select the dropdown list in Control ID. Click OK.
    3. Select Filter in Common Data View Tasks pane, set the City equals the parameter you created in step 2. Click OK.
    4. Now save change in Designer and preview, you can find the data form web part changes along with the change in dropdown list.

    Now we need to get the current login user’s name and find the city relevant in list1, then set the city to the dropdown list selected value. But first, we need insert a button beside the dropdown list in Designer. Then follow this:

    Add a Content Editor Web Part to the page, copy the code below into Source Editor of it.

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>

    <script type="text/javascript" src="http://come:100/sites/collection1/sub1/Shared Documents/jquery.SPServices-0.6.2.min.js"></script>

     

    <script type="text/javascript">

         $(document).ready(function(){

        var city1;

       var CurrUser=$().SPServices.SPGetCurrentUser({

         fieldName: "Name",

          debug: false

        });

     

    $().SPServices({

             operation: "GetListItems",

             async: false,

             listName: "list1",

          completefunc: function(xData,Status){

            $(xData.responseXML).find("[nodeName='z:row']").each(function() {

             var user=$(this).attr('ows_Name');

             if(user==CurrUser)

             {

                city1=$(this).attr('ows_city');

                return false;

             }

           });

         }

      });

     

      $('#ctl00_PlaceHolderMain_DropDownList1').children().each(function(){

        var option1=$(this).text();

        if(option1==city1)

        {

          var test1="option[text="+option1+"]";

         $('#ctl00_PlaceHolderMain_DropDownList1').find(test1).attr("selected","selected");

        }

      });

     

      $('#ctl00_PlaceHolderMain_Button1').onclick=(function(){

         __doPostBack('ctl00_PlaceHolderMain_DropDownList1','');

      });

    });

    </script>

    Note: 1. you can download jquery.SPServices-0.6.2.min.js here: http://cdiscportal.digitalinfuzion.com/CT/Scripts/Forms/DispForm.aspx?ID=1, then upload it to your site and change the path above. 2. The list1, Name, city in bold are the list or column name according to your description. 3. The ctl00_PlaceHolderMain_DropDownList1 is the id of the dropdown list, and the ctl00_PlaceHolderMain_Button1 is the id of the button we insert, please find the two id in your own page. 4. The user name in list1 must be the same as the user login name.

    After all finished above, open your site page you will find the dropdown list is populated with the login user’s city, then press the button besides to do a PostBack, the data form web part will display the data you want.

    Best regards,
    Emir


    Emir Liu

    TechNet Community Support

    2012年2月16日 上午 10:19