Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Go Back  Xtreme Visual Basic Talk > > > Retrun Selection in DropDown Box to DataGrid


Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2006, 09:31 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default Retrun Selection in DropDown Box to DataGrid


In ASP.NET I am trying to Return the Selection of a User's DropDown Box to Display that data in a datafield within the DataGrid.

I am just digging into ASP.Net so I don't know all of the rules vs. Classic ASP. The code I have right now is this:

<asp:BoundColumn DataField="<%Session("tblSelect")%>" HeaderText="Test">
</asp:BoundColumn>

By the errors I get:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A field or property with the name '"Session("tblSelect")"' was not found on the selected data source.

I can assume you cannot bring Sessions into the HTML due to "spaghetti stringing'.

How can I display a custom value in the DG from a Dropdown box selection... BTW here is my code for the dropdown box and selectionchange:

Protected Sub FieldSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If FieldSelect.SelectedValue = "Cell_Number" Then
Session("tblSelect") = "SID"
ElseIf FieldSelect.SelectedValue = "Switch_Number" Then
Session("tblSelect") = "Switch_Number"
End If
Call ShowDataGrid()
End Sub
Reply With Quote
  #2  
Old 12-07-2006, 09:49 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

A Bound Column, only pulls a field directly from the database. You can create a TemplateColumn, and then define exactly what happens in the column.

Are you trying to change which Column Displays? If so, you can have a Grid that has all of the possible columns. Then in the dropdownlists change handler, modify the Visible properties of your columns so the right ones show up.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #3  
Old 12-07-2006, 10:25 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Thanks Wayneph... in ASP.NET what is the closest equivalent to calling Rs.Fields("myField")....

In my template field in the DG that is what I want to call....
Reply With Quote
  #4  
Old 12-07-2006, 10:39 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

There really isn't one. What I normally do for somthing like that is use the ItemDataBound Event, and set any dynamic properties in the code behind.

In the event, the e.Item.DataItem will let you get at the data fields.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #5  
Old 12-07-2006, 11:40 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Ok, I have added an event:

Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Session("value1") = e.Item.DataItem("SID")
End Sub 'Item_Bound

And I added OnItemDataBound="Item_Bound" to the DG...

<asp:TemplateColumn HeaderText="TEST">
<ItemTemplate>
<%=Session("value1")%>
</ItemTemplate>
</asp:TemplateColumn>

I get the error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error:


Line 165:
Line 166: Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Line 167: Session("value1") = e.Item.DataItem("SID")
Line 168: End Sub 'Item_Bound
Line 169:</script>

I have played around with the different formatting with this error and I am not having any luck. I am playing around with ASP.Net cause I want to slowly convert a system to ASP.Net...
Sorry if my ASP.Net questions seem non-intellectual... I am learning ASP.Net and having used ASP exclusively, it is taking a toll on me.
Reply With Quote
  #6  
Old 12-07-2006, 02:16 PM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

I have been formatting this a little more and this is what I now have...

Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

Label3.Text = e.Item.DataItem("SID").ToString

End Sub 'Item_Bound


I still get an error though...

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error:


Line 186:
Line 187: End If
Line 188: Label3.Text = e.Item.DataItem("SID").ToString
Reply With Quote
  #7  
Old 12-07-2006, 03:39 PM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

it also runs for the headers, footers and things like that. You need to add an If Statement that checks the e.Item.ItemType is either an Item or AlternatingItem.

Then you'll need to find Label3 with in the row since there will be a Label3 for every row.

Code:
If e.Item.ItemType = ListItemType.Item OrElse _ e.Item.ItemType = ListItemType.AlternatingItem Then DirectCast(e.Item.FindControl("Label3"), Label).Text = e.Item.DataItem("SID").ToString End If
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #8  
Old 12-07-2006, 03:50 PM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Thanks, I was almost there... I was on the right track with the Item and Alternating Items.... I thought I was wrong though because even after you shared your code with me I still get this new error...

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 191: If e.Item.ItemType = ListItemType.Item OrElse _
Line 192: e.Item.ItemType = ListItemType.AlternatingItem Then
Line 193: DirectCast(e.Item.FindControl("Label3"), Label).Text = e.Item.DataItem("SID").ToString
Line 194: End If
Line 195:



Thanks!!!






Quote:
Originally Posted by wayneph
it also runs for the headers, footers and things like that. You need to add an If Statement that checks the e.Item.ItemType is either an Item or AlternatingItem.

Then you'll need to find Label3 with in the row since there will be a Label3 for every row.

Code:
If e.Item.ItemType = ListItemType.Item OrElse _ e.Item.ItemType = ListItemType.AlternatingItem Then DirectCast(e.Item.FindControl("Label3"), Label).Text = e.Item.DataItem("SID").ToString End If
Reply With Quote
  #9  
Old 12-07-2006, 03:53 PM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Do you have a Label named Label3 anywhere in your templates?
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #10  
Old 12-07-2006, 06:28 PM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

