Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SelectedIndexChanged event on datagrid

Posted on 2006-06-27
6
Medium Priority
?
271 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 2000 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 2000 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

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!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 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