Solved

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

Posted on 2011-03-25
13
217 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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