Sharepoint 2010列表要在Web部件中提取的列 [英] Sharepoint 2010 List Column to be fetched in Web part

查看:133
本文介绍了Sharepoint 2010列表要在Web部件中提取的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我是Sharepoint 2010开发的新手,并且要求在Visual Web部件中我有一个下拉列表,并且需要使用另一个列表的列值来填充此下拉列表。

准确地说,Dropdownlist_projectName应该从名为Project的列表的Name Column填充。我试图使用下面的代码,但我不知道我哪里出错 -

Hello,
I am new to the Sharepoint 2010 development, and have a requirement where in the Visual Web part i have a dropdown and this dropdown needs to be popluated with the Column values of another list.
Being precise, the Dropdownlist_projectName should be populated from the Name Column of a list named "Project". I was trying to work with the below piece of code but i don't know where i am going wrong-

SPQuery ratecardQuery = new SPQuery();

            ratecardQuery.ViewFields = "<FieldRef Name='Project_x0020_Name'/>";
            ratecardQuery.ViewFieldsOnly = true;
            ratecardQuery.Query = "<Where><Eq><FieldRef Name=\"Project Name\"/><Value Type=\"Text\"></Value></Eq></Where>";

            SPList resourceRateList = SPContext.Current.Web.Lists["Project"];
            SPListItemCollection item = resourceRateList.GetItems(ratecardQuery);

            DataTable tmpTable = new System.Data.DataTable();
            tmpTable = item.GetDataTable();





当我调试代码时,我看到tmpTable为null。

请帮忙。



When i debug the code, i see tmpTable coming as null.
Please help.

推荐答案

我不知道为什么社区dint给出了任何回复,但这里是解决方案 -

I dont know why the community dint gave any response but here is the solution-
SPQuery query = new SPQuery();
               query.ViewFieldsOnly = true;
               query.ViewFields = "<fieldref name="Project_x0020_Name" />";
               using (SPSite site = new SPSite(SPContext.Current.Web.Url))
               {
                   using (SPWeb web = site.OpenWeb())
                   {
                       SPList list = web.Lists["Project"];
                       if (list != null)
                       {
                           DataTable dt = new DataTable();
                           dt = list.GetItems(query).GetDataTable();
                           DropDownList1.DataSource = dt;
                           DropDownList1.DataValueField = "Project_x0020_Name";
                           DropDownList1.DataTextField = "Project_x0020_Name";
                           DropDownList1.DataBind();
                           DropDownList1.Items.Insert(0, "--Select Project Name--");

                       }
                   }

               }


这篇关于Sharepoint 2010列表要在Web部件中提取的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