Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scanning dentists xray (the small ones) 3 53
What .NET URL re-routing tool did I use? 2 56
SQL Exceptions 3 43
Best resource to learn entity framework 2 33
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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