Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:19:00+00:00 2026-05-31T17:19:00+00:00

I have a dynamically created GirdView based on value of a DropDownList. I use

  • 0

I have a dynamically created GirdView based on value of a DropDownList.
I use the ITemplate interface to generate the fields:

public class CreateItemTemplateOrder : ITemplate
{
     ImageButton imgbtn_up;
     ImageButton imgbtn_down;

     string s_imgbtn_up_name;
     string s_imgbtn_up_ImageUrl;
     string s_imgbtn_up_CommandName;
     string s_imgbtn_up_CommandArgument;

     public CreateItemTemplateOrder(string imgbtn_up_name, string imgbtn_up_ImageUrl, string imgbtn_up_CommandName, string imgbtn_up_CommandArgument)
     {

         this.s_imgbtn_up_name = imgbtn_up_name;
         this.s_imgbtn_up_ImageUrl = imgbtn_up_ImageUrl;
         this.s_imgbtn_up_CommandName = imgbtn_up_CommandName;
         this.s_imgbtn_up_CommandArgument = imgbtn_up_CommandArgument;

     }

     public void InstantiateIn(Control objContainer)
     {
         imgbtn_up = new ImageButton();
         imgbtn_up.DataBinding += new EventHandler(imgbtn_up_DataBinding);
         objContainer.Controls.Add(imgbtn_up);
     }

     private void imgbtn_up_DataBinding(object sender, EventArgs e)
     {
         ImageButton imgbtn_up = (ImageButton)sender;
         imgbtn_up.ID = s_imgbtn_up_name;
         imgbtn_up.ImageUrl = s_imgbtn_up_ImageUrl;
         imgbtn_up.CommandName = s_imgbtn_up_CommandName;
         imgbtn_up.CommandArgument = s_imgbtn_up_CommandArgument;
         imgbtn_up.CausesValidation = false;
     }
 }

Now I want to fire RowCommand From this dynamically generated column with command argument and command name bounded to this ImageButton

the code to get this working is:

 Protected void inizializza_gw_tipi(){
       TemplateField order_col = new TemplateField;
       order_col.ItemTemplate = new CreateItemTemplateOrdine("imgbtn_up", "~/imgs/Up.gif", "minus", "order");
       order_col.HeaderText = "order";
       order_col.SortExpression = "order";

       gw_tipi.Columns.Add(order_col);
 }

All this code works fine but doesn’t fire RowCommand from GridView when clicked on the ImageButton

