回发后DropDownList所有值/索引丢失

时间:2012-06-11 15:28:00

标签: asp.net vb.net asp.net-2.0

我遇到了gridview和下拉列表的一个小问题。我可以让dropdownlist最初加载,但是当它自动回复时它返回没有值。我正在填充RowEditing子中的下拉列表。我猜我必须以某种方式在RowDataBound子中重新绑定,但不知道如何去做。如果我试图找到控件的SelectedValue,我什么都没有。

VB代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not IsPostBack Then
    bindGridView()
  End If
End Sub
'Menu Click

'bindGridView
Public Sub bindGridView(Optional ByVal sortExp As String = "", Optional ByVal sortDir As String = "")
  Dim strConnString As String = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString
  Dim conn As SqlConnection = New SqlConnection(strConnString)
  conn.Open()
  Dim strProgramNumber As String = 5
  Dim strRecordType As String = "Input Source"
  Dim strProgramInformation As String = "\\path\to\file"
  Dim sql As String
  Dim cmd As SqlCommand = New SqlCommand()
  cmd.Connection = conn
  If sortExp <> String.Empty Then
    sortExp = (sortExp & " " & sortDir).ToString
    sql = "SELECT tblPrgTrackPrograms.ProgramNumber, " & _
          "tblPrgTrackPrograms.ProgramName, " & _
          "tblPrgTrackPrograms.ProgramStatus, " & _
          "tblPrgTrackProgramDocumentation.RecordType, " & _
          "tblPrgTrackProgramDocumentation.ProgramInformation  " & _
          "FROM tblPrgTrackPrograms INNER JOIN tblPrgTrackProgramDocumentation ON " & _
          "tblPrgTrackPrograms.ProgramNumber = tblPrgTrackProgramDocumentation.ProgramNumber ORDER BY " & _
          "@sortExp"
    cmd.Parameters.AddWithValue("sortExp", sortExp)

  Else
    sql = "SELECT tblPrgTrackPrograms.ProgramNumber, " & _
          "tblPrgTrackPrograms.ProgramName, " & _
          "tblPrgTrackPrograms.ProgramStatus, " & _
          "tblPrgTrackProgramDocumentation.RecordType, " & _
          "tblPrgTrackProgramDocumentation.ProgramInformation  " & _
          "FROM tblPrgTrackPrograms INNER JOIN tblPrgTrackProgramDocumentation ON " & _
          "tblPrgTrackPrograms.ProgramNumber = tblPrgTrackProgramDocumentation.ProgramNumber"
  End If
  cmd.CommandText = sql
  Dim myDataSet As New DataSet()
  Dim mySQLAdapter As New SqlDataAdapter(cmd)
  mySQLAdapter.SelectCommand.Connection = conn
  mySQLAdapter.Fill(myDataSet)
  conn.Close()
  gvProgramDetails.DataSource = myDataSet
  gvProgramDetails.DataBind()
End Sub
'ProgramDetails Load
Protected Sub gvProgramDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProgramDetails.Load
  bindGridView()
End Sub
'ProgramDetails Paging
Protected Sub gvProgramDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProgramDetails.PageIndexChanging
  gvProgramDetails.PageIndex = e.NewPageIndex
  bindGridView()
End Sub
'ProgramDetails Sorting
Protected Sub gvProgramDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProgramDetails.Sorting
  Dim SortDirection As String
  If Session("SortDirection") = vbNullString Then
    Session("SortDirection") = "DESC"
  Else
    SortDirection = Session("SortDirection").ToString
    If SortDirection = "ASC" Then
        SortDirection = "DESC"
    ElseIf SortDirection = "DESC" Then
        SortDirection = "ASC"
    Else
        SortDirection = "ASC"
    End If
    bindGridView(e.SortExpression, Session("SortDirection"))
    'Need to store sort info in view state    
    Session("SortDirection") = SortDirection
  End If
End Sub

