Problem when using TemplateField and ImageButton

ASP.Net , Csharp , Javascript Pasir Gudang, Malaysia
  • 11 years ago

    Hi all, i have a question to ask, can someone help me to solve this problem?

    • I using datagridview with templatefield and imagebutton inside

    • The function of image button is use for delete the data

    • Can someone give me the code how to retrieve the key/data for that row using code behind?

    • The idea i have is using FindControl, but i dun know the full sentence of coding

    Thanks

  • 11 years ago

    Sure, I can help. Just to verify are you wanting to remove the row? or just grab values from a selected column?

  • 11 years ago

    oh...thanks mhuff84

    Now i just want to grab the values from selected column

    Thank again ^^

  • 11 years ago

    Well if its innerText then you can simply use:

    GridView1.SelectedRow.Cells[1].Text;

    If you want to get values from a control an example would be:

    DropDownList ddl = (DropDownList)GridView1.FindControl("DropDownList1"); myVariable = ddl.SelectedValue;

    There are other ways to achieving this, some may use RowCommands with Command names and arguments; however, this may achieve what your looking for.

  • 11 years ago

    Thank mhuff84...i will try it later...any issue i will post here and hope u can help

    Thank again

  • 11 years ago

    Thank mhuff84...i will try it later...any issue i will post here and hope u can help

    Thank again

  • 11 years ago

    To mhuff84...

    What i need is ur 2nd coding method which get the values from control

    But i am using image button and for example if i want to grab the 1st column values in datagridview, how can i achieve that?

    Thank again

  • 11 years ago

    Ok, one more question before I can help you. What is the control in the first column you are trying to get values from (e.g. dropdownlist, textbox, etc...)?

  • 11 years ago

    To mhuff84,

    What i doing is only using datagridview and display the data from database

    Link Text

    I want to take the grab the "CODE" from this datagridview

  • 11 years ago

    To mhuff84,

    What i doing is only using datagridview and display the data from database

    Link Text

    I want to take the grab the "CODE" from this datagridview

  • 11 years ago

    ok if the "Code" is databound information you can simply do this:

    string codeVariable;

    codeVariable = GridView1.SelectedRow.Cells[0].Text;

    if "Code" is an integer you must convert it:

    int codeVariable;

    codeVariable = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text);

  • 11 years ago

    Hi mhuff84

    Sorry of make trouble for u, in that datagridview just now i still have imagebutton "Delete" infront of that "Code"

    So when i click that imagebutton "Delete: i want grab the "Code"

    Thank again

  • 11 years ago

    its not a problem chunwei87. If you put that code in your "Button_Click" event that snippet I gave you will grab the "Code" column text. Did you try it? Did it not work as intended?

  • 11 years ago

    oh...ok...i think i get your mean...i trying now...thank mhuff84...u are so helpful

    Thank again

  • 11 years ago

    No problem. Make sure and post back if it does not work.

  • 11 years ago

    try this

    Button btn = (Button)sender; int Index = ((GridViewRow)btn.Parent.Parent).RowIndex;

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger