2

我是 ASP.NET 和 AJAX 的新手,所以我可能会以完全错误的方式处理事情,

目前这一切都只是测试,我有一个带有 onclick 功能的更新面板来访问数据库和更新值。我正在尝试通过按链接按钮来更新页面上的客户端 ID。这适用于第一次点击,但随后的点击显然仍在读取服务器端数据而不是更新的客户端数据,我现在需要能够将客户端 ID 的更新值返回给我的链接按钮点击功能。

ASPX 代码

     <form id="form1" runat="server">
     <asp:toolkitscriptmanager ID="ToolkitScriptManager1" runat="server">
</asp:toolkitscriptmanager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="click" /> 
    </Triggers> 

     <ContentTemplate>

<div class="topbox">
   <h3>Client</h3> 


<div style="float:left">

<p>
    <asp:Label ID="Label1" runat="server" Text="ClientID" Width="150px"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</p>
      <asp:LinkButton ID="LinkButton1" runat="server">next --></asp:LinkButton>
      </ContentTemplate> 
 </asp:UpdatePanel>
</form>

ASPX.VB 代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Open_database(1)
End Sub

Public Sub Open_database(e)
    Dim SQLConn As New SqlConnection()
    'The SQL Connection
    Dim SQLCmd As New SqlCommand()
    'The SQL Command
    Dim SQLdr As SqlDataReader
    'The Local Data Store
    SQLConn.ConnectionString = xxxx
    'Set the Connection String
    SQLConn.Open()
    'Open the connection
    SQLCmd.Connection = SQLConn
    'Sets the Connection to use with the SQL Command
    SQLCmd.CommandText = "Select * from Client where clientid='" & e & "'"
    'Sets the SQL String
    SQLdr = SQLCmd.ExecuteReader
    'Gets Data
    While SQLdr.Read()
        TextBox1.Text = (SQLdr("ClientID"))
    End While
    ' Do While SQLdr.NextResult()
    'Move to the Next Record
    SQLdr.Close()
    'Close the SQLDataReader        
    SQLConn.Close()
    'Close the connection
End Sub

Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
    Dim x = TextBox1.Text + 1
    Open_database(x)
End Sub

任何帮助将不胜感激,

吐温

4

1 回答 1

1

“避免使用共享变量”为了更好的编程......

只需Page_Load()将代码更改为

                     If Not IsPostBack Then
                            Open_database(1)
                     End If

使用上面的代码Page_Load()只会调用 Open_database(1) 函数一次并且LinkButton会给出想要的结果......

于 2013-04-01T13:26:48.330 回答