Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field values not displaying in EditMode of Gridview

Status
Not open for further replies.

JanVolley

Programmer
Sep 8, 2008
14
US
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" />&nbsp;
<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" />&nbsp;
<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" />&nbsp;
<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
 
Text='<% Bind("TaskStatementNum") %>'
is missing the # sign

should be

Text='<%# Bind("TaskStatementNum") %>'

there are a couple times i see that in your code above.
 
you forgot the # sign which needs to preceed Bind();
Code:
<%#Bind("name of column/property")%>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you SO much! I knew it had to be something simple, but it was driving me crazy. I have no idea how that # got removed because it was there originally and was working before I added the FooterTemplate and the EmptyDataTemplate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top