Search code examples
c#gridviewemptydatatemplate

insert new row with detailsview on gridview's EmptyDataTemplate


I use the gridview to manage the data, when the database does not contain the data it needs to be input first. to input data the first time I use detailsview that I put in the gridview's EmptyDataTemplate. how to access detailview's textbox which is in gridview's EmptyDataTemplate?I always get error null reference

<EmptyDataTemplate>                
                <asp:DetailsView ID="DetailsView3" runat="server" AutoGenerateRows="False" DataKeyNames="IDKOREKSIREFUND" DataSourceID="KoreksiRefundObjectDataSource" DefaultMode="Insert" Height="50px" Width="125px" >
                    <Fields>
                        <asp:BoundField DataField="IDKOREKSIREFUND" HeaderText="IDKOREKSIREFUND" InsertVisible="False" ReadOnly="True" SortExpression="IDKOREKSIREFUND" />
                        
                        <asp:TemplateField HeaderText="NILAI" SortExpression="NILAI">                            
                            <InsertItemTemplate>
                                <asp:TextBox ID="dvNilaiTextBox" runat="server" Text='<%# Bind("NILAI") %>'></asp:TextBox>
                            </InsertItemTemplate>                            
                        </asp:TemplateField>
                        
                        <asp:CommandField ShowInsertButton="True" />
                    </Fields>
                </asp:DetailsView>
            </EmptyDataTemplate>
        </asp:GridView>

protected void KoreksiRefundObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        if (KoreksiGridView.Rows.Count == 0)
        // We are inserting through the DetailsView in the EmptyDataTemplate
        {                
            TextBox NewNilai1 = (TextBox)KoreksiGridView.FooterRow.Controls[0].FindControl("dvNilaiTextBox");

            decimal? NILAI1 = null;
            if (!string.IsNullOrEmpty(NewNilai1.Text))
                NILAI1 = Convert.ToDecimal(NewNilai1.Text);
            e.InputParameters["NILAI"] = NewNilai1.Text;

            return;
        }


        // Programmatically reference Web controls in the inserting interface...
        TextBox NewNilai= (TextBox)KoreksiGridView.FooterRow.FindControl("NewNilaiTextBox");                    

        decimal? NILAI = null;
        if (!string.IsNullOrEmpty(NewNilai.Text))
            NILAI = Convert.ToDecimal(NewNilai.Text);
        e.InputParameters["NILAI"] = NewNilai.Text;

    }

Solution

  • thanks for the suggestion, i ended up using the code as follows:

     if (KoreksiGridView.Rows.Count == 0)
            // We are inserting through the DetailsView in the EmptyDataTemplate
            {
                DetailsView DetailsView3 = (DetailsView) KoreksiGridView.Controls[0].Controls[0].FindControl("DetailsView3");
                TextBox NewNilai1 = (TextBox)DetailsView3.FindControl("dvNilaiTextBox");