0

我有一个带有 1 个更新面板和 2 个面板的 asp 页面,如果您单击第一个按钮,则第一个面板会出现,第二个面板会隐藏,如果您单击第二个按钮,则第二个面板会发生同样的事情。它工作正常,没有问题。

问题从第二个面板开始,我有另一个负责打印错误消息的更新面板。仅检查输入长度的消息工作正常,但那些涉及查询数据库以检查 id 有效性的消息没有被打印。

我认为查询是问题所在,我已经在没有更新面板的单独页面中尝试了它们,它们工作正常。问题出在更新面板的某个地方。我真的不明白在哪里。如果您能帮助我,我将不胜感激。

这是前端代码(我省略了一些无关紧要的细节):

<asp:Button ID="Button1" runat="server" Text="[Input your Id?]"  />
<asp:Button ID="Button2" runat="server" Text="[Aditional info]"  />
  .....
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
 < asp:Panel ID="Panel1" runat="server">
 some code here

 </asp:Panel>

 <asp:Panel ID="Panel2" runat="server">

<b>*ID Number:&nbsp;&nbsp;</b> 
<asp:TextBox ID="IdNo" runat="server" ></asp:TextBox><font size='2'>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" class="errorMess" ErrorMessage="Letters are not Allowed!!" ControlToValidate="IdNumb" ValidationExpression="\d+"
    runat="server" />
    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
   <ContentTemplate>
   <asp:Label ID="errorEm"  class="errorMess" runat="server" Text="Please fill in the required fields"></asp:Label>
   <asp:Label ID="errorLenght"  class="errorMess" runat="server" Text="Id is too long!!!"></asp:Label>
   <asp:Label ID="errorUser"  class="errorMess" runat="server" Text="Id is not valid!!!"></asp:Label>
   </ContentTemplate>
   <Triggers>
   <asp:AsyncPostBackTrigger ControlID="Button5" EventName="Click" />
   </Triggers>
   </asp:UpdatePanel> 
   <asp:Button ID="Button5" runat="server" Text="Generate" /></td></tr>

   </asp:Panel>
    </ContentTemplate>
    <Triggers>
   <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
   </Triggers>
   </asp:UpdatePanel>

这是后端 VB 代码:

 Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles  Button5.Click
If IdNo.Text = "" Then
    Panel1.Visible = False
    Panel2.Visible = True
    errorEm.Visible = True
ElseIf IdNo.Text.Length > 9 Then
    Panel1.Visible = False
    Panel2.Visible = True
    errorLenght.Visible = True
Else

    Try 'everything that comes here does not work'
        myconn.Open()
        Dim stquery As String = "SELECT * from account WHERE user_id= @id"
        Dim smd = New MySqlCommand(stquery, myconn)
        smd.Parameters.AddWithValue("@id", Convert.ToInt32(IdNo.Text))
        Dim myreader = smd.ExecuteReader()
        If Not myreader.HasRows Then
            Panel1.Visible = False
            Panel2.Visible = True
            errorUser.Visible = True
            myconn.Close()
            Return
        Else

            Panel1.Visible = False
            Panel2.Visible = True
            myreader.Read()
            Dim ErrorMessage As String = "alert('User has been found');"
            Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)


            myconn.Close()
        End If
        myconn.Close()
    Catch ex As Exception
        Dim ErrorMessage As String = "alert('" & ex.Message.ToString() & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", ErrorMessage, True)
        myconn.Close()
    End Try
End If
End Sub
4

1 回答 1

0

当您想从更新面板内部执行脚本时 - 不要使用

Page.ClientScript.RegisterStartupScript

利用

ClientScriptManager.RegisterStartupScript

反而。

参考:http: //msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

于 2013-05-06T16:12:34.813 回答