0

我在使用gridview 和下拉列表时遇到了一个小问题。我可以让下拉列表最初加载,但是当它自动回发时,它返回时没有任何值。我正在填充 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 是什么,该值总是会发回。所以除非你能解决这个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 不维护。禁用网格视图的视图状态。

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

1 回答 1

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" />

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

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

于 2012-06-11T15:51:31.477 回答