0

如果在我的列表中添加这两个项目,我只能在详细信息视图中显示列表中的第一个更新项目,所以我的问题是,如果我在列表中有 10 个项目,它应该在所有项目上显示标题,然后当我点击我希望每个超链接按钮从 id 显示有关该标题的信息

这是我想做的图片链接。 http://i.stack.imgur.com/ZhJmc.jpg

protected void ButtonChoose_Click(object sender, EventArgs e)
{
    try
    {

        if (DropDownListNewsFeed.SelectedItem.Value == "All")
        {
            SPWeb web = SPContext.Current.Web;

            SPSiteDataQuery query = new SPSiteDataQuery();

            query.Lists = "<Lists>" +
            "<List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " />" +
            "<List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " />" +
            "<List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " />" +
            "</Lists>";

            query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";
            query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";

            query.RowLimit = 10;

            DataTable dt = web.GetSiteData(query);
            DataView dv = new DataView(dt);

            GridViewNewsFeed.DataSource = dv;
            GridViewNewsFeed.DataBind();
        }
        else if(DropDownListNewsFeed.SelectedItem.Value == "Staff News")
        {
            SPWeb web = SPContext.Current.Web;

            SPSiteDataQuery query = new SPSiteDataQuery();

            query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " /></Lists>";


            query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";
            query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";

            query.RowLimit = 10;

            DataTable dt = web.GetSiteData(query);
            DataView dv = new DataView(dt);

            GridViewNewsFeed.DataSource = dv;
            GridViewNewsFeed.DataBind();
        }
        else if (DropDownListNewsFeed.SelectedItem.Value == "Management News")
        {
            SPWeb web = SPContext.Current.Web;

            SPSiteDataQuery query = new SPSiteDataQuery();

            query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " /></Lists>";


            query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";
            query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";

            query.RowLimit = 10;

            DataTable dt = web.GetSiteData(query);
            DataView dv = new DataView(dt);



            GridViewNewsFeed.DataSource = dv;
            GridViewNewsFeed.DataBind();
        }
        else if (DropDownListNewsFeed.SelectedItem.Value == "Company News")
        {
            SPWeb web = SPContext.Current.Web;

            SPSiteDataQuery query = new SPSiteDataQuery();

            query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " /></Lists>";


            query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";
            query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";

            query.RowLimit = 10;

            DataTable dt = web.GetSiteData(query);
            DataView dv = new DataView(dt);

            GridViewNewsFeed.DataSource = dv;
            GridViewNewsFeed.DataBind();
        }

    }
    catch (Exception x)
    {
        LabelException.Text = x.Message;
    }
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    try
    {
        SPWeb web = SPContext.Current.Web;

        SPSiteDataQuery query = new SPSiteDataQuery();

        query.Lists = "<Lists>" +
                      "<List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " />" +
                      "<List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " />" +
                      "<List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " />" +
                      "</Lists>";


        query.ViewFields = "<FieldRef Name=\"Title\" />" +
                        "<FieldRef Name=\"Authors\" />" +
                        "<FieldRef Name=\"Link\" />" +
                        "<FieldRef Name=\"Contents\" />" +
                        "<FieldRef Name=\"_Category\" />" +
                         "<FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";

        //query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
        query.Webs = "<Webs Scope=\"SiteCollection\" />";

        query.RowLimit = 10;

        DataTable dt = web.GetSiteData(query);
        DataView dv = new DataView(dt);

        DetailsViewShowNews.DataSource = dv;
        DetailsViewShowNews.DataBind();
    }
    catch (Exception x)
    {

        LabelException.Text = x.Message;
    }
}

这是我的 asp gridview 代码:

<asp:GridView AutoGenerateColumns="False" ID="GridViewNewsFeed" runat="server" BackColor="White" 
    BorderColor="#DEDFDE" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" 
    EnableModelValidation="True" GridLines="Vertical" ForeColor="Black" >

    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />

    <AlternatingRowStyle BackColor="White" />            
            <Columns>
                <asp:TemplateField HeaderText="Title">
                  <ItemTemplate>                        
                      <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" runat="server" Text='<%# Bind("Title") %>'></asp:LinkButton>                       
                  </ItemTemplate>
                </asp:TemplateField> 

                <asp:templatefield headertext="Date">
                    <itemtemplate>
                            <asp:label id="lblDate" runat="server" text='<%# DateTime.Parse(Eval("Date1").ToString()).ToString("d") %>' />
                    </itemtemplate>
                </asp:templatefield>                                                                                                
            </Columns>
</asp:GridView>

这是我的 asp detailsview 代码:

