Solved

SelectedIndexChanged event on datagrid

Posted on 2006-06-27
6
260 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
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
orbulat earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Assisted Solution

by:orbulat
orbulat earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
ar, I misunderstood "event code for opening webpage" :-p

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now