473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

putting text info from DataGrid ButtonColumn into a cookie

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
Nov 15 '05 #1
3 1695
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.
Nov 15 '05 #2
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
Nov 15 '05 #3
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
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
0
by: BK Kim | last post by:
Hi. I am trying to use buttonColumn in the datagrid. My datagrid is binded to a datatable. And I am trying to get the text value from itemcreated or itemdatabound event in the datagrid. ...
5
by: Alan Seunarayan | last post by:
Hello, I need to create a DataGrid that looks something like this... <HyperLinkColumn><ButtonColumn><Data><Data><ButtonColumn> but I can only seem to produce a DataGrid as below... ...
1
by: Sathyaish | last post by:
How do you get the text of a ButtonColumn control column in a datagrid? As an example, I tried this little snippet on a test project, but it returns a null string. Private Sub...
19
by: Joe | last post by:
I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred...
1
by: Ben | last post by:
Hi there, I would like to change the text of the ButtonColumn after datagrid binding, depending on the value of a certain field. (eg. Text="Details" for case 1, Text="others' for case 2) Is...
2
by: ismaelf | last post by:
i have a textbox in a template column in a datagrid, it displays the initial value of the row, but when i change it remains with the original value (it doesnt get the new value..) i fill the...
2
CroCrew
by: CroCrew | last post by:
Hello all, First off I would like to thank anyone that helps me out with this problem that I have. Ok, the environment I am working in is ASP.net with the code behind done in VB. I have a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.