0

我有一个奇怪的问题。我有一个 asp.net 页面,其中有一个下拉列表/组合框,它是数据绑定到 SQL 表的。下拉列表设置为显示主键字段,它只是一个从 1 开始的递增数字。

我以前让整个表格工作得很好,但现在我遇到了一些问题。我已将问题缩小到下拉列表、一个按钮和按钮的 onClick 事件。当我单击按钮时,下拉列表重置为数字 2。目前我的 sql 表中只有 3 个项目,因此我的下拉列表中只有 3 个数字(1、2、3)。如果我将列表设置为 1 并单击按钮,它会停留在 1 号。如果我将其设置为 2,它会停留在 2 号。如果我将其设置为 2 以上的任何值,它总是重置为 2。我实际上已经删除了所有行单击子中的代码,因此按钮单击实际上什么都不做,但如果所选项目高于该值,它仍会重置为 2。

这是我的 HTML 代码。如果问题出在其他地方,我会将其全部包括在内:

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="SGC.aspx.vb" Inherits="sgc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
  .auto-style1 {
        width: 181px;
    }
    .auto-style2 {
        width: 1px;
    }
    .auto-style3 {
        width: 14px;
    }
    .auto-style4 {
    }
    .auto-style5 {
        height: 23px;
    }
    .auto-style6 {
        width: 181px;
        height: 23px;
    }
    .auto-style7 {
        width: 4px;
    }
    .auto-style10 {
        width: 182px;
        height: 23px;
    }
    .auto-style11 {
        width: 302px;
        height: 23px;
    }
    .auto-style13 {
        width: 14px;
        height: 23px;
    }
</style>
</head>
<body style="background-image:URL(/idle_screen.jpg);">

<form id="form1" runat="server">
<div>
    <table align="center" style="width: 100%;">
    <tr>
        <td class="auto-style6" ></td>
        <td class="auto-style5" bgcolor="#FF0000" colspan="3" align="center">
            <asp:Label ID="Label2" runat="server" Font-Names="Arial Black" Font-Size="Large" ForeColor="#003399" Text="SGC Message Log"></asp:Label>
        </td>
        <td class="auto-style13"></td>
    </tr>
    <tr>
        <td class="auto-style1">&nbsp;</td>
        <td class="auto-style4">
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="select sub_id 'ID #', race 'Race', orgMsg 'Original Message', Case when replyMsg like '%' then 'See Reply' else 'Reply' end as Reply, androidID from messages"></asp:SqlDataSource>
        </td>
        <td>&nbsp;</td>
        <td class="auto-style2">&nbsp;</td>
        <td class="auto-style3">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style6"></td>
        <td class="auto-style7" colspan="3" rowspan="2">
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" align="center" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="ID #" DataSourceID="SqlDataSource2" EnableModelValidation="True" GridLines="Vertical" Width="1400px">
                <AlternatingRowStyle BackColor="#DCDCDC" />
                <Columns>
                    <asp:BoundField DataField="ID #" HeaderText="ID #" InsertVisible="False" ReadOnly="True" SortExpression="ID #" />
                    <asp:BoundField DataField="Race" HeaderText="Race" SortExpression="Race" />
                    <asp:BoundField DataField="Original Message" HeaderText="Original Message" SortExpression="Original Message" />
                    <asp:BoundField DataField="Reply" HeaderText="Reply" ReadOnly="True" SortExpression="Reply" />
                    <asp:BoundField DataField="androidID" HeaderText="androidID" SortExpression="androidID" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            </asp:GridView>

        </td>
        <td class="auto-style10"></td>
    </tr>
    <tr>
        <td class="auto-style6"></td>
        <td class="auto-style10"></td>
    </tr>
    <tr>
        <td class="auto-style6"></td>
        <td class="auto-style11"></td>
        <td class="auto-style5"></td>
        <td class="auto-style5"></td>
        <td class="auto-style5"></td>
    </tr>
    <tr>
        <td class="auto-style6">&nbsp;</td>
        <td class="auto-style4" bgcolor="#FF0000" align="center" colspan="3">
          <asp:Button ID="Button1" runat="server" OnClick="MyButton_Click" Text="Refresh" Width="175px" ForeColor="#003399" />
          </td>
        <td class="auto-style10">&nbsp;</td>
    </tr>
