Solved

SelectedIndexChanged event on datagrid

Posted on 2006-06-27
6
266 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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