Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have worked with grids before and they worked fine. Suddenly i am gettin this issue that whenever i try to add new course or edit an existing course name, the textbox value i enter returns an empty string! THerefore, a blank entry is made into the database! I have tried to debug, but i cannot understand why this is happening. Whatever i type in, turns into a blank string!

I am writing this grid in ajax tab control and ajax panel
i don't think that should cause an issue. I tried removing it from there but still same issues.

XML
<asp:GridView ID="gvListCourses" runat="server" AllowPaging="True"
                                      CellPadding="1" CellSpacing="2" ShowFooter="True" DataKeyNames="CourseID"
                                      EmptyDataText="No courses available" AutoGenerateColumns="False"
                                      Width="292px">

                                     <AlternatingRowStyle CssClass="ProfileAlternateRow" />

                                     <Columns>
                                         <asp:TemplateField HeaderText="Course Name" SortExpression="Name">
                                             <EditItemTemplate>
                                                 <asp:TextBox ID="txtCourseName" runat="server" BorderColor="#999999"
                                                     BorderStyle="Solid" BorderWidth="1px" Height="25px" Width="240px"></asp:TextBox>
                                             </EditItemTemplate>
                                             <FooterTemplate>
                                                 <asp:TextBox ID="txtNewCourseName" runat="server"
                                                     BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" Height="25px"
                                                     Width="240px" MaxLength="150" Wrap="False"></asp:TextBox>
                                             </FooterTemplate>
                                             <ItemTemplate>
                                                 <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                             </ItemTemplate>
                                         </asp:TemplateField>
                                         <asp:TemplateField ShowHeader="False">
                                             <EditItemTemplate>
                                                 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                                                     CommandName="Update" Text="Update"></asp:LinkButton>
                                                 &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
                                                     CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                             </EditItemTemplate>
                                             <FooterTemplate>
                                                 <asp:LinkButton ID="lnkAddCourse" runat="server" CommandName="Add">Add...</asp:LinkButton>
                                             </FooterTemplate>
                                             <ItemTemplate>
                                                 <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                                     CommandName="Edit" Text="Edit"></asp:LinkButton>
                                             </ItemTemplate>
                                         </asp:TemplateField>
                                         <asp:CommandField ShowDeleteButton="True"/>
                                                                               </Columns>
                                     <FooterStyle CssClass="ProfileRow" />
                                     <HeaderStyle CssClass="ProfileChildHeader" />
                                     <PagerStyle CssClass="ProfileChildHeader" HorizontalAlign="Right" />

                                     <RowStyle CssClass="ProfileRow" />

                               </asp:GridView>


VB
Protected Sub gvListCourses_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvListCourses.PageIndexChanging
    Try
        gvListCourses.PageIndex = e.NewPageIndex
        FillCourses()
    Catch ex As Exception
    End Try
End Sub
Protected Sub gvListCourses_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvListCourses.RowCancelingEdit
    gvListCourses.EditIndex = -1
    FillCourses()
End Sub
Protected Sub gvListCourses_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvListCourses.RowCommand
    Try
        If e.CommandName.Equals("Add") Then
            Dim txtCname As TextBox = CType(gvListCourses.FooterRow.FindControl("txtNewCourseName"), TextBox)
            If Not txtCname Is Nothing Then
                Dim akkad As New InstituteDefaults
                If akkad.AddCourse(txtCname.Text) > 0 Then
                    FillCourses()
                End If
            End If
        End If
    Catch ex As Exception
    End Try
End Sub
Protected Sub gvListCourses_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvListCourses.RowDeleting
    Try
        Dim Akkad As New InstituteDefaults
        If Akkad.DeleteCourse(gvListCourses.DataKeys(e.RowIndex).Values(0)) = True Then
            FillCourses()
        End If
    Catch ex As Exception
    End Try
End Sub
Protected Sub gvListCourses_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvListCourses.RowEditing
    Try
        gvListCourses.EditIndex = e.NewEditIndex
        FillCourses()
    Catch ex As Exception
    End Try
End Sub
Protected Sub gvListCourses_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvListCourses.RowUpdating
    Try
        Dim akkad As New InstituteDefaults
        Dim txtCName As TextBox = CType(gvListCourses.Rows(e.RowIndex).FindControl("txtCourseName"), TextBox)
        If Not txtCName Is Nothing Then
            If akkad.AddCourse(txtCName.Text, 0, gvListCourses.DataKeys(e.RowIndex).Values(0)) = True Then
                gvListCourses.EditIndex = -1
                FillCourses()
            End If
        End If
    Catch ex As Exception
    End Try
End Sub
Posted
Updated 30-Aug-10 23:53pm
v2

Didn't run your code to investigate whether there fis any further problem, but I believe you should Bind the Text boxes in the EditItemTemplate. That is:

XML
<edititemtemplate>
      <asp:TextBox ID="txtCourseName" Text='<%# Bind("Name") %>' runat="server" BorderColor="#999999"
     BorderStyle="Solid" BorderWidth="1px" Height="25px" Width="240px"></asp:TextBox>
</edititemtemplate>
 
Share this answer
 
You may be binding your gridview at page load. The problem may be that the binding method is outside (!Page.IsPostBack) method. Thats why, its making the text boxes empty before posting the values to the server. Do inform, if this was not the reason.
Thanks
Anurag
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900