I got it... I moved the label3 out of the template for testing... I moved it back in and it works great.

I really appreciate the help! Thanks many! I am sure we'll be talking again soon....

Quote:
Originally Posted by wayneph
Do you have a Label named Label3 anywhere in your templates?
Reply With Quote
  #11  
Old 12-08-2006, 08:16 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

One last thing... If I want to display in the template header.. another label... say Label4... I want Label4 to be the name of the field selected in the dropdown box.. what is the easiest way to do this?

Will I have to do a.FindControl again for the label4 that will be within the template?

Since I saw you used DirectCast (never seen this command before, perhaps I will have to use that again). I have tried a couple different methods but have had no luck.

Quote:
Originally Posted by mountainbiking
I got it... I moved the label3 out of the template for testing... I moved it back in and it works great.

I really appreciate the help! Thanks many! I am sure we'll be talking again soon....
Reply With Quote
  #12  
Old 12-08-2006, 08:25 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

If it's in the HeaderTemplate, you can use the same ItemDataBound event. Just put an Else statement and check for the Header Row. Then you can use FindControl and DirectCast to get at your label.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #13  
Old 12-08-2006, 08:30 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

If I used the same event procedure won't that try to return the values from the database and not the "Name of the Field".

For example if my DB field is 'State_Code' I want the header to say 'State'
and then below will be the different rows of records...
PA
DE
NJ
etc....

Quote:
Originally Posted by wayneph
If it's in the HeaderTemplate, you can use the same ItemDataBound event. Just put an Else statement and check for the Header Row. Then you can use FindControl and DirectCast to get at your label.
Reply With Quote
  #14  
Old 12-08-2006, 08:37 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Wayneph:

I got it... I thought you meant add within the same if statment.. I added a new if statement and I works...


two questions... what does directcast really do? and what is OrElse really intended for... if an or conditions exists then it would follow sequence but if it doesn't it still follows sequence??? that is how I read an OrElse ....


Also... here is my If Statement
If e.Item.ItemType = ListItemType.Header Then
DirectCast(e.Item.FindControl("Label4"), Label).Text = e.Item.DataItem(FieldSelect.Text).ToString
End If

I get an error using this.... when I replace the e.Item.DataItem(FieldSelect.Text).ToString
with "TEST" it comes back and displays the header properly... what attribute should I be using to display the "Text" of the dropdownlist?
Reply With Quote
  #15  
Old 12-08-2006, 08:52 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

don't use e.DataItem. Just FieldSelect.

DirectCast(e.Item.FindControl("Label4"), Label).Text = FieldSelect.Text
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #16  
Old 12-08-2006, 09:14 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Thanks... ya know when i am playing new new programming technology it is kind of funny how I throw out the obvious rules and make them harder than they should be... should have known it would just be FieldSelect.Text


Last thing I promise... it there any way to 'Mask' the value that gets returned to Label4? Other then capturing a value from the FieldSelect_SelectedIndexChanged?


If I have this for the FieldSelect

<aspropDownList ID="FieldSelect" runat="server" AutoPostBack="True" OnSelectedIndexChanged="FieldSelect_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="Cell_Number">Cell Number</asp:ListItem>
<asp:ListItem Value="Switch_Number">Switch Number</asp:ListItem>
</aspropDownList>

i want to display for example Cell Number instead of Cell_Number!
Quote:
Originally Posted by wayneph
don't use e.DataItem. Just FieldSelect.

DirectCast(e.Item.FindControl("Label4"), Label).Text = FieldSelect.Text
Reply With Quote
  #17  
Old 12-08-2006, 09:20 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Use FieldSelect.SelectedValue when getting data from e.Item.DataItem.
Use FieldSelect.SelectedItem.Text when displaying the Header.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #18  
Old 12-08-2006, 09:32 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Thanks! You've helped tremendously...

Any brief description on now getting that custom Label3 to convert to a textbox on the OnEditCommand = "dgPaging_Edit" with the DG?





Quote:
Originally Posted by wayneph
Use FieldSelect.SelectedValue when getting data from e.Item.DataItem.
Use FieldSelect.SelectedItem.Text when displaying the Header.
Reply With Quote
  #19  
Old 12-08-2006, 09:36 AM
wayneph's Avatar
waynephRetrun Selection in DropDown Box to DataGrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Add an EditItemTemplate and put a text box in it.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #20  
Old 12-08-2006, 09:56 AM
mountainbiking's Avatar
mountainbiking mountainbiking is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 259
Default

Sorry I should have been a little more clear on what I was asking... I did add a textbox to the EditItemTemplate... what sub routine should I place the popluation of that textbox in? I cannot get the SelectedValue to appear in the textbox... Should I be using the Item_Bound Sub to populate the textbox or the dgPaging_Edit Sub or something else?



Quote:
Originally Posted by wayneph
Add an EditItemTemplate and put a text box in it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
 
Retrun Selection in DropDown Box to DataGrid
Retrun Selection in DropDown Box to DataGrid
 
-->