I'm using asp.net 2.0 and C#. I have a gridview with templates defined (ItemTemplate, EditItemTemplate, and FooterTemplate).
<asp:GridView EnableTheming="true" ID="TTIGrid" ShowFooter="true" runat="Server" DataKeyNames="ttiID" DataSourceID="TTIDataSource" OnRowCommand="TTIGrid_RowCommand" PageSize="15">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />
<asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button Text="Add Task" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task #">
<ItemTemplate>
<asp:Label ID="TaskStatementNum" Text='<%# Eval("TaskStatementNum") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TaskStatementNum" runat="server" Text='<% Bind("TaskStatementNum") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TaskStatementNum" runat="server" Text="" Width="51px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Statement">
<ItemTemplate>
<asp:Label ID="TaskStatement" Text='<%# Eval("TaskStatement") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TaskStatement" runat="server" Text='<% Bind("TaskStatement") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TaskStatement" runat="server" Text="" Width="663px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
The FooterTemplate is for adding a new record. I also have an EmptyDataTemplate defined for input of a new record when there are no rows in the grid, code as follows:
<EmptyDataTemplate>
<asp:Label SkinID="TextBoxLabel" ID="InstructionLabel1" runat="server" Text="There are currently no task statements defined for this course."></asp:Label><br />
<asp:Label SkinID="TextBoxLabel" ID="InstructionLabel2" runat="server" Text="Enter a task statement below:"></asp:Label><br /><br />
<asp:Label SkinID="TextBoxLabel" ID="TaskStatementNumLabel" runat="server" Width=125 Text="Task #"></asp:Label>
<asp:TextBox ID="TaskStatementNum" runat="server" Width=40></asp:TextBox><br />
<asp:Label SkinID="TextBoxLabel" ID="TaskStatementLabel" runat="server" Width=125 Text="Task Statement"></asp:Label>
<asp:TextBox ID="TaskStatement" runat="server" Width=600></asp:TextBox><br />
<asp:Button ID="btSend" Text="Add Task" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
In my code-behind, I have the following method:
protected void TTIGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
TTIDataSource.InsertParameters["CourseProfileID"].DefaultValue = CourseProfileIDTextBox.Text;
if (e.CommandName == "EmptyInsert")
{
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatementNum")).Text; TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatement")).Text;
TTIDataSource.Insert();
}
if (e.CommandName == "Insert")
{
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.FooterRow.FindControl("TaskStatementNum")).Text; TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.FooterRow.FindControl("TaskStatement")).Text;
TTIDataSource.Insert();
}
if (e.CommandName == "Update")
{
TTIDataSource.InsertParameters["ttiID"].DefaultValue = TTIGrid.SelectedDataKey.Value.ToString();
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatementNum")).Text;
TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatement")).Text;
TTIDataSource.Update();
}
Everything works fine except for when you click on the 'Edit' button in a row. It creates the textboxes for editting the row's values, but the textboxes are not populated with the values. Instead, the textboxes are populated with the string '<% Bind("TaskStatementNum") %>' and the string '<% Bind("TaskStatement") %>'. The 'Delete' button for a row works fine. I can't figure out why the 'Edit' button is not working and how to get the values populated correctly. Any help would be highly appreciated.
Jan
<asp:GridView EnableTheming="true" ID="TTIGrid" ShowFooter="true" runat="Server" DataKeyNames="ttiID" DataSourceID="TTIDataSource" OnRowCommand="TTIGrid_RowCommand" PageSize="15">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />
<asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button Text="Add Task" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task #">
<ItemTemplate>
<asp:Label ID="TaskStatementNum" Text='<%# Eval("TaskStatementNum") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TaskStatementNum" runat="server" Text='<% Bind("TaskStatementNum") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TaskStatementNum" runat="server" Text="" Width="51px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Statement">
<ItemTemplate>
<asp:Label ID="TaskStatement" Text='<%# Eval("TaskStatement") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TaskStatement" runat="server" Text='<% Bind("TaskStatement") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TaskStatement" runat="server" Text="" Width="663px" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
The FooterTemplate is for adding a new record. I also have an EmptyDataTemplate defined for input of a new record when there are no rows in the grid, code as follows:
<EmptyDataTemplate>
<asp:Label SkinID="TextBoxLabel" ID="InstructionLabel1" runat="server" Text="There are currently no task statements defined for this course."></asp:Label><br />
<asp:Label SkinID="TextBoxLabel" ID="InstructionLabel2" runat="server" Text="Enter a task statement below:"></asp:Label><br /><br />
<asp:Label SkinID="TextBoxLabel" ID="TaskStatementNumLabel" runat="server" Width=125 Text="Task #"></asp:Label>
<asp:TextBox ID="TaskStatementNum" runat="server" Width=40></asp:TextBox><br />
<asp:Label SkinID="TextBoxLabel" ID="TaskStatementLabel" runat="server" Width=125 Text="Task Statement"></asp:Label>
<asp:TextBox ID="TaskStatement" runat="server" Width=600></asp:TextBox><br />
<asp:Button ID="btSend" Text="Add Task" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
In my code-behind, I have the following method:
protected void TTIGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
TTIDataSource.InsertParameters["CourseProfileID"].DefaultValue = CourseProfileIDTextBox.Text;
if (e.CommandName == "EmptyInsert")
{
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatementNum")).Text; TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatement")).Text;
TTIDataSource.Insert();
}
if (e.CommandName == "Insert")
{
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.FooterRow.FindControl("TaskStatementNum")).Text; TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.FooterRow.FindControl("TaskStatement")).Text;
TTIDataSource.Insert();
}
if (e.CommandName == "Update")
{
TTIDataSource.InsertParameters["ttiID"].DefaultValue = TTIGrid.SelectedDataKey.Value.ToString();
TTIDataSource.InsertParameters["TaskStatementNum"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatementNum")).Text;
TTIDataSource.InsertParameters["TaskStatement"].DefaultValue = ((TextBox)TTIGrid.Controls[0].Controls[0].FindControl("TaskStatement")).Text;
TTIDataSource.Update();
}
Everything works fine except for when you click on the 'Edit' button in a row. It creates the textboxes for editting the row's values, but the textboxes are not populated with the values. Instead, the textboxes are populated with the string '<% Bind("TaskStatementNum") %>' and the string '<% Bind("TaskStatement") %>'. The 'Delete' button for a row works fine. I can't figure out why the 'Edit' button is not working and how to get the values populated correctly. Any help would be highly appreciated.
Jan