2

I have checkbox control on gridview with the ability to check all and uncheck all.

The page also uses pagination. Each page has 25 records. Of course anymore more goes to the next page.

A user checks one or more checkboxes and user's selections are processed using the code below:

Dim uItems As String = String.Empty 

For Each r As GridViewRow In GridView1.Rows 

    If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then 

        If uItems <> String.Empty Then 

            uItems += "," 

        End If 

        uItems += "http://default.html?gen=" & r.Cells(1).Text & "&NO=3&F=1" 

    End If 

Next 

If a user checks 15 or less, then you get this:

http://default.html?gen=" & r.Cells(1).Text & "&NO=3&F=1 this works because you get as many as you checked.

The issue we are having currently is that if a user checks more than 15 checkboxes, we get

"Internet Explorer cannot display webpage; what you can try - diagnose connection..."

After several troubleshooting, we discovered that the reason it is breaking is we could pass more than 15 values from cell(1) to the that link.

Does anyone know of a workaround to this?

This was exactly the same problem I posted yesterday except that I was describing it incorrectly, thereby making it difficulty for experts to give the correct solution.

Thanks for your help.

Kenny
  • 1,058
  • 4
  • 20
  • 49
  • 1
    `http://default.html` is this your REAL url? or did you remove your host (for privacy concerns) and not include "example.com" or something? If this is really the URL you're using, then it's not valid. – hometoast Aug 10 '12 at 14:31
  • Your URL appears to be completely wrong: http://default.html isn't a valid domain name at all. Also, what does your URL look like with just 1 value selected, I'm specifically interested in what comma delimited data you're assigning to the "gen" key in query string. – Paul Aldred-Bann Aug 10 '12 at 14:32
  • 2
    It sounds like the url you are generating is too long. – hometoast Aug 10 '12 at 14:33
  • 1
    I'm not really sure what the issue is. Please provide more detail on the error you are receiving. On a side note, you should append "&NO=3&F=1" after the loop, so it doesn't include duplicate query string key/value pairs. – NoAlias Aug 10 '12 at 14:34
  • @NoAlias, et al, the error is that Internet explorer cannot display the webpage, no status code, nothing. The urls are generating correctly. They may be too long and that's the problem. I am looking for a workaround – Kenny Aug 10 '12 at 14:57

4 Answers4

4

I think it's the same issue I had once: the URL you're building is too long and the request cannot be processed properly.

If you need to send a very long sequence of data in the URL you should switch to a POST request (in place of a GET request), thus removing the data from the querystring

Another option is that you're appending URL parameters with the same key.

Community
  • 1
  • 1
Alex
  • 23,004
  • 4
  • 39
  • 73
  • the URLs are not getting the same key and shouldn't. Pls see my comment to hometest. Thanks – Kenny Aug 10 '12 at 14:53
2

Sounds like you might be running into the limit on the length of a url (see here - you might try a different browser to confirm). How long is the text you are retrieving from each cell?

If this is the issue, then you might want to POST instead of GET (which is what the query string is doing) to your server, or figure out a way to shortner the query string.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • you are correct. That's the issue. I thought that using the maxurllength property of httpRuntime will solve this. So far, total character is like 2089. How do I switch to POST from GET? – Kenny Aug 10 '12 at 14:55
1

Using the code you posted, if you have more than one item checked your url will looks something like

http://default.html?gen=sometext&NO=3&F=1,http://default.html?gen=sometext&NO=3&F=1,http://default.html?gen=sometext&NO=3&F=1

I don't think this is what you intended.

try

Dim uItems As String = String.Empty  

For Each r As GridViewRow In GridView1.Rows  
    If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then  
        If uItems <> String.Empty Then  
           uItems += ","  
        End If  
        uItems +=  r.Cells(1).Text  
    End If  
Next  
uItems = "http://default.html?gen=" & uItems & "&NO=3&F=1"

That should return something more like

http://default.html?gen=sometext,itemblah,moretext&NO=3&F=1

