DataList Control

Hi,

In a Datalist I have a template, this consists of a Tedxtbox which I write a field from the database to and a link.

my question is, how can you code it so that on clicking the link, the code knows which row you are on in the datalist or which item?  I ask because i would prefer to do it all on one page and not use another page as the processing one.

thanks in advance

Andrew
LVL 20
REA_ANDREWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

culshajaCommented:
There are two ways you can do this. In both cases you use a LinkButton control rather than a hyperlink.

You can set the DataKeyField property on the datalist and then when you postback you just get the value via list.DataKeys(e.ItemIndex) within the datalist itemcommand event

The other way is to set the commandargument property of the hyperlink control and then access is on the itemcommand event via e.CommandArgument.

My preference would be for the first.

James :-)
0
REA_ANDREWAuthor Commented:
I have chosen to go with the

DataListCommandEventArgs e

But I get this error with this function?

Compiler Error Message: CS0118: 'System.Web.UI.WebControls.BaseDataList.DataKeys' is a 'property' but is used like a 'method'

Source Error:

 

Line 96:             String sname;
Line 97:             String SQLthis;
Line 98:             sid = sub_cats_Dlist.DataKeys(e.Item.ItemIndex);
Line 99:             tbox = CType(e.Item.FindControl("subTypeID"), TextBox);
Line 100:            sname = tbox.Text;
 


Here is my function


            protected void Update_Command(Object sender,DataListCommandEventArgs e)
            {
            TextBox tbox = new TextBox();
            String sid;
            String sname;
            String SQLthis;
            sid = sub_cats_Dlist.DataKeys(e.Item.ItemIndex);
            tbox = CType(e.Item.FindControl("subTypeID"), TextBox);
            sname = tbox.Text;
            SQLthis = "UPDATE tbl_sub_categories SET sub_type = '" + sname + "' WHERE sub_type_id = " + sid ;
            MySqlCommand UpdateIt = new MySqlCommand(SQLthis,myConnection);
            myConnection.Open();
            UpdateIt.ExecuteNonQuery();
            myConnection.Close();
              sub_cats_Dlist.EditItemIndex = -1;
              LoadData();
            }
0
nouloukCommented:
Hi,

Try this because c#, not vb:
 sid = sub_cats_Dlist.DataKeys[e.Item.ItemIndex];

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

REA_ANDREWAuthor Commented:
thank you I did that and now I get this error

Compiler Error Message: CS0118: 'System.Web.UI.WebControls.TextBox' is a 'type' but is used like a 'variable'

Source Error:

 

Line 98:             System.Type t;
Line 99:             sid = sub_cats_Dlist.DataKeys[e.Item.ItemIndex].ToString();
Line 100:            tbox = Convert.ChangeType(e.Item.FindControl("subTypeID"), TextBox);
Line 101:            SQLthis = "UPDATE tbl_sub_categories SET sub_type = '" + sname + "' WHERE sub_type_id = " + sid ;
Line 102:            MySqlCommand UpdateIt = new MySqlCommand(SQLthis,myConnection);
 



here is my revised code
            protected void Update_Command(Object sender,DataListCommandEventArgs e)
            {
            TextBox tbox = new TextBox();
            String sid;
            String sname;
            String SQLthis;
            System.Type t;
            sid = sub_cats_Dlist.DataKeys[e.Item.ItemIndex].ToString();
            tbox = Convert.ChangeType(e.Item.FindControl("subTypeID"), TextBox);
            SQLthis = "UPDATE tbl_sub_categories SET sub_type = '" + sname + "' WHERE sub_type_id = " + sid ;
            MySqlCommand UpdateIt = new MySqlCommand(SQLthis,myConnection);
            myConnection.Open();
            UpdateIt.ExecuteNonQuery();
            myConnection.Close();
              sub_cats_Dlist.EditItemIndex = -1;
              LoadData();
            }
0
GavinMannionCommented:
Try changing

tbox = Convert.ChangeType(e.Item.FindControl("subTypeID"), TextBox);
to
TextBox tbox = (TextBox)e.Item.FindControl("subTypeID");

and drop the line System.Type t; because it is doing nothing???
Also drop the original declaration of TextBox tbox = new TextBox();

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Elvio Lujan.Net Senior DeveloperCommented:
tbox = (TextBox)e.Item.FindControl("subTypeID");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.