Solved

SelectedIndexChanged event on datagrid

Posted on 2006-06-27
6
267 Views
Last Modified: 2008-01-09
Hi,

I 'm trying to get my datagrid SelectedIndexChanged do the following:

User clicks the 'Select' command on the datagrid. The value of a field in the datagrid's selected record is passed to a public variable ('strSelectedDepotID'), then a different web-page opens and loads the specific record using the public variable as the criteria.

1. I need the SelectedIndexChanged event code to get the value of field whose HeaderText: is 'ID', and DataField: 'DepotID' of the selected record in datagrid1. I have tried using following without any luck:

DOESN'T WORK:
SqlCommand1.Parameters("@DepotID").Value = e.Item.Cells(2).Text
strSelectedDepotID = e.Item.Cells(2).Text
strSelectedDepotID = CType(DataGrid1.SelectedItem.FindControl("ID"), Label).Text

2. What is the event code to open a different web-page? (Page called 'EditFSLInfo.aspx'). When this page loads, I can then use the public variable passed in step1 to load the specific record - (I can do this bit )

0
Comment
Question by:LoveToSpod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
orbulat earned 500 total points
ID: 16990702
sorry my code is in c#,

step1 is inside this event -->

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
         DropDownList ddl = (DropDownList)e.Item.FindControl("ddlist");
        string newLink = "EditFSLInfo.aspx?id="+e.Item.Cells(2).Text;
      ddl .Attributes.Add("onChange","location.href="+newLink);
    }
}
0
 
LVL 10

Expert Comment

by:orbulat
ID: 16990713
this should also work

ddl .Attributes.Add("onChange","goToPage(this);");

function goToPage(ddl)
{
   var newLink = "EditFSLInfo.aspx?id="+ddl.options[ddl.selectedIndex].value;
   location.href = newLink;
}
0
 

Author Comment

by:LoveToSpod
ID: 16990764
Hey Orbulat:

Your code above helped me discover that I've got all tangled up.

I should be using the ItemCommand event, not the SelectedIndexChanged. DOH!!!!!!!!!!  Using this with code: (strSelectedDepotID = e.Item.Cells(2).Text) has passed the DepotID to the Vaiable! Cool, Step1 fixed. 250pts for you so far!

Step 2: How do I now redirect to Page called 'EditFSLInfo.aspx'.

I can see you have provided a C# suggestuion above, but the event is wrong (sorry...). Can C# be embedded into vb.net? Do I just paste it in? If so give me the C# code, else I'll need the VB.NET code.

Cheers,

LTS
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:orbulat
orbulat earned 500 total points
ID: 16990803
you can do sth. like this

Step1:
go to Design Mode, choose your Datagrid, on Properties Window, click "Events" icon (it is look like a thunder icon)

you can find "OnItemDataBound", u double click it, then it will add this event for u in code behind

If (e.Item.ItemType == ListItemType.Item or e.Item.ItemType == ListItemType.AlternatingItem) Then
   
         DropDownList ddl = CType(e.Item.FindControl("ddlist"),DropDownList)
        string newLink = "EditFSLInfo.aspx?id="+e.Item.Cells(2).Text
        ddl.Attributes.Add("onChange","location.href="+newLink)
 End If

for Step2:
you want sth. like when user selects an item in the drop down box, then it will redirect him to another page?
if it is so, you can add the javascript function as the code above, provide your own links with IDs, and use location.href to go to another page

of course, u can use your own event handler if your don't want to use javascript
then it will be sth. like

ddl.OnSelectedIndexChange += new EventHandler(GoToPage)..etc..
0
 

Author Comment

by:LoveToSpod
ID: 16990903
orbulat

The overall solution is:

1. strSelectedDepotID = e.Item.Cells(2).Text
2. Response.Redirect("SERVERNAME/technicalCourier/SecretFiles/EditFSLData.aspx")

Simple as that. I did deduce the answer assisted by your suggestions. (Remember the problem was I was using the wrong event. I should have been using ItemCommand event, not the SelectedIndexChanged as I wasn't even using a ddl). Will be useful for the future I'm sure ;)

Thanks, LTS
0
 
LVL 10

Expert Comment

by:orbulat
ID: 16990941
ar, I misunderstood "event code for opening webpage" :-p

anyway, it's great that everything works now :o)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question