Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control

Hi all,

I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:



asp:GridView ID="GridEditSettlement" runat="server" AutoGenerateColumns="False" BackColor="Navy"

BorderColor="IndianRed" BorderStyle="Solid" Font-Names="Verdana" Font-Size="X-Small" DataSourceID="SqlDataSource_grid" AllowPaging="True" AllowSorting="True" ForeColor="White" DataKeyNames="legid">


<asp:CommandField ShowEditButton="True" CancelImageUrl="~/App_Graphics/quit.gif" CancelText="" EditImageUrl="~/App_Graphics/EditGrid.GIF" EditText="" UpdateImageUrl="~/App_Graphics/save.gif" UpdateText="" ButtonType="Image" />

<asp:BoundField DataField="StartDate" HeaderText="Start Date" ReadOnly="True" />

<asp:BoundField DataField="EndDate" HeaderText="End Date" ReadOnly="True" />

<asp:BoundField DataField="CounterpartDealRef" HeaderText="CP Deal Ref" ReadOnly="True" />


<asp:TemplateField HeaderText="Preliminary Settlement Price" ><ItemTemplate>

<asp:Label ID=lblPreliminary runat=server Text='<%# Bind("PrimarySettlementPrice") %>' />



<asp:TextBox runat="server" ID=txtPrimaryPrice Text='<%# Bind("PrimarySettlementPrice") %>'></asp:TextBox>




<asp:TemplateField HeaderText="Agreed Settlement Price"><ItemTemplate>

<asp:Label ID=lblAgreed runat=server Text='<%# Bind("AgreedSettlementPrice") %>' />



<asp:TextBox runat="server" ID=txtAgreedPrice Text='<%# Bind("AgreedSettlementPrice") %>'></asp:TextBox>





<asp:BoundField DataField="Volume" HeaderText="Volume" ReadOnly="True" />

<asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" />

<asp:BoundField DataField="TotalVolume" HeaderText="Total Volume" ReadOnly="True" />

<asp:BoundField DataField="InstrumentName" HeaderText="Instrument" ReadOnly="True" />

<asp:BoundField DataField="NominalValue" HeaderText="Nominal Value" ReadOnly="True" />

<asp:BoundField DataField="Strike" HeaderText="Strike" ReadOnly="True" />

<asp:BoundField DataField="DeliveryDate" HeaderText="Delivery Date" ReadOnly="True" />

<asp:TemplateField HeaderText="LegId" SortExpression="LegId">


<asp:Label ID="lblLegID" runat="server" Text='<%# Bind("LegId") %>'></asp:Label>



<asp:TextBox runat="server" ID=txtLegID Text='<%# Bind("LegId") %>'></asp:TextBox>







<RowStyle BackColor="#FFFF66" ForeColor="#333333" />

<EditRowStyle BackColor="#FFFF66" Font-Names="Verdana" Font-Size="X-Small" ForeColor="#333333" />

<PagerStyle ForeColor="White" />

<AlternatingRowStyle BackColor="White" ForeColor="#333333" />


&nbsp;<br />


<asp:SqlDataSource ID="SqlDataSource_grid" runat="server" ConnectionString="<%$ ConnectionStrings:DealCaptureDev %>" SelectCommand="sp_get_single_deal" SelectCommandType="StoredProcedure" UpdateCommand="Update trDealLeg Set [email protected], [email protected], LastUpdate=GetDate(), LastUpdateBy=Session('userid') Where [email protected]" EnableCaching="True" ConflictDetection="CompareAllValues" ProviderName="System.Data.SqlClient">


<asp:QueryStringParameter DefaultValue="" Name="dealnum" QueryStringField="deal"

Type="String" />



<asp:ControlParameter ControlID="txtLegId" PropertyName="Text" Name="legId" />

<asp:ControlParameter ControlID="txtPrimarySettlement" Name="primarysettlement" PropertyName="Text" />

<asp:ControlParameter ControlID="txtAgreedSettlement" Name="agreedsettlement" PropertyName="Text"/>

<asp:SessionParameter DefaultValue="" Name="userid" SessionField="userid" />




As seen above, controls such as txtPrimarySettlement are referenced but the update is not successful. The text boxes are within the GridEditSettlement gridview.  In the .aspx code I cannot use FindControl (at least I don't think it will work).

So the questions are:  Is it possible to reference the child controls, if so - how?  Is there another way to do this, such as in the vb code behind - in the either the gridview's RowUpdating event or the SQLDataSource's Updating event.

What is the best approach?  Anyone come up against this issue before?



11/22/2006 2:39:35 AM
📃 29906 articles.

💬 1 Replies

You could try this way:

code in gridview:
                <asp:TemplateField HeaderText="company name" SortExpression="camcompany">
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("camcompany") %>'></asp:TextBox>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("camcompany") %>'></asp:TextBox>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("camcompany") %>'></asp:Label>

code in sqlDataSource:


                <asp:Parameter Name="camcompany" Type="String" />


 You can specify a row and access the controls within that row:

  Dim lastName As String = selectRow.Cells(1).Text

You can access the individual cells of the GridViewRow object by using the Cells property. If a cell contains other controls, you can retrieve a control from the cell by using the Controls collection of the cell. You can also use the FindControl method of the cell to find the control, if the control has an ID specified.

see for details.

Hope it helps.

Young Fang
11/28/2006 1:59:01 AM