K
Knoxy
Hi guys,
I'm using a gridview to do some inline editing. The data binds okay,
its goes into edit mode and cancel okay. I'm using my own custom
commandName, "save", and it picks this up okay. Don't want to use the
built in "update" command. Trouble is when I go to access the form
fields (textboxes etc), there isn't any data there (ie text in the
textbox), I can access them though, I guess I'm missing an event
somewhere.
I've stripped out the necessary bits of code that i've used and added
it below.
Any ideas most appreciated, driving me nuts.
Regards.
**** UI ****
..
..
..
<asp:TemplateField HeaderStyle-Width="150">
<ItemTemplate><%# Eval("EmailAddress") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailAddress" runat="server" Text='<%#
Eval("EmailAddress") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="80" ItemStyle-
HorizontalAlign="Right">
<ItemTemplate>
<asp:LinkButton CommandName="Edit" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Content/Images/
document_edit.png" />
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Save" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image2" runat="server" ImageUrl="~/Content/Images/
document_ok.png" />
</asp:LinkButton>
<asp:LinkButton CommandName="Cancel" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image3" runat="server" ImageUrl="~/Content/Images/
undo.png" />
</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:ObjectDataSource .... SelectMethod="GetAlarms" .... />
**** code behind ****
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = gvData.Rows[index];
switch (e.CommandName.ToLower())
{
case "save":
UpdateAlarmMapping(row);
gvData.EditIndex = -1;
gvData.DataBind();
break;
}
}
private void UpdateAlarmMapping(GridViewRow row)
{
Alarm alarm = new Alarm();
alarm.Enabled = ((CheckBox)row.Cells[2].Controls[0]).Checked;
alarm.EmailAddress =
((TextBox)row.FindControl("txtEmailAddress")).Text;
alarm.MobileNumber =
((TextBox)row.FindControl("txtMobileNumber")).Text;
alarm.AlarmNo =
int.Parse(((Literal)row.FindControl("litAlarmNo")).Text);
bool ok = alarm.SaveAlarmMapping();
}
I'm using a gridview to do some inline editing. The data binds okay,
its goes into edit mode and cancel okay. I'm using my own custom
commandName, "save", and it picks this up okay. Don't want to use the
built in "update" command. Trouble is when I go to access the form
fields (textboxes etc), there isn't any data there (ie text in the
textbox), I can access them though, I guess I'm missing an event
somewhere.
I've stripped out the necessary bits of code that i've used and added
it below.
Any ideas most appreciated, driving me nuts.
Regards.
**** UI ****
..
..
..
<asp:TemplateField HeaderStyle-Width="150">
<ItemTemplate><%# Eval("EmailAddress") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailAddress" runat="server" Text='<%#
Eval("EmailAddress") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="80" ItemStyle-
HorizontalAlign="Right">
<ItemTemplate>
<asp:LinkButton CommandName="Edit" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Content/Images/
document_edit.png" />
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Save" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image2" runat="server" ImageUrl="~/Content/Images/
document_ok.png" />
</asp:LinkButton>
<asp:LinkButton CommandName="Cancel" CommandArgument='<%#
Container.DataItemIndex %>' runat="server">
<asp:Image ID="Image3" runat="server" ImageUrl="~/Content/Images/
undo.png" />
</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:ObjectDataSource .... SelectMethod="GetAlarms" .... />
**** code behind ****
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = gvData.Rows[index];
switch (e.CommandName.ToLower())
{
case "save":
UpdateAlarmMapping(row);
gvData.EditIndex = -1;
gvData.DataBind();
break;
}
}
private void UpdateAlarmMapping(GridViewRow row)
{
Alarm alarm = new Alarm();
alarm.Enabled = ((CheckBox)row.Cells[2].Controls[0]).Checked;
alarm.EmailAddress =
((TextBox)row.FindControl("txtEmailAddress")).Text;
alarm.MobileNumber =
((TextBox)row.FindControl("txtMobileNumber")).Text;
alarm.AlarmNo =
int.Parse(((Literal)row.FindControl("litAlarmNo")).Text);
bool ok = alarm.SaveAlarmMapping();
}