'ProgramDetails RowEditing
Protected Sub gvProgramDetails_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvProgramDetails.RowEditing
  Dim lbl As Label = CType(gvProgramDetails.Rows(e.NewEditIndex).FindControl("lblRecordType"), Label)
  Dim strValue As String = lbl.Text
  gvProgramDetails.EditIndex = e.NewEditIndex
  bindGridView()
  Dim row As GridViewRow = gvProgramDetails.Rows(e.NewEditIndex)
  Dim strConnString As String = ConfigurationManager.ConnectionStrings("CSPaperWEBConnectionString").ConnectionString
  Dim ddlRecordType As DropDownList = CType(row.FindControl("ddlRecordType"), DropDownList)
  Dim conn As SqlConnection = New SqlConnection(strConnString)
  conn.Open()
  Dim sql As String
  Dim cmd As SqlCommand = New SqlCommand()
  cmd.Connection = conn
  sql = "select RecordType from [tblPrgTrackValidRecordTypes] "
  cmd.CommandText = sql
  Dim myDataSet As New DataSet()
  Dim mySQLAdapter As New SqlDataAdapter(cmd)
  mySQLAdapter.SelectCommand.Connection = conn
  mySQLAdapter.Fill(myDataSet)
  conn.Close()
  If ddlRecordType IsNot Nothing Then
    ddlRecordType.DataTextField = "RecordType"
    ddlRecordType.DataValueField = "RecordType"
    ddlRecordType.DataSource = myDataSet
    ddlRecordType.SelectedValue = strValue
    ddlRecordType.DataBind()
  End If
End Sub
Protected Sub gvProgramDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProgramDetails.RowDataBound
  If e.Row.RowType = DataControlRowType.DataRow Then
    Dim ctrl As Control = e.Row.FindControl("ddlRecordType")
    If ctrl IsNot Nothing Then
      Dim ddlRecordType As DropDownList = ctrl
      MsgBox(ddlRecordType.SelectedValue)

    End If
  End If
End Sub
'ProgramDetails RowUpdating
Protected Sub gvProgramDetails_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvProgramDetails.RowUpdating
  Dim ddlRecordType As DropDownList = gvProgramDetails.Rows(e.RowIndex).FindControl("ddlRecordType")
  MsgBox(ddlRecordType.SelectedValue)
  gvProgramDetails.EditIndex = -1
  bindGridView()
End Sub
'ProgramDetails RowCancelingEdit
Protected Sub gvProgramDetails_RowCancelingEdit1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvProgramDetails.RowCancelingEdit
  gvProgramDetails.EditIndex = -1
  bindGridView()
End Sub
'ProgramDetails RowDeleting
Protected Sub gvProgramDetails_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProgramDetails.RowDeleting
  Dim strConnString As String = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString
  Dim conn As SqlConnection = New SqlConnection(strConnString)
  conn.Open()
  Dim strProgramNumber As String = 5
  Dim strRecordType As String = "Input Source"
  Dim strProgramInformation As String = "\\path\to\file"
  Dim sql As String = "delete from tblPrgTrackProgramDocumentation where ProgramNumber = @ProgramNumber and RecordType = @RecordType and ProgramInformation = @ProgramInformation"
  Dim cmd As SqlCommand = New SqlCommand()
  cmd.Connection = conn
  cmd.CommandText = sql
  cmd.Parameters.AddWithValue("ProgramNumber", strProgramNumber)
  cmd.Parameters.AddWithValue("RecordType", strRecordType)
  cmd.Parameters.AddWithValue("ProgramInformation", strProgramInformation)
  cmd.ExecuteNonQuery()
  cmd.Dispose()
  bindGridView()
End Sub

ASP前端

<ajx:UpdatePanel ID="ajaxpanel" runat="server">   
  <ContentTemplate>        
    <asp:GridView ID="gvProgramDetails" runat="server" AutoGenerateColumns="False" 
    CssClass="gridview" DataKeyNames="ProgramNumber" AllowPaging="True" PageSize="3" AllowSorting="True" >
      <Columns>
        <asp:TemplateField ShowHeader="False">
          <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
            CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>
          </ItemTemplate>
          <EditItemTemplate>
          </EditItemTemplate>
          <ControlStyle CssClass="button delete" />
        </asp:TemplateField>
        <asp:CommandField ControlStyle-CssClass="button save" ShowEditButton="True">
          <ControlStyle CssClass="button save" />
        </asp:CommandField>
        <asp:BoundField DataField="ProgramNumber" HeaderText="ProgramNumber" 
          InsertVisible="False" ReadOnly="True" SortExpression="ProgramNumber" />
        <asp:BoundField DataField="ProgramName" HeaderText="ProgramName" ReadOnly="True"
          SortExpression="ProgramName" />
        <asp:BoundField DataField="ProgramStatus" HeaderText="ProgramStatus" ReadOnly="True"
          SortExpression="ProgramStatus" />
        <asp:TemplateField HeaderText="RecordType" SortExpression="RecordType">
          <EditItemTemplate>
            <asp:DropDownList ID="ddlRecordType"  runat="server" autopostback="True">
            </asp:DropDownList>
          </EditItemTemplate>
          <ItemTemplate>
            <asp:Label ID="lblRecordType" runat="server" Text='<%# Bind("RecordType") %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ProgramInformation" HeaderText="ProgramInformation" 
          SortExpression="ProgramInformation" />
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:WEBConnectionString %>"
    SelectCommand="SELECT * FROM [tblPrgTrackValidRecordTypes]"></asp:SqlDataSource>
  </ContentTemplate>
