2013-03-04 64 views
1

我是一個AS400程序員,要求使用vb.net在asp.net中編寫一個程序。我從來沒有這樣做過,我在GridView中填充列表框有問題。我已經研究了幾天的這個主題,但是我嘗試過的所有代碼在示例中都找不到。請原諒任何真正糟糕的代碼,因爲我對.net是如此的陌生,我相信這可以寫得更好。我感謝您可能提供的任何幫助。網格是「AdjusterList」,列表框被稱爲「MAICD」。我認爲 Public Sub AdjusterList_RowDataBound中的'失敗/錯誤'代碼。它在數據綁定之前給我一個空的異常錯誤; oCtrl.DataSource = oRs列表框綁定Gridvew內部故障

我使用的是一位資深.net程序員給我的代碼,他無法爲這個新手提供更多的幫助。 Just Fyi ....

這是我的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程序員認爲這很有趣:(

非常感謝,請隨時與我聯繫,我可以提供任何幫助我只需要darn列表框填充我刪除了任何代碼我認爲無關,這個列表框問題

+1

@ MarcusVinicius的回答看起來不錯。我只是想建議你查看'AdjusterList_RowUpdating'中的參數'e'的'OldValues'和'NewValues'屬性。通過引用'OldValues'和'NewValues',你可以節省你所有與'FindControl'一起工作的東西。和我打個招呼吧。 – 2013-03-05 03:35:18

回答

0

在您的標記,你應該將ListBox的DataValueField屬性更改爲「值」的字符串,因爲它是你給的財產在查詢("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事件中,您正在檢索列表使用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