GridView的Footer显示添加按钮
2010-09-01 11:32
316 查看
1、首先添加GridView
2、设置模板
<Columns>
<asp:TemplateField HeaderText="账号">
<ControlStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="lbID" runat="server" Text='<%# Bind("EmpID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ControlStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="lbRealName" runat="server" Text='<%# Bind("EmpRealName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRealName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="lbSex" runat="server" Text='<%# Bind("EmpSex") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSex" runat="server">
<asp:ListItem Value="男">男</asp:ListItem>
<asp:ListItem Value="女">女</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="住址">
<ControlStyle Width="200px" />
<ItemTemplate>
<asp:Label ID="lbAddress" runat="server" Text='<%# Bind("EmpAddress") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddress" runat="server" Width="80px"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取 消" OnClick="btnCancel_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
3
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string sqlStr = "select * from Employee";
DataSet myds = Common.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = false;
bind();
//Functions.Alert("Cancel");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
TextBox empID = GridView1.FooterRow.FindControl("txtID") as TextBox;
TextBox empRealName = GridView1.FooterRow.FindControl("txtRealName") as TextBox;
DropDownList empSex = GridView1.FooterRow.FindControl("ddlSex") as DropDownList;
TextBox empAddress = GridView1.FooterRow.FindControl("txtAddress") as TextBox;
string sql = "insert into Employee(EmpID,EmpRealName,EmpSex,EmpAddress) values('" + empID.Text.ToString() + "','" + empRealName.Text.ToString() + "','" + empSex.SelectedValue.ToString() + "','" + empAddress.Text.ToString() + "')";
Common.ExecuteSql(sql);
bind();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = true;
bind();
}
2、设置模板
<Columns>
<asp:TemplateField HeaderText="账号">
<ControlStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="lbID" runat="server" Text='<%# Bind("EmpID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<ControlStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="lbRealName" runat="server" Text='<%# Bind("EmpRealName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRealName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="lbSex" runat="server" Text='<%# Bind("EmpSex") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSex" runat="server">
<asp:ListItem Value="男">男</asp:ListItem>
<asp:ListItem Value="女">女</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="住址">
<ControlStyle Width="200px" />
<ItemTemplate>
<asp:Label ID="lbAddress" runat="server" Text='<%# Bind("EmpAddress") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddress" runat="server" Width="80px"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click" />
<asp:Button ID="btnCancel" runat="server" Text="取 消" OnClick="btnCancel_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
3
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string sqlStr = "select * from Employee";
DataSet myds = Common.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = false;
bind();
//Functions.Alert("Cancel");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
TextBox empID = GridView1.FooterRow.FindControl("txtID") as TextBox;
TextBox empRealName = GridView1.FooterRow.FindControl("txtRealName") as TextBox;
DropDownList empSex = GridView1.FooterRow.FindControl("ddlSex") as DropDownList;
TextBox empAddress = GridView1.FooterRow.FindControl("txtAddress") as TextBox;
string sql = "insert into Employee(EmpID,EmpRealName,EmpSex,EmpAddress) values('" + empID.Text.ToString() + "','" + empRealName.Text.ToString() + "','" + empSex.SelectedValue.ToString() + "','" + empAddress.Text.ToString() + "')";
Common.ExecuteSql(sql);
bind();
}
protected void btnInsert_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = true;
bind();
}
相关文章推荐
- ASP GridView绑定数据为空时,显示footer和header,以实现添加
- 在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能/新建页面DataXML,使用GridView显示book.xm中的图书信息
- C# winform 上传图片,显示图片和gridview中添加按钮
- DEV GridView中添加按钮列
- 带添加按钮的GridView,item的删除事件
- 利用GridView显示主细表并添加打开、关闭功能
- 高德地图显示在fragment中并添加按钮点击事件
- WordPress添加显示和隐藏侧边栏按钮开关
- 给ubuntu 12.04 添加显示桌面按钮的方法
- 利用GridView显示主细表并添加删除、打开、关闭功能(续)
- 如何用数据驱动实现通用化巡检APP(如何一个界面里添加多个点击拍照按钮并显示在不同的imageview里)
- 关于在GridView模版里添加DropDownList实现根据下拉列表的不同选择gridview根据条件筛选显示。
- 窗体上有两个文本框:一个文本框中最多输入字符6个;一个文本框中输入任何内容都显示*号。再添加一个按钮、2个单选按钮。实现单击按钮后,根据单选按钮,将对应文本框中内容显示在标签
- GridView中添加图片显示
- 当GridView没有数据时显示Header或Footer
- SQLite数据库储存图片信息,GridView显示,基本实现添加、删除图标操作 ... ...
- DEV GridView中添加按钮列
- GridView中为按钮添加相应的事件
- 利用GridView显示主细表并添加删除、打开、关闭功能(转自孟子E章)
- Gridview控件添加编辑按钮时候需编写的函数》