</ajx:UpdatePanel>

对于那些回答从工具箱中拖动控件的人,创建数据源,并设置bind(“RecordType”)请不要。我已经尝试过这种方式,并且值始终会以Recordtype的形式发回。因此,除非你能解决that version of this gridview,否则不要使用拖放控制解决方案。我正在挠挠脑袋来解决这个问题。


更新


我是在App_Code Process / ddlRecordType.vb

下创建的
Imports Microsoft.VisualBasic
Namespace processes.ProgramTrack.dllRecordType
   Public Class ddlRecordType
        Public Sub ddlRecordType()

        End Sub
        Public Function GetRecords() As DataSet
            Dim conn As New SqlConnection
            Dim cmd As New SqlCommand
            conn.ConnectionString = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString.ToString
            cmd.CommandText = "select RecordType from [tblPrgTrackValidRecordTypes] "
            cmd.Connection = conn
            Dim myDataSet As New DataSet()
            Dim mySQLAdapter As New SqlDataAdapter(cmd)
            mySQLAdapter.Fill(myDataSet)
            Return myDataSet
        End Function
    End Class
End Namespace

我的标记看起来像这样。

<asp:DropDownList ID="ddlRecordType" DatasourceID="odsRecordType" DataTextField="RecordType" DataValueField="RecordType" runat="server" autopostback="True"   >                          </asp:DropDownList>                                     
<asp:ObjectDataSource ID="odsRecordType" runat="server" TypeName="processes.ProgramTrack.dllRecordType.ddlRecordType" SelectMethod="GetRecords"></asp:ObjectDataSource> 

然后我遇到了与之前相同的问题,关于DDL没有在回发时保持它的值。我应该开始一个新的问题还是继续这个问题?

更新以修复不维护的DDL。禁用Gridview的Viewstate。

<asp:GridView ID="gvProgramDetails" runat="server" AutoGenerateColumns="False" CssClass="gridview"DataKeyNames="ProgramNumber" AllowPaging="True" PageSize="10" EnableViewState="False" AllowSorting="True">

1 个答案:

答案 0 :(得分:0)

尝试为DDL创建objectDataSource,然后使用它。问题是DDL必须在页面加载时,或在页面初始化时,或通过DataSource控件初始化。如果你没有使用Ajax UpdatePanel(继续,把它拿出来,看看你的代码是否正常工作,无论如何都应该这样做)那么你就可以这样做了。

如果您要引入一个ObjectDataSource(并且您也可以将参数传递给它),那么您应该得到您想要的结果。

编辑:

我将从我的项目中提供代码,以便您可以看到我是如何使用它的。此代码不能完美满足您的需求,但会告诉您如何做您想做的事。

namespace Appropriate.Namespace.Here {
  public class MyType {
    public List<KeyValuePair<string, string>> GetRoles() {
      List<KeyValuePair<string, string>> l = new List<KeyValuePair<string, string>>();

      l.Add( new KeyValuePair<string, string>( "Level1", "Analyst" ) );
      l.Add( new KeyValuePair<string, string>( "Level2", "Customer Service" ) );
      l.Add( new KeyValuePair<string, string>( "Level3", "Customer Service Manager" ) );
      l.Add( new KeyValuePair<string, string>( "Level4", "Full-Access User" ) );
      l.Add( new KeyValuePair<string, string>( "Level5", "Super User" ) );

      return l;
    }
  }
}

<asp:DropDownList ID="cmbRoles" runat="server" AutoPostBack="False" DataSourceID="odsRoles" DataTextField="Value" DataValueField="Key" />
<asp:ObjectDataSource ID="odsRoles" runat="server" TypeName="Appropriate.Namespace.Here.MyType" SelectMethod="GetRoles" />

注意命名空间和typename如何协同工作以给我SelectMethod?我没有提供选择参数的覆盖,尽管你可以。您可以在页面支持代码中执行此操作,我也可以对此提供一些见解,但我不会完全过于复杂。

注意我是如何从方法中返回List的?而我只是在那种方法中定义它?你可以在那里轻松地进行数据库调用。