1

What I'm trying to accomplish is when I click a button in my gridview, the values of that particular row must be displayed in textlabels lblID, lblName, ...
I just don't have a clue how to do it.
This is the code of my sqldatasource:

<asp:SqlDataSource ID="srcProd" runat="server"  
ConnectionString="<%$ ConnectionStrings:ConnDB %>"   
ProviderName="<%$ ConnectionStrings:ConnDB.ProviderName %>"   
SelectCommand="SELECT p.tProdID, p.tProdImage, p.tProdBeschrijving, m.tMerkNaam, m.tMerkImage,   p.tProdVerkoop FROM tblProducten AS p INNER JOIN tblMerken AS m ON p.tMerkID = m.tMerkID ORDER BY m.tMerkNaam">  
</asp:SqlDataSource>

This is a screenshot of the gridview I'm talking about. Here is a gridview screenshot

When I press one of the buttons I would like e.g. the parameter ID displayed in lblID.

All help, hints and tips are appreciated.

2 Answers 2

4

Assign a CommandName (which is like "ShowDetails" or so) and a CommandArgument (which is the ID of the record) to the button. A click on the button will now trigger the grid's RowCommand event, passing you both CommandName and CommandArgument in e. Then fetch the record from your DB and populate your controls:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button runat="server" Text="Foo2" CommandArgument='<%# Eval("RowID") %>'
            CommandName="ShowDetails" />
    </ItemTemplate>
</asp:TemplateField>


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Debug.WriteLine("Command Handler");
}
3
  • Idd, that's best practise i guess. At some point, this method did not work for me, dont know why tho. Btw, can u specify multiple arguments here ? (and not as a ";" seperated string or so) Commented May 2, 2012 at 13:56
  • As far as I know, you cannot specify multiple arguments. I am annoyed at this pretty frequently too... Commented May 2, 2012 at 15:10
  • You can, using a ";" seperated string to pass multiple values. It's dirty in my opinion, but its possible. Commented May 3, 2012 at 6:53
1

SO basicly this can be done on two ways:

  1. Add a server-side click handler behind the button and update the label inside an UpdatePanel (this case we do not need a postback)
  2. Add some javascript on the button's "OnClientClick" event to set this label... This method can be set in the GridView's bind method.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.