asp.net下拉列表重置

时间:2013-07-09 18:31:45

标签: html asp.net vb.net html-select

我有一个奇怪的问题。我有一个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

由于

1 个答案:

答案 0 :(得分:1)

你只需要检查你的pageload事件的回发。

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

    If Me.IsPostBack Then Return

End Sub
相关问题