hometoast
  • 11,522
  • 5
  • 41
  • 58
  • I am very grateful for the quick (many responses). To answer the first question, the default.html is dummy for privacy concerns, although the actual is still .html. Second, when I debug it, the code is generating the correct urls. For instance, it is getting something similar to what hometoast posted. Also, I have already added maxurllenght property of httpRuntime in config file. Finally, how do I change it to Get instead of Post? – Kenny Aug 10 '12 at 14:44
  • But the code you provided will still include `http://host/default.html?gen=` as many times as you have checked boxes. – hometoast Aug 10 '12 at 14:46
  • that's correct, except that ?gen= will have different values and that's the intention. The reason is we are passing those URLs to another page which processes them and extracts the contents of the URLs. – Kenny Aug 10 '12 at 14:51
  • OH! I understand now. You actually do NEED the urls. In that case, go with one of the answers that suggests you use POST data instead of GET. your URL is just going to be too long. – hometoast Aug 10 '12 at 15:02
1

I suspect that you are generating a query string that is too long but there are other issues with the code. I've changed it to use a StringBuilder and save repeated instantiation of Strings.

Dim uItems As New StringBuilder("http://default.html?gen=")

For Each r As GridViewRow In GridView1.Row
    If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then
        uItems.AppendFormat("{0},", r.Cells(1).Text)
    End If
End For

-- Remove trailing delimiter
uItems.Remove(uItems.Length - 1, 1)

uItems.Append("&NO=3&F=1")

With this code uItems.ToString() will give you somthing like this

http://default.html?gen=bla1,bla2,bla3&NO=3&F=1

it may be that you actually want somthing like this.

Dim uItems As New StringBuilder("http://default.html?")

Dim checkCount = 0
For Each r As GridViewRow In GridView1.Row
    If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then
        uItems.AppendFormat("g{0}={1}&", checkCount, r.Cells(1).Text)
        checkCount += 1
    End If
End For

-- Remove trailing delimiter
uItems.Remove(uItems.Length - 1, 1)

uItems.Append("&NO=3&F=1")

This will enumerate the checked cells in your query string and give you somthing like this.

http://default.html?g1=bla1&g2=bla2&g3=bla3&NO=3&F=1

To Return a URL for each checked Item

Dim urlItems = New List(Of String)()

Const urlFormat As String = _
    "http://default.html?gen={0}&NO=3&F=1"

For Each r As GridViewRow In GridView1.Row
    If CType(r.Cells(0).FindControl("recs"), CheckBox).Checked Then
        Dim url = String.Format(urlFormat, r.Cells(1).Text)
        urlItems.Add(url)
    End If
End For

This makes urlItems that is a generic List of Strings, each item being a url


Okay the bit above shows you how to get a list of url Strings so, to iterate the urls do

For Each url As String In urlItems
    //... Some code for POSTing or GETting .. your request 
End For
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Thanks @Jodrell for your kindness. It still gives me the solution we don't need. We don't need http://default.html?gen=t,m,t,etc&no=3&f=1. What we need is as many separate URLs as possible like: http://default.html?gen=t&no=3&f=1,http://default.html?gen=m&no=3&f=1,http://default.html?gen=e&no=3&f=1,... I love the stringbuilder idea though. Will it work with what I am trying to accomplish? – Kenny Aug 10 '12 at 15:01
  • Ahh, so you want multiple urls, ok – Jodrell Aug 10 '12 at 15:03
  • I see that you just edited your solution. Is that the current soluton? We still need to pass it to getRecs. Oh bummer I mistakenly left that out. After Next, or in your case, after End For, is getRecs like: Response.Redirect("getRecs.aspx?pin=" & Server.UrlEncode(uItems)) – Kenny Aug 10 '12 at 15:17
  • if I understand correctly, I need to put this code below: ("getRecs.aspx?pin=" & Server.UrlEncode(uItems)) in the second FOR loop. I did but it gives me only one URL even though I check 5 just to test. It is possible I am doing it incorrectly. – Kenny Aug 10 '12 at 16:41
  • I suggest you debug the first loop and see if five items are added to the list. – Jodrell Aug 10 '12 at 16:44
  • I did and I got 5 URLs but when selecting the 5 to add to getRecs.aspx, it only 1 was returned. The returned url has the correct value for gen but only one. – Kenny Aug 10 '12 at 16:58
  • I tested again and you are right the first FOR loop is returning just one record only. The problem lies there. – Kenny Aug 10 '12 at 17:55