1

我是一名 AS400 程序员,被要求使用 vb.net 在 asp.net 中编写程序。我以前从未这样做过,并且在 Gridview 中填充列表框时遇到问题。我已经研究了这个主题好几天了,但是我在示例中找到的所有我尝试过的代码都不起作用。请原谅任何非常糟糕的代码,因为我对.net 如此陌生,我相信这可以写得更好。感谢您提供的任何帮助。网格是“AdjusterList”,列表框称为“MAICD”。我认为 Public Sub AdjusterList_RowDataBound 中的“失败/错误”代码。它在数据绑定之前的行给了我一个空异常错误;oCtrl.DataSource = oRs

我正在使用一位高级 .net 程序员给我的代码,他无法为这个新手提供更多帮助。仅供参考....

这是我的aspx。

     <div id="div1" runat="server">
        <asp:GridView ID="AdjusterList" runat="server" Width="1100px" 
             ClientIDMode="Static" AllowSorting="True"
             AutoGenerateColumns="False" 
             OnRowCommand="AdjusterList_RowCommand"
             OnRowEditing="AdjusterList_RowEditing" 
             OnRowUpdating="AdjusterList_RowUpdating" 
             OnRowDeleting="AdjusterList_RowDeleting" 
             OnRowCancelingEdit="AdjusterList_RowCancelingEdit" 
             OnRowDataBound="AdjusterList_RowDataBound"
             DataKeyNames="INSCD, INSSEQ" 
             ShowHeaderWhenEmpty="True" EditRowStyle-BackColor="#FF9900" PageSize="20" 
             EmptyDataText="NO RECORDS FOUND FOR THIS INSURER" 
             ShowFooter="True"EnableViewState="true">
             <EditRowStyle BackColor="#FF9900" />

             <RowStyle BackColor="White" ForeColor="Black" />
            <Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton id="btnedit" runat="server" CommandName="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton id="btnupdate" runat="server" CommandName="Update" Text="Save" />
<asp:LinkButton id="btncancel" runat="server" CommandName="Cancel" Text=Cancel"/>
</EditItemTemplate>

<FooterTemplate>
<asp:LinkButton id="btninsert" runat="server" CommandName="Insert" Text="Insert"/>
</FooterTemplate>
</asp:TemplateField> 


 <asp:TemplateField HeaderText ="Mail Code" ItemStyle-HorizontalAlign="Center">
 <ItemTemplate>
 <asp:Label ID="lblmaicd" runat="server" Text='<%# Bind("MAICD")%>'></asp:Label> 
 </ItemTemplate>
 <EditItemTemplate>
 <asp:ListBox ID="MAICD" runat="server" Rows="1"DataTextField="Text"
  DataValueField='<%# Bind("MAICD")%></asp:ListBox>
  </EditItemTemplate> 

  <FooterTemplate>
  <asp:ListBox ID="MAICD" runat="server" width="200" DataTextField="Text">
  </asp:ListBox>
  </FooterTemplate>
  </asp:TemplateField>
  </Columns>
  </asp:GridView> 

我后面的代码...

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class EditAdjusterData
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e AsSystem.EventArgs)    
     Dim sAdjuster As String = Session("Adjuster")
     Dim sSeqNo As String = Session("SeqNo")

    PopulateEdit(sAdjuster, sSeqNo, dateok)
    If IsPostBack Then
        'divEdit.Visible = False
        'divSelect.Visible = True
        'ShowForm()
    Else
        AdjusterList.DataBind()
        'divEdit.Visible = True
        'divSelect.Visible = False
    End If

End Sub




Protected Sub PopulateEdit(sValue As String, sValue2 As String, sValue3 As String)

    ' CALL TO PGRTSTLIB FILE INSP TO GET INSURER NAME


    ' 2ND CALL TO PGRTSTLIB FILE INSD 


    Dim oConn As New OleDbConnection()
    Dim sConn As String = ""
    Dim oCmd As New OleDbCommand()
    Dim oAdapter As New OleDbDataAdapter()
    Dim oRs As DataSet = New DataSet
    Dim sSql As String = ""

    sConn = "Provider=IBMDA400.DataSource.1; 
    oConn = New OleDb.OleDbConnection(sConn)
    oConn.Open()


    sSql = "SELECT INSCD, INSSEQ, MAICD, MAISEQ, (substr(char(EFFDT),5,2) || '-' || substr(char(EFFDT),7,2) || '-' || substr(char(EFFDT),1,4)) AS EFFDT, (substr(char(CANDT),5,2) || '-' || substr(char(CANDT),7,2) || '-' || substr(char(CANDT),1,4)) AS CANDT, ACCFIL FROM PGRTSTLIB.INSD WHERE INSCD = '" & sValue & "' "

    oCmd = New OleDbCommand(sSql, oConn)

    Session(INSCD) = INSCD
    Session(EFFDT) = EFFDT
    Session(MAICD) = MAICD
    Session("CANDT") = CANDT


    oCmd.CommandType = CommandType.Text
    oAdapter.SelectCommand = oCmd
    oAdapter.Fill(oRs, "Data")


    Dim sMailCode As String = " "
    Dim sMailSeq As String = " "

    Dim pRow As DataRow
    For Each pRow In oRs.Tables("Data").Rows
        sMailCode = pRow("MAICD").ToString()
    Next

    Session(sMailCode) = MAICD
    Session(MAICD) = sMailCode
    Session(sMailCode) = sMailCode

    AdjusterList.DataSource = oRs
    'AdjusterList.DataBind()

    'If sMailCode <> " " Then
    '    PopulateMailCode(sMailCode)
    'End If

    oRs.Dispose()
    oAdapter.Dispose()
    oCmd.Dispose()
    oConn.Dispose()

     End Sub




