Gridview文本框-空字符串! [英] Gridview Textbox - Empty String!
问题描述
我以前曾经使用过网格,但是它们工作得很好.突然我开始遇到这个问题,每当我尝试添加新课程或编辑现有课程名称时,我输入的文本框值都会返回一个空字符串!因此,将在数据库中输入一个空白条目!我已经尝试调试,但是我不明白为什么会这样.无论我输入什么,都会变成一个空字符串!
我正在ajax标签控件和ajax面板中编写此网格
我不认为这会引起问题.我尝试从那里删除它,但仍然存在相同的问题.
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.
<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>
<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>
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
推荐答案
没有运行代码来调查是否还有其他问题,但是我相信您应该将.即:
Didn''t run your code to investigate whether there fis any further problem, but I believe you should Bind the Text boxes in theEditItemTemplate
. That is:
<edititemtemplate>
<asp:TextBox ID="txtCourseName" Text='<%# Bind("Name") %>' runat="server" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" Height="25px" Width="240px"></asp:TextBox>
</edititemtemplate>
您可能在页面加载时绑定了网格视图.问题可能是绑定方法在(!Page.IsPostBack)方法之外.这就是为什么,在将值发布到服务器之前,它使文本框为空.如果不是原因,请告知.
谢谢
阿努拉格(Anurag)
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
这篇关于Gridview文本框-空字符串!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!