<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>english</asp:ListItem>
<asp:ListItem>hindi</asp:ListItem>
<asp:ListItem>nepali</asp:ListItem>
<asp:ListItem>science</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /><br /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("name") %>'></asp:Label> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:RadioButtonList ID="rd1" runat="server"
RepeatDirection="Horizontal" ViewStateMode="Enabled">
<asp:ListItem>english</asp:ListItem>
<asp:ListItem>hindi</asp:ListItem>
<asp:ListItem>nepali</asp:ListItem>
<asp:ListItem>science</asp:ListItem>
</asp:RadioButtonList></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="submit all"
onclick="Button2_Click" /><br />
<asp:Button ID="Button3" runat="server" Text="Button" onclick="Button3_Click" />
</div>
<asp:GridView runat="server" ID="GridView2" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="UserId" />
<asp:BoundField DataField="Name" HeaderText="UserName" />
<asp:BoundField DataField="sub" HeaderText="LastName" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
SqlDataAdapter da;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sub = "";
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected == true)
{
sub = RadioButtonList1.Items[i].ToString();
break;
}
}
cmd = new SqlCommand("insert into test(name,sub) values(@name,@sub)", con);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@sub", sub);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void bindgrid()
{
da = new SqlDataAdapter("select name from test",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
string sub = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lblname = (Label)GridView1.Rows[i].FindControl("lbl1");
RadioButtonList rd = (RadioButtonList)GridView1.Rows[i].FindControl("rd1");
//sub = (GridView1.Rows[0].FindControl("rd1") as RadioButtonList).SelectedValue;
for (int j = 0; j < rd.Items.Count; j++)
{
if (rd.Items[j].Selected == true)
{
sub = rd.Items[j].ToString();
break;
}
}
cmd = new SqlCommand("insert into test(name,sub) values(@name,@sub)", con);
cmd.Parameters.AddWithValue("@name", lblname.Text);
cmd.Parameters.AddWithValue("@sub", sub);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select (name+':'+sub) as name from Test",con);
DataTable dt = new DataTable();
da.Fill(dt);
ExportToExcel(dt, "abc");
}
void ExportToExcel(DataTable dt, string FileName)
{
if (dt.Rows.Count > 0)
{
string filename = FileName + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition",
"attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>english</asp:ListItem>
<asp:ListItem>hindi</asp:ListItem>
<asp:ListItem>nepali</asp:ListItem>
<asp:ListItem>science</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" /><br /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("name") %>'></asp:Label> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:RadioButtonList ID="rd1" runat="server"
RepeatDirection="Horizontal" ViewStateMode="Enabled">
<asp:ListItem>english</asp:ListItem>
<asp:ListItem>hindi</asp:ListItem>
<asp:ListItem>nepali</asp:ListItem>
<asp:ListItem>science</asp:ListItem>
</asp:RadioButtonList></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="submit all"
onclick="Button2_Click" /><br />
<asp:Button ID="Button3" runat="server" Text="Button" onclick="Button3_Click" />
</div>
<asp:GridView runat="server" ID="GridView2" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="UserId" />
<asp:BoundField DataField="Name" HeaderText="UserName" />
<asp:BoundField DataField="sub" HeaderText="LastName" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
SqlDataAdapter da;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sub = "";
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected == true)
{
sub = RadioButtonList1.Items[i].ToString();
break;
}
}
cmd = new SqlCommand("insert into test(name,sub) values(@name,@sub)", con);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@sub", sub);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void bindgrid()
{
da = new SqlDataAdapter("select name from test",con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
string sub = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lblname = (Label)GridView1.Rows[i].FindControl("lbl1");
RadioButtonList rd = (RadioButtonList)GridView1.Rows[i].FindControl("rd1");
//sub = (GridView1.Rows[0].FindControl("rd1") as RadioButtonList).SelectedValue;
for (int j = 0; j < rd.Items.Count; j++)
{
if (rd.Items[j].Selected == true)
{
sub = rd.Items[j].ToString();
break;
}
}
cmd = new SqlCommand("insert into test(name,sub) values(@name,@sub)", con);
cmd.Parameters.AddWithValue("@name", lblname.Text);
cmd.Parameters.AddWithValue("@sub", sub);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select (name+':'+sub) as name from Test",con);
DataTable dt = new DataTable();
da.Fill(dt);
ExportToExcel(dt, "abc");
}
void ExportToExcel(DataTable dt, string FileName)
{
if (dt.Rows.Count > 0)
{
string filename = FileName + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition",
"attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
}
No comments:
Post a Comment