Link to home
Start Free TrialLog in
Avatar of Azaniah
AzaniahFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of masterpass
masterpass
Flag of India image

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
Avatar of Azaniah

ASKER

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?

ASKER CERTIFIED SOLUTION
Avatar of masterpass
masterpass
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Azaniah

ASKER

Thanks very much :)