Protected Sub AdjusterList_PageIndexChanging(ByVal sender As Object, 
ByVal e As  System.Web.UI.WebControls.GridViewPageEventArgs) 
    AdjusterList.PageIndex = e.NewPageIndex

    AdjusterList.DataBind()


   End Sub




Protected Sub AdjusterList_RowCommand(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) 

End Sub



Protected Sub AdjusterList_RowCreated(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

 End Sub




Public Sub AdjusterList_RowDataBound(ByVal Sender As Object,
ByVal e As GridViewRowEventArgs) Handles AdjusterList.RowDataBound


    If e.Row.RowType = DataControlRowType.DataRow Then

        If e.Row.FindControl("MAICD") IsNot Nothing Then

            Dim MAICD As ListBox = e.Row.FindControl("MAICD")

            'If (e.Row.RowState And DataControlRowState.Edit) > 0 Then

            BindAjusterList()

        End If
    End If

End Sub




 Public Sub BindAjusterList()


    Dim oConn As New OleDbConnection()
    Dim sConn As String = ""
    Dim oCmd As New OleDbCommand()
    Dim oAdapter As New OleDbDataAdapter()
    Dim oRs As DataSet = New DataSet
    Dim sSql As String = ""


    sConn = "Provider=IBMDA400.DataSource.1; "
    oConn = New OleDb.OleDbConnection(sConn)
    oConn.Open()

    'sSql = "SELECT MAICD as Value,  MAICD  AS Text from PGRTSTLIB.INSM order by MAICD"
    sSql = "SELECT MAICD As Value, MAICD AS TEXT from PGRTSTLIB.INSM"
    oCmd = New OleDbCommand(sSql, oConn)

    'oCmd.Parameters.Add(New SqlParameter("@Type", Insurer))
    oCmd.CommandType = CommandType.Text
    oAdapter.SelectCommand = oCmd
    oAdapter.Fill(oRs, "ListBox")

    Dim oCtrl As ListBox
    oCtrl = AdjusterList.FindControl("MAICD")


    oCtrl.Items.Add(New ListItem("", ""))

    oCtrl.DataSource = oRs
    oCtrl.DataBind()

    oCtrl.Items.Insert(0, New ListItem(String.Empty, String.Empty))

    If Len(sValue) > 0 Then
        oCtrl.SelectedValue = sValue
    Else
        sValue = " "
    End If

    oRs.Dispose()
    oAdapter.Dispose()
    oCmd.Dispose()
    oConn.Dispose()
End Sub



Public Sub AdjusterList_RowEditing(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) 

 AdjusterList.EditIndex = e.NewEditIndex

 AdjusterList.DataBind()

End Sub



Protected Sub AdjusterList_RowUpdating(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) 

    ' Dim row As GridViewRow = DirectCast(SubsidaryList.Rows(e.RowIndex), GridViewRow)


Dim INSCD As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells (0).FindControl    ("INSCD"), TextBox).Text
    Dim INSSEQ As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(1).FindControl("INSSEQ"), TextBox).Text
    Dim MAICD As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(2).FindControl("MAICD"), ListBox).Text
    Dim MAISEQ As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(3).FindControl("MAISEQ"), TextBox).Text
    Dim EFFDT As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(4).FindControl("EFFDT"), TextBox).Text
    Dim CANDT As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(5).FindControl("CANDT"), TextBox).Text
    Dim ACCFIL As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(6).FindControl("ACCFIL"), TextBox).Text


    AdjusterList.EditIndex = -1
    AdjusterList.DataBind()



    ' New Data to DataBind to sql datasource and resend page after RECORD updated

           Response.Redirect("EditAdjusterData.aspx")

   End Sub





Protected Sub AdjusterList_RowCancelingEdit() Handles AdjusterList.RowCancelingEdit
    AdjusterList.EditIndex = -1
    AdjusterList.DataBind()
End Sub

 End Class

我对.net 很陌生,我可能无法完全理解每个示例。我认为高级 .net 程序员认为这很有趣:(

非常感谢,如果您能提供任何帮助,请随时与我联系。我只需要填充该死的列表框。我删除了我认为与此列表框问题无关的所有代码。

4

1 回答 1

0

在您的标记中,您应该将DataValueFieldListBox 的属性更改为“Value”字符串,因为它是您为查询中的属性提供的别名("SELECT MAICD As Value, MAICD AS TEXT from PGRTSTLIB.INSM")

<asp:ListBox ID="MAICD" runat="server" Rows="1"DataTextField="Text" DataValueField="Value"></asp:ListBox>

此外,RowDataBound如果您正在使用该方法检索该行的 ListBox 实例FindControl,但随后您将在该BindAjusterList()方法中再次检索它。尝试更改方法以接收找到的控件:

Public Sub AdjusterList_RowDataBound(ByVal Sender As Object, ByVal e As GridViewRowEventArgs) Handles AdjusterList.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.FindControl("MAICD") IsNot Nothing Then
            Dim MAICD As ListBox = e.Row.FindControl("MAICD")            
            BindAjusterList(MAICD)
        End If
    End If
End Sub

Public Sub BindAjusterList(ByVal oCtrl As ListBox)
   ' existing logic
   oCtrl.Items.Add(New ListItem("", ""))

   oCtrl.DataSource = oRs
   oCtrl.DataBind()
   ' existing logic
End Sub
于 2013-03-04T19:52:34.763 回答