EDIT:
I call the procedure in SelectedIndexChanged event of a DropDownList
ddl_tipi_SelectedIndexChanged(Object sender , System.EventArgs e)
{
inizializza_gw_tipi();
gw_tipi.DataBind();
}

    Protected Sub inizializza_gw_tipi()
       Using cn As New SqlConnection(shared_foos.connectionString)
        Using cmd As New SqlCommand("SELECT nome_tabella, nome_campo_id, nome_campo_nome, nome_campo_descrizione, has_order FROM maschere_tipi WHERE id = @id", cn)
            cmd.Parameters.AddWithValue("id", IIf(Session("sel_val") Is Nothing, "", Session("sel_val")))
            cn.Open()
            Dim rdr As SqlDataReader = cmd.ExecuteReader
            rdr.Read()
            If rdr.HasRows Then
                Dim b_crea_controllo As Boolean = True
                'controllo se mettere o no la colonna ordine
                If rdr("has_order") = True Then
                    'controllo che la colonna non sia già stata inserita, 
                    'se è già stata inserita la rimuovo e la ricreo
                    For i As Integer = 0 To gw_tipi.Columns.Count - 1
                        If gw_tipi.Columns(i).HeaderText = "ordine" Then
                            'gw_tipi.Columns.Remove(gw_tipi.Columns(i))
                            b_crea_controllo = False
                        End If
                    Next

                    If b_crea_controllo = True Then

                        Dim ordine_col As New TemplateField()
                        ordine_col.ItemTemplate = New CreateItemTemplateOrdine("lbl_ordine", "ordine", "imgbtn_up", "~/imgs/Up.gif", "meno", "ordine", "imgbtn_down", "~/imgs/Down.gif", "piu", "ordine", AddressOf ImageCommand)
                        ordine_col.HeaderText = "ordine"
                        ordine_col.SortExpression = rdr("nome_campo_nome")

                        gw_tipi.Columns.Add(ordine_col)

                    End If
                End If

                b_crea_controllo = True

                For i As Integer = 0 To gw_tipi.Columns.Count - 1
                    If gw_tipi.Columns(i).HeaderText = rdr("nome_campo_nome") Then
                        b_crea_controllo = False
                    End If
                Next

                If b_crea_controllo = True Then
                    Dim nome_col As New TemplateField()
                    nome_col.ItemTemplate = New CreateItemTemplateLabel("lbl_nome", rdr("nome_campo_nome"))
                    nome_col.HeaderText = rdr("nome_campo_nome")
                    nome_col.SortExpression = rdr("nome_campo_nome")
                    gw_tipi.Columns.Add(nome_col)
                End If

                b_crea_controllo = True

                For i As Integer = 0 To gw_tipi.Columns.Count - 1
                    If gw_tipi.Columns(i).HeaderText = rdr("nome_campo_descrizione") Then
                        b_crea_controllo = False
                    End If
                Next

                If b_crea_controllo = True Then
                    Dim descrizione_col As New TemplateField()
                    descrizione_col.ItemTemplate = New CreateItemTemplateLabel("lbl_descrizione", rdr("nome_campo_descrizione"))
                    descrizione_col.HeaderText = rdr("nome_campo_descrizione")
                    descrizione_col.SortExpression = rdr("nome_campo_descrizione")
                    gw_tipi.Columns.Add(descrizione_col)
                End If

                Dim str_order_by As String = " ORDER BY "
                Dim str_ordine As String = ""

                If rdr("has_order") = True Then
                    str_ordine = ", ordine "
                    str_order_by &= "ordine"
                Else
                    str_order_by &= rdr("nome_campo_nome")
                End If

                Dim sqlds_tipi As New SqlDataSource(shared_foos.connectionString, "SELECT " & rdr("nome_campo_id") & ", " & rdr("nome_campo_nome") & ", " & rdr("nome_campo_descrizione") & str_ordine & " FROM " & rdr("nome_tabella") & str_order_by)

                gw_tipi.DataSource = sqlds_tipi

            End If
        End Using
    End Using

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:19:01+00:00Added an answer on May 31, 2026 at 5:19 pm

    I took your code as is and tested. there is no errors on this code. Also the row command is triggered properly. I presume the problem is with eventbinding of the dynamic gridview you created.

    For the row command to be triggered the gridview has to be created on every postback. and the event should be bound.

    The Code you provided is not exactly what i needed. It doesn’t have the initialization of the grid view.

    The following should give you some insight.

    GridView GV = new GridView(); //Make sure to create the grid view on every postback and populate it. Alternatively (i dont know if its a good practice.) you can store a reference of gridview in session.
    GV.RowCommand += new GridViewCommandEventHandler(GV_RowCommand); //bind the row command event. this should solve the problem.
    

    The server doesnt keep track of dynamically created controls. So when the post back occurs the gridview control does not exist. and thus no event can be triggered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamically created UL. I want to use jQuery to use the
I have a dynamically-created list of strings called 'variables'. I need to use these
I have some dynamically created inputs which are not server-side controls. I want to
I have a dynamically created image that I am saving to a stream so
I have a dynamically created table which in the last <td> there is a
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
i have a table which contains a bunch of dynamically created radio button lists,
I have a table of paged data, along with a dynamically created pager (server-side
i have a unordered list of links, which are dynamically created by Ajax, and
I have a page containing a control called PhoneInfo.ascx. PhoneInfo is dynamically created using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.