Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 23, 2012 01:14 PM by karthicks
Member
5 Points
55 Posts
Apr 23, 2012 11:06 AM|LINK
Hi, How to handle multiple commandfields in gridview.
My aspx code is:
<pre type="c#">
<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>
</pre>
My aspx.cs code for commandfield
<pre type="html"> 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); } } </pre>
How to write 2 commandfields(Header text of Material & Material1) code.
C
All-Star
31334 Points
5414 Posts
Apr 23, 2012 12:07 PM|LINK
hi, i will suggest you to use ButtonField instead of CommandField
<asp:ButtonField HeaderText="Material" CommandName="Material" Text="Select" /> <asp:ButtonField HeaderText="Material1" CommandName="Material1" Text="Select" /> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex; switch (e.CommandName) { case "Material": Response.Write("Material"); break; case "Material1": Response.Write("Material1"); break; } }
Refer :http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
http://www.dreamincode.net/forums/topic/184448-using-gridview-and-rowcommand-events/
http://www.c-sharpcorner.com/uploadfile/syedshakeer/rowcommand-event-in-gridview/
Apr 23, 2012 12:58 PM|LINK
Using RowCommand property how to come id value
Apr 23, 2012 01:14 PM|LINK
hi, you can get id like below
<asp:ButtonField HeaderText="Material" CommandName="Material" Text="Select" /> <asp:ButtonField HeaderText="Material1" CommandName="Material1" Text="Select" /> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex; int Id = Convert.ToInt32(GridOracle.DataKeys[index].Value); switch (e.CommandName) { case "Material": Response.Write("Material"); break; case "Material1": Response.Write("Material1"); break; } }
if you statisfied with my ans. mark both the replies as answered one.
sampath1750
Member
5 Points
55 Posts
How to handle more than one commandfields in gridview
Apr 23, 2012 11:06 AM|LINK
Hi,
How to handle multiple commandfields in gridview.
My aspx code is:
<pre type="c#">
<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>
</pre>
My aspx.cs code for commandfield
<pre type="html">
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);
}
}
</pre>
How to write 2 commandfields(Header text of Material & Material1) code.
C
karthicks
All-Star
31334 Points
5414 Posts
Re: How to handle more than one commandfields in gridview
Apr 23, 2012 12:07 PM|LINK
hi, i will suggest you to use ButtonField instead of CommandField
<asp:ButtonField HeaderText="Material" CommandName="Material" Text="Select" /> <asp:ButtonField HeaderText="Material1" CommandName="Material1" Text="Select" /> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex; switch (e.CommandName) { case "Material": Response.Write("Material"); break; case "Material1": Response.Write("Material1"); break; } }Refer :http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
http://www.dreamincode.net/forums/topic/184448-using-gridview-and-rowcommand-events/
http://www.c-sharpcorner.com/uploadfile/syedshakeer/rowcommand-event-in-gridview/
C
Karthick S
sampath1750
Member
5 Points
55 Posts
Re: How to handle more than one commandfields in gridview
Apr 23, 2012 12:58 PM|LINK
Using RowCommand property how to come id value
karthicks
All-Star
31334 Points
5414 Posts
Re: How to handle more than one commandfields in gridview
Apr 23, 2012 01:14 PM|LINK
hi, you can get id like below
<asp:ButtonField HeaderText="Material" CommandName="Material" Text="Select" /> <asp:ButtonField HeaderText="Material1" CommandName="Material1" Text="Select" /> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex; int Id = Convert.ToInt32(GridOracle.DataKeys[index].Value); switch (e.CommandName) { case "Material": Response.Write("Material"); break; case "Material1": Response.Write("Material1"); break; } }if you statisfied with my ans. mark both the replies as answered one.
Karthick S