4 Replies - 14411 Views - Last Post: 12 April 2011 - 07:58 PM

#1 xtr3mnico   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Update Panel and GridView Custom Buttons

Posted 12 April 2011 - 06:18 PM

Initially it was some trouble at first getting the update panel to work. Essentially, i got the hang of it and start to go further. At the moment i am stuck with trying to update my gridview using buttons[templated buttons] that are on the gridview. Seems pretty straight forward just do
ChildrenAsTriggers = "true"
and it works... I tried that... the button gets called it retrieves the right data from the database but... just stops.. i even add a
UpdatePanelArticle.Update();
and yea my luck it didnt work. Anyway here is my aspx / markup code. Irrelevant code are excluded.

           <asp:UpdatePanel ID="UpdatePanelArticle" runat="server" ChildrenAsTriggers = "true">
           <ContentTemplate>
               <asp:GridView ID="ArticleGridView" runat="server" AutoGenerateColumns="False" 
            CellPadding="3" DataSourceID="artgvDataSource" 
            onselectedindexchanged="ArticleGridView_SelectedIndexChanged" 
            DataKeyNames="ID" BackColor="#DEBA84" BorderColor="#DEBA84" 
               BorderStyle="None" BorderWidth="1px" CellSpacing="2" ondatabound="ArticleGridView_DataBound" EmptyDataText="There are no articles found of that type">
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:TemplateField ShowHeader="False" HeaderText = "Articles">
                    <ItemTemplate>
                    <table id = "table1" cellpadding = "3" cellspacing = "2" width = "800px">
                        <tr>
                            <td align = "left" >
                                <strong><asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label></strong> <br />
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label> <br />
                               <asp:Button ID="Downloadbtn" runat="server" CausesValidation="False" CommandName="Select" Text="Download" 
                            CommandArgument='<%# bind("ID") %>' />  <asp:LinkButton ID="Viewbtn" runat="server" CausesValidation="False" CommandArgument='<%# bind("ID") %>' CommandName="Cancel" 
                                onclick="Viewbtn_Click" Text="Read Article"/> <br />
                            </td>
                            <td width = "150px">
                            Image <br />
                            Author: <asp:Label ID="Label2" runat="server" Text='<%# Bind("UserProf") %>'></asp:Label><br />
                            Date: <asp:Label ID="Label5" runat="server" Text='<%# Bind("DatePosted") %>'></asp:Label>
                            </td>
                        </tr>
                    </table>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="View Article" Visible ="false">
                    <ItemTemplate>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name" SortExpression="Name" Visible ="false" >
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UserProf" SortExpression="UserProf" 
                    Visible="False">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserProf") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>

                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID" SortExpression="ID" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description" SortExpression="Description" Visible ="false">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DatePosted" SortExpression="DatePosted" Visible = "false">
                    <ItemTemplate>

                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DatePosted") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView></ContentTemplate>
               <Triggers>
                   <asp:AsyncPostBackTrigger ControlID = "IBSearch" EventName="Click" />
               </Triggers>
        </asp:UpdatePanel>


download c#:
    protected void ArticleGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        String path = ""; String filename = ""; String ext = "";
        SWOPPDataContext ds = new SWOPPDataContext();
       // Button btn = (Button)sender;

        var articles = from art in ds.Articles
                       where art.ID.ToString() == ArticleGridView.SelectedDataKey.Value.ToString()
                       select new { art.URL, art.Name, art.Type };

        foreach (var info in articles)
        {
            path = info.URL;
            filename = info.Name;
            ext = info.Type;
        }
        try
        {
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
            if (ext == ".doc" || ext == ".docx" || ext == ".rtf")
            {
                Response.ContentType = "Application/msword";
                Response.TransmitFile(path);
            }
            else if (ext == ".pdf")
            {
                Response.ContentType = "Application/pdf";
                Response.TransmitFile(path);
            }
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write("Error occured\n" + ex.Message);
        }
        UpdatePanelArticle.Update();
    }


The search works perfectly noteable the IBSearch control ID for the button. All i want to work now is my download and view button which are select and custom button respectively.

I tried the onrowcreated to register the control as to the scriptmanager as a asyncpostbackControl and:
1) i dont think i fully know if that will work due to the fact that the button is hidden
2) i do not have enough knowledge to validate its effectiveness

Any idea of how to get this working?

This post has been edited by xtr3mnico: 12 April 2011 - 07:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Update Panel and GridView Custom Buttons

#2 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Update Panel and GridView Custom Buttons

Posted 12 April 2011 - 07:40 PM

You can't open files (excel, word, etc.) using an ajax call. You'll either need to take it out of the update panel or use a postbacktrigger
Was This Post Helpful? 0
  • +
  • -

#3 xtr3mnico   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: Update Panel and GridView Custom Buttons

Posted 12 April 2011 - 07:45 PM

postbacktrigger sounds best to me any idea of how i can start heading in that direction?
Was This Post Helpful? 0
  • +
  • -

#4 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Update Panel and GridView Custom Buttons

Posted 12 April 2011 - 07:52 PM

do it about the same as you do an asyncpostbacktrigger, provide the control ID that is going to generate the full page postback, in this case the GridView since it's onselectedindexchanged event is what should cause the postback, at least from what I can tell
Was This Post Helpful? 0
  • +
  • -

#5 xtr3mnico   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: Update Panel and GridView Custom Buttons

Posted 12 April 2011 - 07:58 PM

EDIT: Found the solution... i'll share it knowing that others can benefit:

Since the button is inside the gridview it throws an [b]OnDataBinding[/b ]event when the button is clicked.

This is what i used to give my postback control. so in the markup or aspx you want to ensure that you have the function which we will code for defined
ex. In my case i had:
 <asp:LinkButton ID="Viewbtn"  runat="server" CausesValidation="False" CommandArgument='<%# bind("ID") %>' CommandName="Cancel" 
                                onclick="Viewbtn_Click"  OnDataBinding = "PostBackBind_DataBinding" Text="Read Article"/>



C# for the function
ScriptManager1 is on my master.page
/* CAST the sender object to a linkbutton to immitate the button being clicked by an object
     * find the scriptmanager on the master page[If it is located on that page else just write the ID for it]
     * and register the control of the specific button clicked on the page as a postback control
     */ 
    protected void PostBackBind_DataBinding(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender; 
        ScriptManager sm = (ScriptManager)Page.Master.FindControl("ScriptManager1");
        sm.RegisterPostBackControl(lb);
    }

This post has been edited by xtr3mnico: 12 April 2011 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1