Solved

ASP.NET GridView control - Using data in a template column

Posted on 2011-03-25
13
216 Views
Last Modified: 2012-06-21
I've got a GridView control bound to an SqlDataSource control.  In the Gridview I've got a TemplateField with a LinkButton control in it.  I'm trying to create an OnClientClick event for that LinkButton control that will utilize the ID of the data row bound to the GridView row.  I've seen this done before, I just can't seem to find any references on how this is done.  I don't care if it gets done in the code behind in the OnRowDataBound event or if it happens in the markup with a server side include statement <% %>.  I would be interested in knowing how to do either one effectively though.

Thanks in advance.
0
Comment
Question by:JosephEricDavis
  • 5
  • 4
  • 4
13 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 35215628
It should be something along the lines of:
<asp:LinkButton ID="Link1" runat="server" OnClientClick='doSomething(<%# Eval("YourIDColumn") %>);' />

Open in new window

0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35215667
Yep, that's what I thought too, but when I do that it's throwing me an error saying that the server tag is not well formed.  Here is my control below.
<asp:GridView runat="server" ID="gvPurchaseOrders" DataSourceID="sdsPurchaseOrders" AutoGenerateColumns="false" SkinID="StandardGridView" OnRowDataBound="gvPurchaseOrders_OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="PurchaseOrderName" HeaderText="PO Name" ItemStyle-Width="120" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderNumber" HeaderText="PO Number" ItemStyle-Width="100" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderValue" HeaderText="PO Value" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="AgreementNumber"  HeaderText="Agreement #" ItemStyle-Width="90" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderCategoryName" HeaderText="Category" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderTypeName" HeaderText="Type" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderScope" HeaderText="PO Scope" ItemStyle-Width="180" ReadOnly="true" />
        <asp:TemplateField  ItemStyle-Width="150">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lbEditPurchaseOrder" PostBackUrl="~/PurchaseOrderDetails.aspx" Text="Edit" OnClientClick="setEditID('<%# Eval("PurchaseOrderID") %>');" /> 
                <a href="#">Delete</a>
                <a href="#">View Pay Apps</a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35215700
The quotes are the problem, try it as:
OnClientClick='setEditID(\'<%# Eval("PurchaseOrderID") %>\');'

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35215728
Or, failing that:
OnClientClick='<%# Eval("PurchaseOrderID", "setEditID(\'{0}\');" %>'

Open in new window

0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35216060
Still getting the same message... The server tag is not well formed.
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35216068
How can this be done from the server side?
0
 
LVL 4

Expert Comment

by:zylver
ID: 35216998
to avoid all that stuff you can simply use the action buttons that the control can handle, just in the grid view activate a property that says: "AutoGenerateDeleteButton". this can be handle in various ways, one of the ways is to specify a delete statement to the data source. and ASP.NET engine will do the work for you
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35217202
No it won't. The question is referring to a client-side event on the control. The AutoGenerateDelete option will create a button that triggers a server-side event.

There is a sample of setting the OnClientClick from the RowDataBound event here:

      http://forums.asp.net/t/1059814.aspx

Haven't got access to Visual Studio at the moment so can't knock together a more precise sample at the moment.
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35217361
Ok... I think I'm going to attack this in a little bit different way.

Instead of trying to set the OnClientClick event I now want to use the GridViewCommand event.  The markup I have posted below works except for the fact that instead of passing a command argument of the actual 'PurchaseOrderID' it is passing the index of the GridView.  How can I configure this thing so it passes the ID of the data bound to the GridViewRow?
<asp:GridView runat="server" ID="gvPurchaseOrders" DataSourceID="sdsPurchaseOrders" AutoGenerateColumns="false" 
    SkinID="StandardGridView" OnRowDataBound="gvPurchaseOrders_OnRowDataBound" OnRowCommand="gvPurchaseOrders_OnRowCommand"
    DataKeyNames="PurchaseOrderID">
    <Columns>
        <asp:BoundField DataField="PurchaseOrderName" HeaderText="PO Name" ItemStyle-Width="120" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderNumber" HeaderText="PO Number" ItemStyle-Width="100" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderValue" HeaderText="PO Value" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="AgreementNumber"  HeaderText="Agreement #" ItemStyle-Width="90" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderCategoryName" HeaderText="Category" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderTypeName" HeaderText="Type" ItemStyle-Width="80" ReadOnly="true" />
        <asp:BoundField DataField="PurchaseOrderScope" HeaderText="PO Scope" ItemStyle-Width="180" ReadOnly="true" />
        <asp:TemplateField  ItemStyle-Width="150">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lbEditPurchaseOrder" Text="Edit" CommandName="Edit" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" />
                <a href="#">Delete</a>
                <a href="#">View Pay Apps</a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window

0
 
LVL 4

Expert Comment

by:zylver
ID: 35217396
in an eval like the first response:

<%# Eval("YourIDColumn") %>

Open in new window


<asp:LinkButton runat="server" ID="lbEditPurchaseOrder" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("YourIDColumn") %>' />

Open in new window

0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 35217428
Well for some reason when I try to use code like that I'm getting an error saying my server tag is not well formed.  Am I missing something on the page that would allow that to be used properly?
<asp:GridView runat="server" ID="gvPurchaseOrders" DataSourceID="sdsPurchaseOrders" AutoGenerateColumns="false" 
        SkinID="StandardGridView" OnRowDataBound="gvPurchaseOrders_OnRowDataBound" OnRowCommand="gvPurchaseOrders_OnRowCommand"
        DataKeyNames="PurchaseOrderID">
        <Columns>
            <asp:BoundField DataField="PurchaseOrderName" HeaderText="PO Name" ItemStyle-Width="120" ReadOnly="true" />
            <asp:BoundField DataField="PurchaseOrderNumber" HeaderText="PO Number" ItemStyle-Width="100" ReadOnly="true" />
            <asp:BoundField DataField="PurchaseOrderValue" HeaderText="PO Value" ItemStyle-Width="80" ReadOnly="true" />
            <asp:BoundField DataField="AgreementNumber"  HeaderText="Agreement #" ItemStyle-Width="90" ReadOnly="true" />
            <asp:BoundField DataField="PurchaseOrderCategoryName" HeaderText="Category" ItemStyle-Width="80" ReadOnly="true" />
            <asp:BoundField DataField="PurchaseOrderTypeName" HeaderText="Type" ItemStyle-Width="80" ReadOnly="true" />
            <asp:BoundField DataField="PurchaseOrderScope" HeaderText="PO Scope" ItemStyle-Width="180" ReadOnly="true" />
            <asp:TemplateField  ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lbEditPurchaseOrder" Text="Edit" CommandName="Edit" CommandArgument="<%# Eval("PurchaseOrderID") %>" />
                    <a href="#">Delete</a>
                    <a href="#">View Pay Apps</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window

0
 
LVL 4

Assisted Solution

by:zylver
zylver earned 250 total points
ID: 35217713
change the line:



<asp:LinkButton runat="server" ID="lbEditPurchaseOrder" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("YourIDColumn") %>' />

Open in new window

0
 
LVL 4

Expert Comment

by:zylver
ID: 35217716
its just the quotes
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question