putting text info from DataGrid ButtonColumn into a cookie

M

Mark Jones

Hi All,

I'm currently having difficulty putting the text from a selected link in a
ButtonColumn from a DataGrid into a cookie. The idea is that a user selects
a group link from this column and is taken to the group page, where the
group link they have just selected is used to populate the page with the
relevant group information from the database. This is done by assigning the
info to a cookie, which is then called on the next page. However, I can't
seem to be able to extract the text information from the selected link in
the DataGrid.

The relevant C# code is as follows:

private void dgGroups_SelectedIndexChanged(object sender, System.EventArgs
e)

{

if (dgGroups.SelectedIndex != -1)

{

string strGroupName = "";

strGroupName += dgGroups.SelectedIndex.ToString();




HttpCookie objNewCookie = new HttpCookie("GroupName");

objNewCookie.Expires = DateTime.Now.AddDays(30);

objNewCookie.Values.Add("groupname", strGroupName);

Response.Cookies.Add(objNewCookie);

Response.Redirect("grouppage.aspx");

}

}



The cookie works fine but I can't deliver the selected text from the
ButtonColumn to the next page, as I can't define the SelectedIndex as Text.
I'm pretty sure I'm missing something out or approaching it the wrong way.
Any help would be much appreciated.

Mark Jones
 
F

Fergus Cooney

Dear Mark

Let's have a look at your code.

1 string strGroupName = "";
strGroupName += dgGroups.SelectedIndex.ToString();

2 HttpCookie objNewCookie = new HttpCookie("GroupName");
objNewCookie.Expires = DateTime.Now.AddDays(30);
3 objNewCookie.Values.Add("groupname", strGroupName);
Response.Cookies.Add(objNewCookie);


1
This may be left over from other editing but it's not necessary to
create an empty string and then add to it.
This should become.
string strGroupName = dgGroups.SelectedIndex.ToString();

2
You're creating your cookie with a given name. That's fine.
HttpCookie objNewCookie = new HttpCookie("GroupName");

Did you know that you can also give it its value at the same time?
HttpCookie objNewCookie = new HttpCookie("GroupName",
strGroupName);

3
You then add the value to the Cookie but with the value's name in
lowercase. This doesn't match the name in the constructor. This may
explain why you are getting nothing back. Your cookie will have no
value for "GroupName" and the index for "groupname". Which one are you
accessing in the redirected page?


You might find that the following works.
HttpCookie objNewCookie = new HttpCookie("GroupName",
dgGroups.SelectedIndex.ToString());
objNewCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(objNewCookie);

Regards,
Fergus

ps. I've already sent this but it didn't appear so I sent it again.
Thus there may be two copies.
 
M

Mark Jones

Fergus,

Thanks for your input - I gave your ammended code a go but unfortunatly the
text selected from the DataGrid still isn't being passed on to the next
page. When, for instance, the group name 'sheffield' is clicked from the
ButtonColumn, the aim is to pass this text information to the next page.
Currently, a '0' value is being passed on, so I am getting something, just
not the text. Therefore, I suspect the problem isn't so much in the creation
of the cookie (from what you say this obviously could be improved but I am
using code that has previously been successful) but in the accessing of the
selected information in the DataGrid.

strGroupName += dgGroups.SelectedIndex.ToString();

I think this is the point at which I'm going wrong - perhaps because it's
not specifically asking for text? A friend suggested the following in place
of the above code:

int varIndex =dgGrids.SelectedIndex;

int id =
Int32.Parse(dgGrid.DataKeys[varIndex].ToString().Trim());



But this code involves integers rather than text and I have been unable to
successfully adapt it to my needs. Do you have any other suggestions?



Regards



Mark Jones
 
F

Fergus Cooney

Hi Mark,

SelectedIndex is an index - a number. That's what I thought you
wanted!!

You're going to have to use it to access the correct properties of
the DataGrid. Read the help on the DataGrid. There's a lot of it, I
know. Look at all the members that talk about Index, Item and Key.
Keep at it and your understanding will increase. Draw diagrams if you
can. Follow the links to the Samples.

I say this because I can do no more today. I have to finish now
and won't be back until tomorrow.

Best of luck,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top