Solved

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

Posted on 2011-03-25
13
211 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now