Devx AspxGridView Confusion:

I wonder if someone can explain to me why the following works:

I have a situation where (converting from classic asp) we need to take data content of the row and conditionally and on the fly create a pdf that is then presented to the user.

As part of this I would need to be able to open a new window to display the pdf (which does not exist permanently so no direct linking is possible), and then also refresh the gridview.

I've got the attached code working, but I'm not sure why declaring a button causes this to work.

It all appears to work nicely, in the sense the grid does a Callback in pageing - searching, editing instance. When the image button is clicked the stored procedure is called (which in this case simply deletes the record as a test), and then a new window is opened (with some querystring data to prove its passable).

This seems to be the functionality I need, but I'm not sure what makes this work. Is it simply the declaration of the button? I might be missing something obvious here, but how does declaring a new button cause this functionality to work?

Any help is appreciatted.

<dx:GridViewDataColumn>
             <DataItemTemplate>
                    <asp:ImageButton ID="btnTemplate" runat="server" ImageUrl="~/printer-red.gif" CommandArgument='<%# Container.VisibleIndex%>' OnClick="btnTemplate_Click" ToolTip="Click Me" />
                    <asp:Button ID="btnTemplate2" runat="server" CommandArgument='<%# Container.VisibleIndex%>'
                        OnClick="btnTemplate_Click" Text="Click Me" />
              </DataItemTemplate>
            </dx:GridViewDataColumn>


Code Behind:

    protected void btnTemplate_Click(object sender, EventArgs e)
    {
        ImageButton button = sender as ImageButton;
        if (button == null) return;
        int visibleIndex = int.Parse(button.CommandArgument);
        object[] values = grid.GetRowValues(visibleIndex, "BrandId", "OPERATOR", "Active") as object[];

         OpenNewWindow("Target2.aspx?BrandId="+values[0]);

        string connectionString;
        connectionString = WebConfigurationManager.ConnectionStrings["T4ConnectionString"].ConnectionString;

         SqlConnection con = new SqlConnection(connectionString);
         SqlCommand cmd = new SqlCommand("usp_DeleteBrand", con);
         cmd.Parameters.AddWithValue("@id", values[0]);
         cmd.CommandType = CommandType.StoredProcedure;

         try
         {
             con.Open();
             cmd.ExecuteNonQuery();
         }
         finally
         {
             con.Close();
         }
         DataBind();              
    }

Open in new window

AzaniahAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

masterpassCommented:
Is it simply the declaration of the button? I might be missing something obvious here, but how does declaring a new button cause this functionality to work? >>>

If you see both the Image button and button is pointing to btnTemplate_Click function.

<asp:ImageButton ID="btnTemplate" runat="server" ImageUrl="~/printer-red.gif" CommandArgument='<%# Container.VisibleIndex%>' OnClick="btnTemplate_Click" ToolTip="Click Me" />
<asp:Button ID="btnTemplate2" runat="server" CommandArgument='<%# Container.VisibleIndex%>' OnClick="btnTemplate_Click" Text="Click Me" />

I the function, you need the commandargument as it contains the data. So declaring a new imagebutton and casting the sender to an imagebutton will give you the command argument irrespective of which button is clicked and making your code  work
AzaniahAuthor Commented:
I think I see.

So by using
ImageButton button = sender as ImageButton;

I'm effectively adding the functionality of the new button to the button that was clicked, and although that new button hasn't been clicked ( or called directly ) it's extending the fact that the first button was and so everything is performed.

If that's correct then I assume you could do the same with any type of object and any "onSomething" action?

masterpassCommented:
I'm effectively adding the functionality of the new button to the button that was clicked >>>>

Sort of, to be very precise, you are adding all the properties of the button that was clicked to the new button. Since the new button is created in the code and execution of the original button is not complete, there is no need for the new button to be clicked(you are not even adding the new button to the form, it is like a temporary variable holding the data ),

If that's correct then I assume you could do the same with any type of object and any "onSomething" action? >>>>>

In all most all cases, where you get the user input (like button, textfield) you could do the same

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
AzaniahAuthor Commented:
Thanks very much :)
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
Web Development

From novice to tech pro — start learning today.