目标:传递一个值,即SQL DB中的整数,并返回文本值
问题:要从Eval获取值到函数我似乎必须放入.tostring,这使得字符串不是数字。如果我尝试使用下面的convert.toint32,则不会传递任何值。我最后尝试使用.tostring。
检查:没有Eval我得到网格中显示的数字,当使用没有转换的.tostring传递数字时,因为字符串然后在搜索失败,因为我不能让字符串成为数字?!
我查看了各种论坛讨论,尤其是Converting value of an Eval from int to string
代码:
的.aspx
<%# RiskIDToText(Convert.ToInt32(Eval("RiskID")))%>
.aspx.vb
Public Function RiskIDToText(ByVal vRiskIDString As Integer) As Integer
Dim vString As String = ""
Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("SQL").ConnectionString
Dim dsNames As SqlDataSource
dsNames = New SqlDataSource
dsNames.ConnectionString = sConnString
Dim sSQL As String
sSQL = "SELECT RiskCategories,RiskCatID FROM TblRisks_Categories WHERE RiskCatID = '& vRiskIDString & '"
dsNames.SelectCommand = sSQL
For Each datarow As Data.DataRowView In dsNames.Select(DataSourceSelectArguments.Empty)
vString = datarow("RiskCategories").ToString
Next
Return vString
End Function
答案 0 :(得分:2)
尝试更改您的函数声明:
Public Function RiskIDToText(ByVal vRiskIDString As Integer) As String
dim vRiskIDStr= cstr(vRiskIDString) ''use vRiskIDStr in db query.
其余代码保持不变。
答案 1 :(得分:0)
的.aspx
<%# RiskIDToText((Eval("RiskID")))%>
.aspx.vb
Public Function RiskIDToText(ByVal vRiskIDString As String) As String
Dim vString As String = ""
Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("SQL").ConnectionString
Dim dsNames As SqlDataSource
dsNames = New SqlDataSource
dsNames.ConnectionString = sConnString
Dim sSQL As String
sSQL = "SELECT RiskCategories,RiskCatID FROM TblRisks_Categories WHERE RiskCatID = '" & vRiskIDString & "' "
dsNames.SelectCommand = sSQL
For Each datarow As Data.DataRowView In dsNames.Select(DataSourceSelectArguments.Empty)
vString = datarow("RiskCategories").ToString
Next
Return vString
End Function