We help IT Professionals succeed at work.

DataList Control

REA_ANDREW
REA_ANDREW asked
on
Medium Priority
515 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

Commented:
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 :-)

Author

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();
            }
Commented:
Hi,

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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();
            }
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();

Elvio Lujan.Net Senior Developer
CERTIFIED EXPERT

Commented:
tbox = (TextBox)e.Item.FindControl("subTypeID");
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.