2010-05-29 61 views
0

我试图在我的代码隐藏中将活动的标识(RefNum)传递给Sub。我知道我应该在将参数传递给子例程和方法时使用括号,并且我尝试了很多方法并且一直收到以下错误:在Codebehind中将参数传递给子例程

BC30203:预期标识符。

我在前端硬编码它只是为了让它通过[OnDataBound =“FillSectorCBList(”“”WK.002“”“)”],但它显然是错误的。 :(

前端:

<asp:DetailsView ID="dvEditActivity" AutoGenerateRows="False" DataKeyNames="RefNum" OnDataBound="dvSectorID_DataBound" OnItemUpdated="dvEditActivity_ItemUpdated" DataSourceID="dsEditActivity" > 
    <Fields> 
     <asp:TemplateField> 
       <ItemTemplate> 
        <br /><span style="color:#0e85c1;font-weight:bold">Sector</span><br /><br /> 
        <asp:CheckBoxList ID="cblistSector" runat="server" DataSourceID="dsGetSectorNames" DataTextField="SectorName" DataValueField="SectorID" OnDataBound="FillSectorCBList("""WK.002""")" ></asp:CheckBoxList> 
        <%-- Datasource to populate cblistSector --%> 
        <asp:SqlDataSource ID="dsGetSectorNames" runat="server" ConnectionString="<%$ ConnectionStrings:dbConn %>" ProviderName="<%$ ConnectionStrings:dbConn.ProviderName %>" SelectCommand="SELECT SectorID, SectorName from Sector ORDER BY SectorID"></asp:SqlDataSource> 
       </ItemTemplate> 
       </asp:TemplateField> 
    </Fields> 
</asp:DetailsView> 

代码隐藏:

子FillSectorCBList(BYVAL引用句柄作为字符串,BYVAL发件人作为对象,BYVALË作为System.EventArgs) 昏暗SectorIDs作为新列表项

Dim myConnection As String = ConfigurationManager.ConnectionStrings("dbConn").ConnectionString() 
    Dim objConn As New SqlConnection(myConnection) 
    Dim strSQL As String = "SELECT DISTINCT A.RefNum, AS1.SectorID, S.SectorName FROM Activity A LEFT OUTER JOIN Activity_Sector AS1 ON AS1.RefNum = A.RefNum LEFT OUTER JOIN Sector S ON AS1.SectorID = S.SectorID WHERE A.RefNum = @RefNum ORDER BY A.RefNum" 
    Dim objCommand As New SqlCommand(strSQL, objConn) 
    objCommand.Parameters.AddWithValue("RefNum", RefNum) 

    Dim ad As New SqlDataAdapter(objCommand) 

    Try 
     [Code] 
    Finally 
     [Code] 
    End Try 

    objCommand.Connection.Close() 
    objCommand.Dispose() 
    objConn.Close() 
End Sub 

任何意见将是巨大的。我不知道如果我甚至有正确的方法。

谢谢!

回答

1

您无法将参数传递给OnDataBound事件方法。该方法必须遵循特定的签名。由于这个参数本质上是一个程序常量,所以我只是将它编码到方法中或从某个地方读取它。当然,您也可以使用处理事件的方法调用接受参数的不同方法。

1

更改

OnDataBound="FillSectorCBList("""WK.002""")"

OnDataBound='FillSectorCBList("WK.002")' 

虽然乔尔公布之前,我可以提交此将采取标识预期的错误照顾这很可能会导致另一种。

0

当其他人发布时,您是否无法使用OnDataBound事件来达到此目的。您需要将DataSource属性设置为您所需的方法。

DataSource='FillSectorCBList("WK.002")' 

请注意,您无法同时设置DataSource和DataSourceID属性。您可能也需要手动执行.DataBind()。