如果在我的列表中添加两个项目,我只能在详细信息视图中显示列表中的第一个更新项目,所以我的问题是,如果我在列表中有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 detailview代码:
<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>
答案 0 :(得分: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;
获得您的身份证后,请使用以下条款:
query.Query = <Where>
<Eq>
<FieldRef Name='ID' />
<Value Type='Text'>ID</Value>
</Eq>
</Where>";
编辑:
要从SharePoint获取字段,请在其中添加“UniqueId”字段 query.VieweFields。这将从Sharepoint获取并生成 DataTable:
query.ViewFields = "<FieldRef Name=\"UniqueId\"/>";
在按钮事件方法中获取此代码的值 做以下事情:
SPFieldLookupValue f = new SPFieldLookupValue(button.CommandArgument); string itemID = f.LookupValue;
希望它有所帮助,
安德鲁