在gridview的EmptyDataTemplate上插入带有detailsview的新行

时间:2017-09-26 02:32:35

标签: c# gridview emptydatatemplate

当数据库不包含首先需要输入的数据时,我使用gridview来管理数据。我第一次使用我在gridview的EmptyDataTemplate中放入的detailsview时输入数据。 如何访问gridview的EmptyDataTemplate中的detailview文本框?我总是得到错误空引用

<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;

    }

1 个答案:

答案 0 :(得分:0)

感谢您的建议,我最终使用了以下代码:

 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");