Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to handle multiple commandfields in gridview.

My aspx code is:

<asp:GridView ID="GridOracle" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        DataKeyNames="DIS_CHAPTER_ID" OnSelectedIndexChanged="GridOracle_SelectedIndexChanged"
                        BackColor="#CCCCCC" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="3px"
                        CellPadding="4" CellSpacing="2" ForeColor="Black">
                        <%-- BorderColor=#999999--%>
                        <Columns>
                            <asp:BoundField DataField="SEC_NAME" HeaderText="Section Name" ReadOnly="True" ItemStyle-HorizontalAlign="Left"
                                ItemStyle-Font-Bold="true" ItemStyle-VerticalAlign="Middle">
                                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Font-Bold="True"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="CHAPTER_NAME" HeaderText="CourseName" ReadOnly="True"
                                ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle">
                                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="Duration" HeaderText="Duration" ItemStyle-HorizontalAlign="Left"
                                ItemStyle-VerticalAlign="Middle">
                                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
                            </asp:BoundField>
                            <asp:CommandField CausesValidation="False" InsertVisible="False" ShowCancelButton="False"
                                ButtonType="Image" ShowSelectButton="True" HeaderText="Material" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle" SelectImageUrl="~/Content/Images/PDFLogo.png">
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                            </asp:CommandField>
                            <asp:CommandField  CausesValidation="False" InsertVisible="False" ShowCancelButton="False" 
                                ButtonType="Image" ShowSelectButton="True" HeaderText="Material1" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle" SelectImageUrl="~/Content/Images/PDFLogo.png">
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                            </asp:CommandField>
                          </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>


My aspx.cs code for commandfield

protected void GridOracle_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (GridOracle.SelectedIndex > -1)
                {
                        this.Id = Convert.ToInt32(GridOracle.DataKeys[GridOracle.SelectedIndex].Value);
                        string _FName = "select FILE_NAME from OM_DOC_ALL where DIS_CHAPTER_ID = " + Id;
                        string _FileName = (string)SqlHelper.ExecuteScalar(strConn, CommandType.Text, _FName);
                        string FileName = "~/Downloads/OracleMaterial/" + _FileName + ".pdf";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
                        WebClient client = new WebClient();
                        string pdfPath = Server.MapPath("../Downloads/OracleMaterial/" + _FileName + ".pdf");
                        Byte[] buffer = client.DownloadData(pdfPath);
                        BinaryWriter bw = new BinaryWriter(Response.OutputStream);
                        bw.Write(buffer);
                        bw.Close();
                        Response.ContentType = "application/pdf";
                        Response.Flush();
                        Response.Close();
                    
                }
                else
                {
                    //Clear Controls
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.ErrorLog("PageLoad: ExploreJobs.aspx.cs ,  Error : " + ex.Message);
            }
        
        
    }



How to write 2 commandfields(Header text of Material & Material1) code.
Posted
Updated 23-Apr-12 1:03am
v3

Here: Using CommandField in a GridView control[^]

If needed, just Google/Bing for more!
 
Share this answer
 
Comments
Prasad_Kulkarni 23-Apr-12 6:37am    
Good Link! +5!
Sandeep Mewara 23-Apr-12 7:47am    
Thanks.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 23-Apr-12 6:37am    
Good Links! +5!
sravani.v 23-Apr-12 7:28am    
Thank you but where is 5 yar!
Prasad_Kulkarni 27-Apr-12 2:25am    
sorry dear, forget to make it.
countered now :)
sravani.v 27-Apr-12 2:38am    
Thank you Prasad
Prasad_Kulkarni 27-Apr-12 2:40am    
Not active in last couple of days, where're you?
work load?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900