当以下代码位于 VB 文件中时,__doPostBack JS 函数不起作用
我创建了一个页面,让我的网站管理员编辑和添加数据库中表的记录。由于 ASP.NET gridview 不允许 INSERT(它只允许选择、编辑和删除),我在 VB 文件中添加了一些内容以允许 INSERT INTO 语句(并且它可以工作,因为它是使用和 asp:Button
,它被转换为 html 作为 ,而不是
__doPostBack
)。
问题是,当我添加代码时,除了根本无法使用登录状态(注销链接)之外,我无法在 gridview 上使用分页或排序(即使登录状态完全独立于 accessDataSource 或 这
实际文件:
<asp:LoginStatus ID="LoginStatus1" runat="server"
LogoutAction="RedirectToLoginPage" />
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" DataSourceID="ADS" ForeColor="#333333"
GridLines="Horizontal" Width="100%" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True">
...(irrelevant code omitted)
<table>
<tr><td class="style4"><asp:Label ID="lblID" runat="server" Text="ID"></asp:Label></td>
<td class="style5"><asp:TextBox ID="tbID" runat="server" Width="127px"></asp:TextBox></td>
<td class="style5">
</td></tr>
<tr><td class="style2"><asp:Label ID="lblHead" runat="server" Text="Head"></asp:Label></td>
<td><asp:TextBox ID="tbHead" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please enter a header" ControlToValidate="tbHead"></asp:RequiredFieldValidator>
</td></tr>
<tr><td class="style3"><asp:Label ID="lblBody" runat="server" Text="Body"></asp:Label></td>
<td class="style1"><asp:TextBox ID="tbBody" runat="server"></asp:TextBox></td>
<td class="style1"></td></tr>
<tr><td class="style2"><asp:Label ID="lblBy" runat="server" Text="by"></asp:Label></td>
<td><asp:TextBox ID="tbBy" runat="server"></asp:TextBox></td>
<td></td></tr>
<tr><td class="style2"><asp:Label ID="lblURL" runat="server" Text="URL"></asp:Label></td>
<td><asp:TextBox ID="tbURL" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Please enter a valid url" ControlToValidate="tbURL"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Please enter a valid url" ControlToValidate="tbURL"
Display="Dynamic"
ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
</td></tr>
<tr><td class="style2"><asp:Label ID="lblDateposted" runat="server" Text="dateposted"></asp:Label></td>
<td><asp:TextBox ID="tbDateposted" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="Please enter a valid date following the current format (mm/dd/yyyy) ignore the time."
ControlToValidate="tbDateposted"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Please enter a valid date following the current format (mm/dd/yyyy) ignore the time."
ControlToValidate="tbDateposted" Display="Dynamic"
ValidationExpression="^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$"></asp:RegularExpressionValidator>
</td></tr>
<tr><td class="style2">
<asp:Button ID="submit" runat="server" Text="Update" /></td>
</tr></table>
是VB文件:
Imports System.Data.OleDb
Partial Class staffcp_Default
Inherits System.Web.UI.Page
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../App_Data/Signees.mdb")
Dim dbConnection As OleDbConnection = New OleDbConnection(connStr)
Dim SQL As New OleDbCommand
dbConnection.Open()
SQL.Connection = dbConnection
SQL.CommandText = "INSERT INTO aae VALUES (" + tbID.Text + ", '" + tbHead.Text + "', '" + tbBody.Text + "', '" + tbBy.Text + "', '" + tbURL.Text + "', '" + tbDateposted.Text + "')"
SQL.ExecuteNonQuery()
dbConnection.Close()
SQL.Connection.Close()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SQLResult As Object
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../App_Data/Signees.mdb")
Dim dbConnection As OleDbConnection = New OleDbConnection(connStr)
Dim SQL As New OleDbCommand
dbConnection.Open()
SQL.Connection = dbConnection
SQL.CommandText = "SELECT COUNT (*) FROM aae"
SQLResult = SQL.ExecuteScalar
Dim count As Object = SQLResult
dbConnection.Close()
SQL.Connection.Close()
tbID.Text = count.ToString + 1
tbID.ReadOnly = True
End Sub
End Class
任何帮助将不胜感激
[编辑]:我刚刚尝试了同样的事情,清除了VB文件:同样的问题,没有排序或分页(或在我现在很困惑。
[编辑]:我一直无法弄清楚页面底部的更新按钮的问题。
I have a page that I created to let moderators of my site edit and add records of a table in a database. Since ASP.NET gridview does not allow for the INSERT (it only allows for select, edit, and delete) I added a bit in the VB file to allow for an INSERT INTO statement (and that works because it is submitted using and asp:Button
, which is translated into html as an <input>
as opposed to a __doPostBack
).
The problem is that when I added the code I wasn't able to use paging or sorting on the gridview in addition to not being able to use the loginStatus (logout link) at all (even though the loginStatus is completely independent of the accessDataSource or gridview.
The actual file:
<asp:LoginStatus ID="LoginStatus1" runat="server"
LogoutAction="RedirectToLoginPage" />
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" DataSourceID="ADS" ForeColor="#333333"
GridLines="Horizontal" Width="100%" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True">
...(irrelevant code omitted)
<table>
<tr><td class="style4"><asp:Label ID="lblID" runat="server" Text="ID"></asp:Label></td>
<td class="style5"><asp:TextBox ID="tbID" runat="server" Width="127px"></asp:TextBox></td>
<td class="style5">
</td></tr>
<tr><td class="style2"><asp:Label ID="lblHead" runat="server" Text="Head"></asp:Label></td>
<td><asp:TextBox ID="tbHead" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please enter a header" ControlToValidate="tbHead"></asp:RequiredFieldValidator>
</td></tr>
<tr><td class="style3"><asp:Label ID="lblBody" runat="server" Text="Body"></asp:Label></td>
<td class="style1"><asp:TextBox ID="tbBody" runat="server"></asp:TextBox></td>
<td class="style1"></td></tr>
<tr><td class="style2"><asp:Label ID="lblBy" runat="server" Text="by"></asp:Label></td>
<td><asp:TextBox ID="tbBy" runat="server"></asp:TextBox></td>
<td></td></tr>
<tr><td class="style2"><asp:Label ID="lblURL" runat="server" Text="URL"></asp:Label></td>
<td><asp:TextBox ID="tbURL" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Please enter a valid url" ControlToValidate="tbURL"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Please enter a valid url" ControlToValidate="tbURL"
Display="Dynamic"
ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
</td></tr>
<tr><td class="style2"><asp:Label ID="lblDateposted" runat="server" Text="dateposted"></asp:Label></td>
<td><asp:TextBox ID="tbDateposted" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="Please enter a valid date following the current format (mm/dd/yyyy) ignore the time."
ControlToValidate="tbDateposted"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Please enter a valid date following the current format (mm/dd/yyyy) ignore the time."
ControlToValidate="tbDateposted" Display="Dynamic"
ValidationExpression="^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$"></asp:RegularExpressionValidator>
</td></tr>
<tr><td class="style2">
<asp:Button ID="submit" runat="server" Text="Update" /></td>
</tr></table>
And here is the VB file:
Imports System.Data.OleDb
Partial Class staffcp_Default
Inherits System.Web.UI.Page
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../App_Data/Signees.mdb")
Dim dbConnection As OleDbConnection = New OleDbConnection(connStr)
Dim SQL As New OleDbCommand
dbConnection.Open()
SQL.Connection = dbConnection
SQL.CommandText = "INSERT INTO aae VALUES (" + tbID.Text + ", '" + tbHead.Text + "', '" + tbBody.Text + "', '" + tbBy.Text + "', '" + tbURL.Text + "', '" + tbDateposted.Text + "')"
SQL.ExecuteNonQuery()
dbConnection.Close()
SQL.Connection.Close()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SQLResult As Object
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../App_Data/Signees.mdb")
Dim dbConnection As OleDbConnection = New OleDbConnection(connStr)
Dim SQL As New OleDbCommand
dbConnection.Open()
SQL.Connection = dbConnection
SQL.CommandText = "SELECT COUNT (*) FROM aae"
SQLResult = SQL.ExecuteScalar
Dim count As Object = SQLResult
dbConnection.Close()
SQL.Connection.Close()
tbID.Text = count.ToString + 1
tbID.ReadOnly = True
End Sub
End Class
Any help would be greatly appreciated
[EDIT]: I just tried the same thing with the VB file cleared: same problem, no sorting or paging (or logging out on the loginStatus). I am seriously confused at this point.
[EDIT]: I was able to pinpoint the issue to the Update Button all the way on the bottom of the page. Can't figure out why though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加 :
如果不是 page.isPostBack 到 page_load 事件中的代码
Add :
If not page.isPostBack to your code in the page_load event