2013-03-01 47 views
-3
Dim objItems As clsItems 

'Loads the pages with the Gridview and Infomation pretaining to the Item selected 
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 

    Dim qryItemNum As String = Request.QueryString("qryItemNum") 

    'Populates Gridview 
    If Not Page.IsPostBack Then 
     gvwCategorySelect.DataSource = clsCategory.GetList 
     gvwCategorySelect.DataBind() 

     'imgbtnPrev.Visible = False 

     'If Not String.IsNullOrEmpty(qryItemNum) Then 
     ' Dim ItemNum As Int32 
     ' If Int32.TryParse(test, ItemNum) Then 
     '  imgbtnPrev.Visible = (-1 < ItemNum) 
     ' End If 
     'End If 
    End If 


    objItems = New clsItems(qryItemNum) 

    'Set up the from labels 
    lblTitle.Text = objItems.Title 
    lblPrice.Text = objItems.Price.ToString("C") 
    lblDescription.Text = objItems.Description 
    ImgItem.ImageUrl = "~/images/ItemImages/Item" & objItems.ItemNum & ".jpg" 



End Sub 

'Returns to pervious item 
Protected Sub imgbtnPrev_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgbtnPrev.Click 

    Response.Redirect("~/ItemInfo.aspx?qryItemNum=" & objItems.ItemNum - 1) 

End Sub 

'Next item 
Protected Sub imgbtnNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgbtnNext.Click 


    Response.Redirect("~/ItemInfo.aspx?qryItemNum=" & objItems.ItemNum + 1) 

End Sub 

在上面的代碼中有2個圖像按鈕,它們在不同的ItemNum之間移動。我試着從點擊後退按鈕停止用戶,通過移除imgbtnPrev按鈕時,在1我需要驗證在asp.net中的查詢字符串

錯誤信息的查詢字符串說,有在位置沒有行0

dataRowObject = DBMethods.CreateTable(sqlString, sqlArg).Rows(0) 

^這裏是哪裏是其突出顯示。

+1

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Joe 2013-03-01 20:09:35

+0

這是什麼問題? – 2013-03-01 20:14:00

+0

只需使用Request.QueryString獲取查詢字符串值即可。並做你想要的 – Sachin 2013-03-01 20:14:29

回答

1

試試這個

int number; 
    if(Request.QueryString["qryItemNum"]!= null) 
    { 
    bool isNumeric = int.TryParse(Request.QueryString["qryItemNum"].ToString(), out number); 
     if(isNumeric == true) 
     { 
     if(Convert.ToInt(Request.QueryString["qryItemNum"])<=1) 
     { 
      imgbtnPrev = false; 
     } 
     } 
    } 

編輯: 我轉換,爲VB.NET

Dim number As Integer 
    If Request.QueryString("qryItemNum") IsNot Nothing Then 
Dim isNumeric As Boolean = Integer.TryParse(Request.QueryString("qryItemNum").ToString(), number) 
    If isNumeric = True Then 
    If Convert.ToInt(Request.QueryString("qryItemNum")) <= 1 Then 
     imgbtnPrev = False 
    End If 
    End If 
    End If 
+0

這看起來像做到這一點(除查詢字符串2次之外),爲什麼downvote? – jp2code 2013-03-01 20:17:06

+0

請評價,當你downvote。 – 2013-03-01 20:18:17

+0

我因爲沒有錯誤檢查而低估了。非數字查詢字符串的值將炸燬......也是錯誤的語言....還有其他問題會阻止它編譯。 – MikeSmithDev 2013-03-01 20:18:45

1

我覺得這是VB是怎麼做的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     imgbtnPrev = False 
     Dim qryItemNum As String = Request.QueryString("qryItemNum") 
     If Not String.IsNullOrEmpty(qryItemNum) Then 
      Dim ItemNum As Int32 
      If Int32.TryParse(qryItemNum, ItemNum) Then 
      imgbtnPrev = (-1 < ItemNum) 
      End If 
     End If 
    End If 
End Sub 

這是假設,當然,imgbtnPrev是有些定義的在你的代碼或你的ASPX頁面中。

編輯:從您的評論imgbtnPrev.Visible = False到你原來的問題之一,看來imgbtnPrev是表單上的一個按鈕。如果是這樣的話,你會寫是這樣的:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     imgbtnPrev.Visible = False 
     Dim qryItemNum As String = Request.QueryString("qryItemNum") 
     If Not String.IsNullOrEmpty(qryItemNum) Then 
      Dim ItemNum As Int32 
      If Int32.TryParse(qryItemNum, ItemNum) Then 
      imgbtnPrev.Visible = (-1 < ItemNum) 
      End If 
     End If 
    End If 
End Sub 

EDIT2:該版本採用Try...Catch,看看有什麼錯誤是:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     imgbtnPrev.Visible = False 
     Try 
      Dim qryItemNum As String = Request.QueryString("qryItemNum") 
      If Not String.IsNullOrEmpty(qryItemNum) Then 
      Dim ItemNum As Int32 
      If Int32.TryParse(qryItemNum, ItemNum) Then 
       imgbtnPrev.Visible = (-1 < ItemNum) 
      End If 
      End If 
     Catch err As Exception 
      Response.Write(err.Message) 
     End Try 
    End If 
End Sub 

希望它能幫助!

+0

syed首先回答,但由於問題被標記爲「vb.net」,我添加了這個。不過,我的VB並不是最好的。 – jp2code 2013-03-01 20:23:17

+0

而不是使用「qryItemNum」。該值通過objItem.ItemNum傳遞,我可以用它替換它嗎? – user2011977 2013-03-01 20:33:24

+0

在「用[objItem.ItemNum]替換[Request.QueryString]?」這不會從查詢字符串中的URL中讀取任何內容,但會允許您從「objItem」中讀取和/或轉換值。 – jp2code 2013-03-01 20:37:20