<asp:DetailsView AutoGenerateRows="False" ID="DetailsViewShowNews" runat="server" 
    BackColor="White" BorderColor="#336666" BorderWidth="1px" 
    CellPadding="4" EnableModelValidation="True" GridLines="Horizontal" 
    Height="226px" Width="301px" BorderStyle="Solid"  >
    <EditRowStyle BackColor="#339966" ForeColor="White" Font-Bold="True" />
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#336666" ForeColor="White" 
        HorizontalAlign="Center" />


     <Fields>
        <asp:TemplateField >                      
            <ItemTemplate>
                <asp:Label ID="Label12" runat="server" Text='<%#Eval("Title") %>' Font-Size="Large" Font-Bold="True"></asp:Label>   
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>                      
            <ItemTemplate>
                <asp:TextBox ID="TextBox22" runat="server" Text='<%#Eval("Contents") %>' TextMode="MultiLine" Height="100%" BorderStyle="None" ReadOnly="True" Width="250px" style="overflow:auto;"></asp:TextBox>   
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>                      
            <ItemTemplate>
                <asp:Label ID="Label72" runat="server" Text='<%#Eval("_Category") %>' ></asp:Label>   
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label52" runat="server" Text='<%#Eval("Authors") %>' ></asp:Label>   
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>                      
            <ItemTemplate>
                <asp:Label ID="Label32" runat="server" Text='<%#DateTime.Parse(Eval("Date1").ToString()).ToString("d") %>' ></asp:Label>   
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>                      
            <ItemTemplate>                   
                <a href="<%#Eval("Link") %>"><asp:Label ID="Label42" runat="server" Text='<%#Eval("Link") %>' ></asp:Label>   </a>
            </ItemTemplate>
        </asp:TemplateField>               

        <asp:TemplateField>                      
            <ItemTemplate>                   
                <asp:LinkButton ID="LinkButtonComment" OnClick="LinkButtonComment_Click" runat="server">Leave a comment</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>          
    </Fields>

    <RowStyle BackColor="White" ForeColor="#333333" />

</asp:DetailsView>
4

1 回答 1

1

首先,您可以减少代码并使其更通用,如下所示:

            try
            {
                SPWeb web = SPContext.Current.Web;
                SPSiteDataQuery query = new SPSiteDataQuery();

                switch (DropDownListNewsFeed.SelectedItem.Value)
                {
                    case "ALL":
                        query.Lists = "<Lists>" +
                        "<List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " />" +
                        "<List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " />" +
                        "<List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " />" +
                        "</Lists>";
                        break;
                    case "Staff News":
                        query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Staff News").ID.ToString() + " /></Lists>";
                        break;
                    case "Management News":
                        query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Management News").ID.ToString() + " /></Lists>";
                        break;
                    case "Company News":
                        query.Lists = "<Lists><List ID=" + web.Lists.TryGetList("Company News").ID.ToString() + " /></Lists>";
                        break;
                }

                query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Date1\" Nullable=\"TRUE\"/>";
                query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>";
                query.Webs = "<Webs Scope=\"SiteCollection\" />";

                query.RowLimit = 10;

                DataTable dt = web.GetSiteData(query);
                DataView dv = new DataView(dt);

                GridViewNewsFeed.DataSource = dv;
                GridViewNewsFeed.DataBind();
            }
            catch (Exception x)
            {
                LabelException.Text = x.Message;
            }

关于你的问题:

尝试将项目 ID(最好是通过 UniqueId)作为链接按钮上的“CommandArgument”传递。要指定这一点,您只需将项目 ID 字段绑定到 CommandArgument 属性:

<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" CommandArgument="<%# Bind("ItemId") %>" runat="server" Text='<%# Bind("Title") %>'></asp:LinkButton>

在单击按钮时背后的代码上,您可以使用下一行代码获取它:

LinkButton button = (LinkButton) sender;
string itemID = button.CommandArgument;

获得 ID 后,只需使用以下子句获取它:

query.Query =    <Where>
      <Eq>
         <FieldRef Name='ID' />
         <Value Type='Text'>ID</Value>
      </Eq>
   </Where>";

编辑:

要从 SharePoint 中获取字段,请在 query.VieweFields 中添加“UniqueId”字段。这将从 Sharepoint 获取并在 DataTable 中生成:

query.ViewFields = "<FieldRef Name=\"UniqueId\"/>";

要在按钮事件方法中的代码后面获取此字段的值,请执行以下操作:

SPFieldLookupValue f = new SPFieldLookupValue(button.CommandArgument);
string itemID = f.LookupValue;

希望能帮助到你,

安德鲁

于 2013-02-07T07:30:16.847 回答