</table>
    <asp:Panel ID="Panel1" runat="server" Width="902px">

    </asp:Panel>
      <asp:Panel ID="Panel2" runat="server">
          &nbsp;&nbsp;
          <asp:Label ID="Label1" runat="server" Text="Label" Enabled="False" Visible="False"></asp:Label>
        </asp:Panel>

</div>
<div>
<asp:DropDownList ID="deviceList" runat="server" DataSourceID="SqlDataSource2" DataTextField="ID #" DataValueField="androidID"  AppendDataBoundItems="true">

</asp:DropDownList>
<br /><br />
<asp:TextBox ID="txtPayload" runat="server" Width="750px" Height="120px" TextMode="MultiLine"></asp:TextBox>
<br /><br />
<asp:Button ID="btnPush" runat="server" Text="Send" OnClick="sendtxt" />
<asp:Label ID="lblResponse" runat="server" Text=""></asp:Label>
</div></form>
</body>
</html>

我没有包含任何我的视觉基本代码,因为它似乎没有任何效果。如果有人认为需要它,我很乐意发布它。有谁知道为什么我的列表中选定的索引会在单击一个不执行任何操作的按钮时重置(但仅高于 2)?

更新

这是我的整个 VB 代码隐藏文件。我已经把所有东西都放回了它应该在的地方。

Imports System.Net
Imports System.IO
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient

Public Class sgc
Inherits System.Web.UI.Page

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

End Sub

Protected Sub MyButton_Click(sender As Object, e As EventArgs)

    ''''    
    Response.Redirect(Request.RawUrl)

End Sub



Private Function SendNotification(ByVal authstring As String) As String

    Dim request As WebRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    request.Headers.Add(String.Format("Authorization: key={0}", authstring))
    Dim collaspeKey As String = Guid.NewGuid().ToString("n")
    Dim deviceList As DropDownList = FindControl("deviceList")
    Dim DGridView As GridView = FindControl("GridView1")
    Dim txtPayload As TextBox = FindControl("txtPayload")
    Dim i As Integer = Convert.toInt32(deviceList.Selecteditem.text)
    Dim reply As String = txtPayload.Text
    Dim race As String = DGridView.Rows(i).Cells(1).Text
    Dim orgm As String = DGridView.Rows(i).Cells(2).Text

    Dim payload As String = race + ";" + orgm + ";" + reply
    Dim postData As String = String.Format("registration_id={0}&data.payload={1}&collapse_key={2}", deviceList.SelectedValue, payload, collaspeKey)
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    reader.Close()
    dataStream.Close()
    response.Close()

    Return responseFromServer
End Function

Protected Sub sendtxt(sender As Object, e As System.EventArgs)
    Dim txtPayload As TextBox = FindControl("txtPayload")
    Dim lblResponse As Label = FindControl("lblResponse")
    Dim deviceList As DropDownList = FindControl("deviceList")

    lblResponse.Text = "Message Sent. Confirmation " + SendNotification("My Auth Key")


    txtPayload.text = ""

End Sub
End Class

更新 2

我已经更改了我的代码以在 VB 中自己完成所有绑定。这似乎仍然没有帮助:

 If Not Page.IsPostBack Then

        Dim sqlds As SqlDataSource = New SqlDataSource()
        sqlds.ID = "SqlDataSource3"
        Me.Page.Controls.Add(sqlds)
        sqlds.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString2").ConnectionString
        sqlds.SelectCommand = "select sub_id 'ID #', race 'Race', orgMsg 'Original Message', Case when replyMsg like '%' then 'See Reply' else 'Reply' end as Reply, androidID from messages"
        GridView1.DataSource = sqlds
        GridView1.DataBind()
        deviceList.DataSourceID = "SqlDataSource2"
        deviceList.DataTextField = "ID #"
        deviceList.DataValueField = "androidID"
        deviceList.DataBind()

    End If

谢谢

4

1 回答 1

1

您只需要在您的页面加载事件中检查回发即可。

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

    If Me.IsPostBack Then Return

End Sub
于 2013-07-10T10:04:52.220 回答