0

I have a WebForms GridView, where I want to update a BoundField conditionally (or I use a value from a DropDown or I use the text from a Textbox), based on the value from another DropDownList.

<asp:GridView ID="gv_results" runat="server" DataKeyNames="ID_RESULTS" DataSourceID="get_results">
            <Columns>
              <asp:TemplateField HeaderText="DETAIL" SortExpression="DETAIL">
                    <EditItemTemplate>
                        <asp:TextBox ID="tb_detail_edit" runat="server"></asp:TextBox>
                        <asp:DropDownList ID="dd_detail_edit" runat="server" AutoPostBack="True" DataSourceID="get_detail" DataTextField="DETAIL" DataValueField="DETAIL" ></asp:DropDownList>
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>

        <asp:SqlDataSource runat="server" ID="get_results" 
            UpdateCommand="UPDATE RESULTS SET [DETAIL] = COALESCE(@DETAIL,@DETAILwelcome)">
            <UpdateParameters>
                <asp:ControlParameter ControlID="ctl00$MainContent$gv_results$ctl02$dd_detail_edit" PropertyName="SelectedValue" Name="DETAIL" Type="String"></asp:ControlParameter>
                <asp:ControlParameter ControlID="ctl00$MainContent$gv_results$ctl02$tb_detail_edit" PropertyName="Text"  Name="DETAILwelcome" Type="String"></asp:ControlParameter>
            </UpdateParameters>
        </asp:SqlDataSource>

Well, I found out in the google's that I have to use the Direct ID in the Control Parameter. And it works...the thing is , the $ctl02 (for example) before the id of the dd_detail_edit, changes, with different rows in the Gridview (so this id works for one row, but maybe for the next it doesn't work.

Is there some workaround for this?

Using only something like $gv_results$dd_detail_edit , doesn't work, I dunno why :S

TIA and Best Regards,

chimino
  • 29
  • 1
  • 4
  • Use GridViewRow.FindControl() method. Here's def on MSDN: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.findcontrol(v=vs.110).aspx and here a possible solution: http://stackoverflow.com/questions/6873973/how-to-find-control-in-templatefield-of-gridview – Kami Jul 18 '16 at 19:36
  • Well, I think I got it: In method gv_resultados_RowUpdating , I use the Find Control the row: detail = ((TextBox)gv_results.Rows[e.RowIndex].FindControl("tb_detail_edit")).Text.Trim(); or (DropDownList)gv_results.Rows[e.RowIndex].FindControl("dd_detail_edit").SelectedValue; Thkx ;) – chimino Jul 19 '16 at 10:54

1 Answers1

0

For one thing it looks like you have an open <ItemTemplate> tag without any closing one.

If you check out the .net documentation you can see where they use the regular control IDs, like ControlID = dd_detail_edit.

I do not think you're providing enough details for me to provide a complete answer, but those are a couple of things I noticed.

peyote boy
  • 66
  • 1
  • 2
  • Hello, Peyote, yes there's an open tag , it's a typo because i cutted a lot of code (to show only the important) :P Yes, normally the id from the control works, but with these controls inside EditItemTemplates for example doesn't work... :\ – chimino Jul 19 '16 at 09:41