Solved

C# How to get a value from an unbound control in a gridview

Posted on 2011-02-26
4
665 Views
Last Modified: 2012-05-11
From some research, I've learned that the FindControl method will not work on unbound controls (it just pulls whatever the server thinks might be in the box as the page was loaded).  I do not wish to change the structure of this gridview.  I populate it with data from some tables and TextBoxes which are added via a Template Field.

In short, I need to get the values entered by the user in these text boxes.  What is the best method to do that?


foreach (GridViewRow row in GridView1.Rows)
        {
            Quantity = 0; SerialNumber = "";
            try { Quantity = Convert.ToInt32(((TextBox)row.FindControl("QuantityTBox")).Text); } catch { }
            try { SerialNumber = ((TextBox)row.FindControl("SerialTBox")).Text; } catch { }
        }

Open in new window

0
Comment
Question by:dparkes
  • 3
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
can u post the asp:TemplateField  code?

check the exaple here:
http://www.eggheadcafe.com/community/aspnet/17/10239850/how-to-compare-two--templete-field-value-of-grid-view.aspx

you can use row.FindControl("ContorlName") to get the control object.
0
 
LVL 1

Author Comment

by:dparkes
Comment Utility
The following is the entire set of code for the GridView.  The Gridview is not bound to a database.  Rather, various values are extracted from multiple tables and then assembolled into yet another table (dynamically created).  This third dynamically created datatable is then bound to the GridView.

When I step through the code in debug mode, the value is what the system was initialized with as opposed to what the user entered. Quantity of 5 Not Captured as shown in debug mode above
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
    CellPadding="3" ForeColor="Black" GridLines="None" HorizontalAlign="Center" 
    TabIndex="4" Width="100%" onrowupdating="GridView1_RowUpdating">
    <RowStyle Font-Size="Small" HorizontalAlign="Left" />
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="MfgNumber" HeaderText="MFG #">
        <HeaderStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:BoundField DataField="Name" HeaderText="Product" />
        <asp:BoundField DataField="ID" HeaderText="ID" ShowHeader="False">
        <HeaderStyle ForeColor="Black" Width="1px" />
        <ItemStyle ForeColor="White" Width="1px" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="QuantityTBox" runat="server" Height="15px" Text='<%# Bind("Quantity") %>' Width="38px"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Serial Number">
            <ItemTemplate>
                <asp:TextBox ID="SerialTBox" runat="server" Height="15px" Text='<%# Bind("SerialNumber") %>' Width="150px"></asp:TextBox>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" />
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" Font-Size="Medium" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="White" Font-Bold="True" Font-Size="Small" 
        ForeColor="White" HorizontalAlign="Left" />
    <HeaderStyle BackColor="Black" Font-Bold="True" Font-Size="Small" 
        ForeColor="White" HorizontalAlign="Left" />
    <EditRowStyle Font-Size="Small" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

Open in new window

0
 
LVL 1

Accepted Solution

by:
dparkes earned 0 total points
Comment Utility
I found my error ... a simple mistake that for some reason escaped me.  I forgot to put the load gridview methods inside an !IsPostBack if/then clause.  So the page was reloadeding on postback.
0
 
LVL 1

Author Closing Comment

by:dparkes
Comment Utility
Others would not have been able to resolve the issue as the load statements were not posted.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

728 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

10 Experts available now in Live!

Get 1:1 Help Now