Solved

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

Posted on 2011-02-26
4
671 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
[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
  • 3
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 34990576
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
ID: 34993903
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
ID: 35003540
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
ID: 35042814
Others would not have been able to resolve the issue as the load statements were not posted.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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