在我的 gridview 中,我为每一行提供了一个文本框,该文本框有一个相应的按钮,该按钮从文本框中获取值并更新数据库中的行值。
但是我想在标题模板的顶部添加一个按钮,它将获取所有文本框的值并更新它们的行。
我尝试了以下代码,但在“instance_id cell[0]”处出现空引用异常
我的代码如下。
`
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Update Attendance" CommandName="Select" />
</Columns>
</asp:GridView>`
并在 Button Click 上从所有文本框中获取数据:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
vo.instance_ID = GridView1.SelectedRow.Cells[0].Text;
vo.Project_ID = GridView1.SelectedRow.Cells[1].Text;
vo.volunteer_ID = GridView1.SelectedRow.Cells[6].Text;
vo.Campus_ID = GridView1.SelectedRow.Cells[5].Text;
vo.attendance_hours = (GridView1.SelectedRow.FindControl("TextBox1") as TextBox).Text;
Convert.ToInt32(vo.attendance_hours);
try
{
int success = BL.fn_UpdateAttendance(vo);
if (success == 2)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Attendance Updated Successfully')", true);
// Label2.Text = "Attendance Updated Successfully";
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Attendance Not Updated')", true);
// Label2.Text = "Attendance Not Updated";
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}