GridView控件中加入动态生成的控件
2007-11-09 16:03
525 查看
前台代码如下:
<asp:GridView ID="testDatagrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="testDatagrid_RowDataBound" Width="294px" Height="134px">
<Columns>
<asp:CheckBoxField HeaderText="可选" />
<asp:BoundField DataField="ToolName" HeaderText="text" />
<asp:TemplateField HeaderText="数量"></asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="数据的更新" />
后台代码实现如下:
#region GridView绑定动态生成的控件
protected void testDatagrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
CheckBox check = new CheckBox();
check.ID = e.Row.RowIndex.ToString();
check.AutoPostBack = true;
check.CheckedChanged += new EventHandler(check_CheckedChanged);
e.Row.Cells[0].Controls.Add(check);
TextBox text = new TextBox();
text.Width = 15;
text.ID = "txt" + e.Row.RowIndex.ToString();
text.Enabled = false;
e.Row.Cells[2].Controls.Add(text);
HiddenField hidden = new HiddenField();
hidden.Value = da.Tables[0].Rows[e.Row.RowIndex][0].ToString();
hidden.ID = "Hidd" + e.Row.RowIndex.ToString();
e.Row.Cells[2].Controls.Add(hidden);
}
}
#endregion
#region 处理checkbox所触发的事件
void check_CheckedChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(((CheckBox)sender).ID);
TextBox box = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
box.Enabled = true;
}
#endregion
#region 将客户所选的数据插入数据库
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.testDatagrid.Rows.Count; i++)
{
CheckBox chB = this.testDatagrid.Rows[i].Cells[0].FindControl(i.ToString()) as CheckBox;
TextBox txB = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
HiddenField hid = this.testDatagrid.Rows[i].Cells[2].FindControl("Hidd" + i.ToString()) as HiddenField;
int ii = -1;
if (chB.Checked)
{
if (txB.Text != null)
{
try
{
//引用 DataBase.dll
ii = new ZHXKDatabase(connStr).RunSQLCommandReturnInt("insert into Relation (ToolsID,TagID,[Desc]) values (" + hid.Value + "," + 12 + "," + int.Parse(txB.Text.Trim()) + ")");
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
if (ii == 1)
{
Response.Write("OK!");
}
}
}
}
#endregion
<asp:GridView ID="testDatagrid" runat="server" AutoGenerateColumns="False" OnRowDataBound="testDatagrid_RowDataBound" Width="294px" Height="134px">
<Columns>
<asp:CheckBoxField HeaderText="可选" />
<asp:BoundField DataField="ToolName" HeaderText="text" />
<asp:TemplateField HeaderText="数量"></asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="数据的更新" />
后台代码实现如下:
#region GridView绑定动态生成的控件
protected void testDatagrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > -1)
{
CheckBox check = new CheckBox();
check.ID = e.Row.RowIndex.ToString();
check.AutoPostBack = true;
check.CheckedChanged += new EventHandler(check_CheckedChanged);
e.Row.Cells[0].Controls.Add(check);
TextBox text = new TextBox();
text.Width = 15;
text.ID = "txt" + e.Row.RowIndex.ToString();
text.Enabled = false;
e.Row.Cells[2].Controls.Add(text);
HiddenField hidden = new HiddenField();
hidden.Value = da.Tables[0].Rows[e.Row.RowIndex][0].ToString();
hidden.ID = "Hidd" + e.Row.RowIndex.ToString();
e.Row.Cells[2].Controls.Add(hidden);
}
}
#endregion
#region 处理checkbox所触发的事件
void check_CheckedChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(((CheckBox)sender).ID);
TextBox box = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
box.Enabled = true;
}
#endregion
#region 将客户所选的数据插入数据库
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.testDatagrid.Rows.Count; i++)
{
CheckBox chB = this.testDatagrid.Rows[i].Cells[0].FindControl(i.ToString()) as CheckBox;
TextBox txB = this.testDatagrid.Rows[i].Cells[2].FindControl("txt" + i.ToString()) as TextBox;
HiddenField hid = this.testDatagrid.Rows[i].Cells[2].FindControl("Hidd" + i.ToString()) as HiddenField;
int ii = -1;
if (chB.Checked)
{
if (txB.Text != null)
{
try
{
//引用 DataBase.dll
ii = new ZHXKDatabase(connStr).RunSQLCommandReturnInt("insert into Relation (ToolsID,TagID,[Desc]) values (" + hid.Value + "," + 12 + "," + int.Parse(txB.Text.Trim()) + ")");
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
if (ii == 1)
{
Response.Write("OK!");
}
}
}
}
#endregion
相关文章推荐
- (转)GridView控件中加入动态生成的控件
- 动态生成表格呈现还是将表格直接绑定gridview等控件呈现的开发方式选择依据
- 动态生成GridView时,加入DataKeyNames属性,回调时出错解决方法
- 使用GridView做出列选择效果(获取动态生成的控件.鼠标点选GridView.Ajax控件)
- 动态生成GridView列,加入修改等按钮
- C#动态生成控件以及添加事件处理
- [C#] 动态生成GridView的模版列TemplateField,并在模版列TemplateField中添加复选框CheckBox
- 关于动态生成控件和动态获得控件的句柄值
- 在asp.net中动态生成web控件【文章来源:开店乐KaiDianLe.Com】
- ASP.NET动态加载用户控件的页面生成过程
- 动态创建服务器控件,获取HTML输出(用于生成静态文件,简化页面,简化编程等)
- C# 动态生成窗口中的控件不显示的解决方法
- 动态生成表单与调用动态生成的控件
- MFC 对话框中控件动态生成与删除
- JavaScript动态生成控件的方法
- GridView自动生成列时,利用DIV控制显示长宽 控件数据行和表头文字不换行
- 动态生成多个VBE按钮控件并操作它们的属性
- GridView自动生成列时,利用DIV控制显示长宽 控件数据行和表头文字不换行
- asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现
- UpdateCommand,DeleteCommand 的动态 SQL 生成 及GridView