Gridview in asp.net and template field

I have the below code, works fine on non-template field.

Label1.Text = GridView1.SelectedRow.Cells(4).Text
Response.Redirect("BOL.aspx?Name=" + Label1.Text)

But when I convert to a template field it fails.  How can I get the value from a cell in a row using the template column?

Thanks
LVL 2
bergertimeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
Hi bergertime,
Try this

//assume your template field look like this...
     <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

//selected row by template field  
Label1.Text = TryCast(GridView1.SelectedRow.FindControl("lblCountry"), Label).Text

Open in new window

0
bergertimeAuthor Commented:
I want the user to click the linkbutton2, he gets the value of the databound field 'ID'
 <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="Select" OnClick="LinkButton2_Click" Text="Reverse"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Id" HeaderText="Order#" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                        <asp:BoundField DataField="AFCno" HeaderText="AFC#" SortExpression="AFCno" />
                        <asp:BoundField DataField="Pieces" HeaderText="Pieces" SortExpression="Pieces" />
                        <asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />

Open in new window

0
David H.H.LeeCommented:
You can bind the value into CommandArgument and get the value from it afterward.
eg:
<asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="Select" OnClick="LinkButton2_Click" Text="Reverse" CommandArgument='<%# Eval("Id") %>' ></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>

//inside LinkButton2_Click function, find the selected row by template field, get your ID below using CommandArgument's attribute  
Label1.Text = TryCast(GridView1.SelectedRow.FindControl("LinkButton2"), linkbutton).CommandArgument

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bergertimeAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.