Solved

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

Posted on 2011-02-26
4
669 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
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

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 38
Where did System.Data.Objects go? 2 40
C# Desktop Application 3 54
Input parameteres to DragOver 2 19
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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