Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-25
13
Medium Priority
?
227 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 1000 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
Technology Partners: 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 1000 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

Technology Partners: 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!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

718 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