0
在我的gridview中,我爲每一行提供了一個文本框,它有一個相應的按鈕,用於從文本框中提取值並更新數據庫中的行值。爲一個按鈕上的gridview中的每一行更新文本框點擊
但我想在頁眉模板的頂部添加一個按鈕,它將獲取所有文本框的值並更新它們的行。
我曾嘗試下面的代碼,但我在「INSTANCE_ID細胞[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>`
而上的按鈕進行點擊從所有的文本框獲取數據:
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);
}
}
}