Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
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("StaffNews").ID.ToString() + " />" +
                          "<List ID=" + web.Lists.TryGetList("CompanyNews").ID.ToString() + " />" +
                          "<List ID=" + web.Lists.TryGetList("ManagementNews").ID.ToString() + " />" +
                          "</Lists>";

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

            query.Webs = "<Webs Scope=\"SiteCollection\" />";

            query.RowLimit = 5;

            DataTable dt = web.GetSiteData(query);

            DataView dv = new DataView(dt);

            DetailsView1.DataSource = dv;
            DetailsView1.DataBind();
        }
        catch (Exception x)
        {
            Label1.Text = x.Message;
        }
    }

enter image description here I am displaying the title and date in a gridview when i click the linkbutton i want it to show that information in a detailview. i am using a visual webpart.

share|improve this question
add comment

2 Answers

This is what you do:

<asp:TemplateField>
 <ItemTemplate>
<asp:LinkButton ID="LinkButton1" onclick="LinkButton1_Click" CommandArgument='<%# Eval("ID") %>' runat="server" Text='<%# Bind("Title") %>'></asp:LinkButton>
 </ItemTemplate>
</asp:TemplateField>       

Then, in your code behind:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        var listItemID = btn.CommandArgument;
    }

Hope this helps

share|improve this answer
    
I have updated in the top. –  Martin Feb 5 '13 at 20:02
    
So where I've got the CommandArgument= bla bla above, just add ID Which is the List Item id. CommandArgument='<%# Eval("ID") %>' –  Fox Feb 6 '13 at 8:10
    
ok! should i add CommandArgument='<%# Eval("ID") %>' in the hyperlinkbutton? –  Martin Feb 6 '13 at 9:19
    
Yes. And not a HyperLinkButton, but a LinkButton.. Like so: <asp:LinkButton Text="text" runat="server" CommandArgument='<%# Eval("ID") %>' /> –  Fox Feb 6 '13 at 9:51
    
like this <asp:LinkButton ID="LinkButton1" onclick="LinkButton1_Click" CommandArgument='<%# Eval("ID") %>' runat="server" Text='<%# Bind("Title") %>'></asp:LinkButton> what more should i write –  Martin Feb 6 '13 at 9:53
show 2 more comments

What if you put your UniqueId to CommandeArgument of the buton and get it back when link button is clicked? Once you get your value you just split this using SPFieldLookupValue and get your ID to filter:

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

in the click event:

LinkButton lb = (LinkButton) sender;
SPFieldLookupValue f = new SPFieldLookupValue(lb.CommandArgument);
string itemID = f.LookupValue;

Do not forget to make a field reference to get UniqueId using CAML:

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

Hope it helps,

Andrew

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.