Solved

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

Posted on 2011-03-25
13
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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