我对两个按钮都有同样的错误。我认为这是部分RowDataBound
命令:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
错误代码:
对象引用未设置为对象的实例。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。
异常详细信息:
System.NullReferenceException:未将对象引用设置为实例 一个对象。
public void btninsert_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["@datetime"].DefaultValue = Convert.ToString(DateTime.Now);
SqlDataSource1.InsertParameters["@col1"].DefaultValue = ((TextBox)GridView1.HeaderRow.FindControl("TextBoxHeadercol1")).Text;
SqlDataSource1.InsertParameters["@col2"].DefaultValue = ((TextBox)GridView1.HeaderRow.FindControl("TextBoxHeadercol2")).Text;
SqlDataSource1.InsertParameters["@col3"].DefaultValue = ((TextBox)GridView1.HeaderRow.FindControl("TextBoxHeadercol3")).Text;
SqlDataSource1.Insert();
GridView1.DataBind();
}
public void btntotal_Click(object sender, EventArgs e)
{
foreach (GridViewRow GR in GridView1.Rows)
{
TextBox TempTxt1 = (TextBox)GR.FindControl("txt1");
TextBox TempTxt2 = (TextBox)GR.FindControl("txt2");
TextBox TempTxt3 = (TextBox)GR.FindControl("txt3");
TempTxt1.Text = Convert.ToString(TempTxt1);
TempTxt2.Text = Convert.ToString(TempTxt2);
TempTxt3.Text = Convert.ToString(TempTxt3);
}
}
Default.aspx的
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
DataKeyNames="idt"
OnDataBound="GridView1_RowDataBound"
showfooter="true">
<Columns>
<asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
<asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
<asp:TemplateField SortExpression="col1">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol1" text="col1" runat="server" MaxLength="40" />
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("col1") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol1" text="col1" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col2">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol2" text="col2" runat="server" MaxLength="40" />
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt2" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl2" runat="server" Text='<%# Bind("col2") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol2" text="col2" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col3">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol3" text="col3" runat="server" MaxLength="40" />
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt3" runat="server" Text='<%# Eval("col3") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl3" runat="server" Text='<%# Bind("col3") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxFootercol3" text="col3" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton OnClick="btninsert_Click" ID="btninsert" runat="server" >Insert Into</asp:LinkButton>
</HeaderTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxAllTotal" Text="Total" runat="server" MaxLength="40" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton OnClick="btntotal_Click" ID="btntotal" runat="server" >Total</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
id="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:connone %>"
SelectCommand="SELECT * FROM [test];
InsertCommand="INSERT INTO [test] ([datetime],[col1],[col2],[col3]) VALUES (@datetime,@col1,@col2,@col3);"
runat="server">
<InsertParameters>
<asp:Parameter Name="datetime" Type="DateTime" />
<asp:Parameter Name="col1" Type="String" />
<asp:Parameter Name="col2" Type="String" />
<asp:Parameter Name="col3" Type="String" />
</InsertParameters>
</asp:SqlDataSource>