• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

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
0
REA_ANDREW
Asked:
REA_ANDREW
2 Solutions
 
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
Technology Partners: 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!

 
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
 
Elvio Lujan.Net Senior DeveloperCommented:
tbox = (TextBox)e.Item.FindControl("subTypeID");
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now