Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using DataGrid/drop down list combination...

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
Is is possible to have the hyperlink column on a datagrid redirect to a page based on a selection from a drop down list? For example, when user clicks on hyperlink in Grid, there are 3 possibilities for redirecting to the next aspx page -- based on the user's selection from a drop down box.

Also, as an aside, NetScape isn't reading the "height" of an aspx textbox so is hampering development for both Explorer and Netscape, any ideas here?
 
As I have heard anything is possible with enough code.

You could try having the hyperlink run a sub in your codebehind. In the sub get the value of the list box and build a string based on that criteria. Once your string is built say then do a redirect to that page. That'l do donkey, that'l do
[bravo]
 
Zarcom: Can you steer me in the right direction for "triggering" a code routine with the hyperlink field?

For example:

......String=".\ChemData.aspx?AwwSiteCode={0}"

Would I replace the redirect statement with syntax that looks to a code behind index change in a drop down list? Not sure how this would be done...in the above example I understand how the hyperlink line redirects to another page, using the Key Datafield, but not sure how to have this line "look" to a bit of VB behind code...thanks for your time....
 
That is a good point. All right I change my mind then. Rather do the code behind on the drop down.


Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles_ DropDownList1.SelectedIndexChanged
HyperLink1.NavigateUrl = "
End Sub


Within that sub you can to a case or some if statments so the hyperlink is directed to the proper URL.

HTH That'l do donkey, that'l do
[bravo]
 
Isadore,

Is there only one dropdown on the page, or is there a dropdown on each line of the datagrid that you're going to try to reference?

Problem with Zarcom's solution (no offense Zarcom) is that if I were building it, then I wouldn't want a postback to occur when I changed the index of the dropdown -- and if I'm understanding what you're asking here, I don't think you do either. Rather, the postback should only occur when the user clicks the link, at which point, you'll redirect.

I have a method in mind for each of the two options, but the second is pretty lengthy, so just let me know which suggestion you'd like to hear.

:)
paul
penny1.gif
penny1.gif
 
Thanks for the time, really appreciate that.

There is one drop down, sitting just above the datagrid. The user selects one of the records in the DataGrid, but the redirect must take its cue from the drop down.

For example, lets say the drop down has three choices. In my case they are "Chemistry", "Bacteria" and "Bioassessment". The Grid contains "sampling sites" (with attendant information), designating where people take measurements on streams (samples) - the results of which they are about to enter over the NET. The user will "select" a site in the Grid, but then must also select what "type" of sample that he/she is reporting.

So, user selects, say, Chemistry in the drop down, then goes on to select the Site No. in the Grid. At that point the page should be re-directed to the Chemisry Data Entry Form, as compared with the Bacteria Data Entry Form, and so on. TIA.

**** Part II *****

If you don't mind I'd like to ask one more question; just a hint will do. When I click on the hyperlink field of the grid, how would I throw, say, Column(4), a non-hyperlinked bound column field, into Session State to use a couple of pages down the road? or into the Grid's QueryString? I have been totally unsuccessful in extending a DataGrid's Querystring on the hyperlink field.
 
Ok, let's use a template column for your link. In order to know what line was clicked, we have to make use of the datagriditemindex -- that'll tell us what we need to know.

In order to do that, we'll programatically add the hyperlink column to the grid in the itemDataBound event of the datagrid. If you're not familiar, that event fires on every row of the grid as it's bound, and it's the perfect time to "tweak" things to get them looking or acting correctly.

So, make your template column look like this:
<asp:TemplateColumn headertext=&quot;Navigation&quot;>
<ItemTemplate>
 
</ItemTemplate>
</asp:TemplateColumn>

Nothing there at design time. I'll assume that you've declared that as the first column (0) in your datagrid. With that assumption, you'll need this function in your code-behind:

Protected Sub addHyperLinkColumn(ByVal o As Object, ByVal e As DataGridItemEventArgs) Handles myGrid.ItemDataBound
Dim newLink As New HyperLink()
newLink.Text = &quot;Click Here&quot;
Dim sb As New StringBuilder()
sb.Append(&quot;javascript:redirect(&quot;)
sb.Append(e.Item.ItemIndex.ToString())
sb.append(&quot;,'&quot;)
sb.append(e.item.cells(4).text)
sb.Append(&quot;');&quot;)
newLink.NavigateUrl = sb.ToString()
e.Item.Cells(0).Controls.Add(newLink)
End Sub

Pretty easy to follow what's going on there, I hope. The only somewhat abstract part of it is where we get the item index and the fourth value. It comes to us in the form of the 'e' argument for the function, and we just extract what we want from it. basically, you have the whole datagrid row in that argument, and you pull values from it as I've shown.

I also picked up the fourth column's value and we're going to send that along to our function, as well.

Now, I just called our own custom client side function, redirect, which might look like this:

<script language=javascript>
function redirect(itemIndex,val){
document.forms[0].clickedItem.value = itemIndex;
document.forms[0].theOtherValue.value = val;
document.forms[0].submit();
}
</script>

So then, you'll need your own form elements on the page called 'clickedItem' and 'theOtherValue', since that's where we'll store the user's click position and that other value you want.

<input type=hidden name=clickedItem>
<input type=hidden name=theOtherValue>

Easy enough. So now we have provided unique links on each row that will tell us what row the user clicked and what the fourth column on that row contained. We're home free.

Just as an aside, I showed you how to get the item index, but I'm not going to use it, since we already have that fourth item's value. You could have used the item index to pull it from the underlying datasource if you had wanted to (along with a host of other useful stuff, which is why I indluded it for you):

So... in your page load:

dim fourthValue, newLocation as string
if page.ispostback then
fourthValue = request.form(&quot;theOtherValue&quot;)
session(&quot;fourthValue&quot;) = fourthValue

'now, for your original question:
newLocation = yourListBox.selectedItem.value
response.redirect(newLocation)

end if

That is assuming that you put the actual url in the value attribute of your list box -- but I think you get the point either way, yes?

Let me know if any of this was unclear.

Good luck! :)
paul
penny1.gif
penny1.gif
 
No offense taken Paul. And a rather ingenious solution if I may say so. That'l do donkey, that'l do
[bravo]
 
Paul & Zarcom: Thanks. I'll post one more time later today after I instate the code. I will contribute here at Tek-Tips as my skills get a little better. It's too bad we can't send checks as a token of our appreciation as I would readily do that -- your examples not only help me, but will accomplish two goals beyond that, q.v., help others with similar problems, and more importantly, send the message that helping others is a primary facet of academics and professionalism.
 
Your welcome Isadore. I must say that this is the best forum site I have found. The atmosphere is great and people are friendly and helpful.
No checks are necessary, helping others also helps me to understand different concepts even better. That'l do donkey, that'l do
[bravo]
 
When I get the code running I will post one final time -- using the DataGrid in combination with other objects such as drop downs is a pretty useful feature...so will make sure all working ok on the then stick the code in this thread. Danke shoen!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top