Solved

SelectedIndexChanged event on datagrid

Posted on 2006-06-27
6
262 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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