我有一个网格,我想要做的是当点击任何行的编辑按钮时,该行的数据应该传递给文本框。但我无法这样做。我使用过RowCommand但仍然无法获取数据。
asp代码
<div class="form-group">
<label>FULL NAME</label>
<input type="text" class="form-control" runat="server" id="Name" autocomplete="off" />
</div>
<div class="form-group">
<label>GENDER</label><span class="required">*</span>
<select class="form-control" id="selectGender" runat="server" style="height: 34px;">
<option value="0">MALE</option>
<option value="1">FEMALE</option>
</select>
</div>
<div class="col-md-12">
<asp:GridView ID="famGrid" Visible="false"
runat="server" OnRowCommand="famGrid_RowCommand">
<Columns>
<asp:TemplateField HeaderText="S No." ItemStyle-Width="3%">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField
HeaderText="FULL NAME" DataField="Name">
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField
HeaderText="GENDER" DataField="selectGender">
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:Button Text="EDIT" ID="btnEdit" CommandName="ChangeStatus" formnovalidate="formnovalidate" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
背后的代码
protected void famGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int selectedRowIndex = -1;
if (e.CommandName == "ChangeStatus")
{
DataTable dt = ViewState["FamilyTable"] as DataTable;
GridViewRow gvr = famGrid.SelectedRow;
lblRow.Text = gvr.Cells[1].Text;
Name.Value = gvr.Cells[2].Text;
selectGender.Items[selectGender.SelectedIndex].Text = gvr.Cells[3].Text;
}
}
catch (Exception)
{
throw;
}
}
我收到错误null引用错误。
答案 0 :(得分:0)
我使用了SelectedIndexChanged
。它正在发挥作用。
protected void famGrid_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = famGrid.SelectedRow;
lblRow.Text = gvr.Cells[1].Text;
Name.Value = gvr.Cells[2].Text;
selectGender.Items[selectGender.SelectedIndex].Text = gvr.Cells[3].Text;
}
答案 1 :(得分:0)
请告诉我们您将获得例外的哪条线。