高手进来,看看如何批量更新datalist模板列里的信息

happstar 2009-04-09 12:50:00
我想批量提交datalist模板列里的数据,但是却取不到模板列里文本框和单选按钮的值,请高手帮忙看一下.

前台页面代码:
<body>
<form id="form1" runat="server">
<uc1:LocationBar ID="Navigate" runat="server" />
<table class="tar w97">
<tr>
<td align="left">
<asp:RadioButton ID="btnOne" runat="server" AutoPostBack="True" Checked="True" GroupName="Test"
Text="填报本条计划" OnCheckedChanged="btnOne_CheckedChanged" />
<asp:RadioButton ID="btnTwo" runat="server" AutoPostBack="True" GroupName="Test"
Text="填报所有计划" OnCheckedChanged="btnTwo_CheckedChanged" />
</td>
<td align="right">
<asp:Button ID="btnOk" CssClass="butt_bg" runat="server" Text=" 提 交 " OnClick="btnOk_Click" /> 
<asp:Button ID="btCancel" CssClass="butt_bg" CausesValidation="false" runat="server"
Text=" 取 消 " />
</td>
</tr>
</table>
<asp:DataList ID="dlDetail" runat="server" BorderStyle="None" BorderWidth="0px" RepeatLayout="table"
GridLines="Horizontal">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<SelectedItemStyle Font-Bold="True"></SelectedItemStyle>
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" class="w97" style="margin-bottom: 0px;">
<tr>
<asp:HiddenField ID="hidId" Value='<%# DataBinder.Eval(Container.DataItem,"pdid") %>'
runat="server" />
<td width="120">
计划内容</td>
<td class="tal">
<%# DataBinder.Eval(Container.DataItem, "content")%>
</td>
</tr>
<tr>
<td>
完成情况</td>
<td class="tal">
<asp:RadioButton ID="btnR1" runat="server" Checked="True" GroupName="R" Text="完成" />
<asp:RadioButton ID="btbR2" runat="server" GroupName="R" Text="未完成" />
</td>
</tr>
<tr>
<td>
备注说明</td>
<td class="tal">
<asp:TextBox ID="txtNote" TextMode="MultiLine" Rows="5" Width="300px" runat="server"></asp:TextBox></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</form>
</body>

后台代码:

protected void btnOk_Click(object sender, EventArgs e)
{
for (int i = 0; i < dlDetail.Items.Count; i++)
{
HiddenField hidId = dlDetail.Items[i].FindControl("hidId") as HiddenField;
TextBox t = dlDetail.Items[i].FindControl("txtNote") as TextBox;
RadioButton r1 = dlDetail.Items[i].FindControl("btnR1") as RadioButton;
RadioButton r2 = dlDetail.Items[i].FindControl("btnR2") as RadioButton;

Response.Write("txtNode= "+ t.Text);
Response.Write("r2= "+ r2.Checked);
}
}
...全文
214 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
happstar 2009-04-09
  • 打赏
  • 举报
回复
谢谢大家的热心帮助.我新建了一个页面,用原来的代码居然OK了.
编程有钱人了 2009-04-09
  • 打赏
  • 举报
回复

//完整的
protected void btnOk_Click(object sender, EventArgs e)
{
IList<string> l = new List<string>();
for (int i = 0; i <this.DataList1.Items.Count; i++)
{
CheckBox c= (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
TextBox tb = (TextBox)this.DataList1.Items[i].FindControl("TextBox1");
//下面几个TextBox省略
if(c.Checked)
{
l.Add("update 表 set='"+tb.Text+"' where id="+ this.DataList1.DataKeys[i].ToString());
}
}
SqlServerHelper.ExecuteSqlTran(l);
}


SqlServerHelper.cs

//数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.
public static string connectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(IList<string> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
编程有钱人了 2009-04-09
  • 打赏
  • 举报
回复

protected void btnOk_Click(object sender, EventArgs e)
{
string upsql = "update 表 set=123 where id=";//省略其他SET
IList<string> l = new List<string>();
for (int i = 0; i <this.DataList1.Items.Count; i++)
{
CheckBox c= (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
TextBox tb = (TextBox)this.DataList1.Items[i].FindControl("TextBox1");
//下面几个TextBox省略
if(c.Checked)
{
l.Add("update 表 set='"+tb.Text+"' where id="+ this.DataList1.DataKeys[i].ToString());
}
}
SqlServerHelper.ExecuteSqlTran(l);
}
SqlServerHelper.cs类库
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(IList<string> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}


sohighthesky 2009-04-09
  • 打赏
  • 举报
回复
这是后台代码,前台只改了绑定字段的:
    protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
dlDetail.DataSource = (new DAO()).GetTopNews();
dlDetail.DataBind();
}
protected void btnOk_Click(object sender, EventArgs e)
{
string str = "";
for (int i = 0; i < dlDetail.Items.Count; i++)
{
TextBox txt = dlDetail.Items[i].FindControl("txtNote") as TextBox;
str += txt.Text;
RadioButton r1 = dlDetail.Items[i].FindControl("btnR1") as RadioButton;
Response.Write("<script>alert('" + r1.Checked + "')</script>");
}
Response.Write("<script>alert('"+str+"')</script>");
}
happstar 2009-04-09
  • 打赏
  • 举报
回复
嗯,我再用我的代码试试.我重新做一个页面.今天真见鬼了.
编程有钱人了 2009-04-09
  • 打赏
  • 举报
回复

//其实也不用 HiddenField 也可以
//这样
DataTable dt= Get2005Pager("testtab", "*", "id", "", this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, 1,out sss);
this.DataList1.DataSource = dt;
this.DataList1.DataKeyField = "id";//数据库里主键字段
this.DataList1.DataBind()
//获得
for (int i = 0; i <this.DataList1.Items.Count; i++)
{
string keyid = this.DataList1.DataKeys[i].ToString();
CheckBox t = (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
if(t.Checked)
{
Response.Write("txtNode= " + t.Text);
Response.Write("r2= " + r2.Checked);
}

}
happstar 2009-04-09
  • 打赏
  • 举报
回复
晕.把你做的页面发给我看看.我要撞墙了.本来我觉的我的写法是可行的,谁知道今天倒大霉了,这个小问题弄的我一天什么也没干.
sohighthesky 2009-04-09
  • 打赏
  • 举报
回复
是按你的只绑定了”计划内容“,然后填写值就能取出来了
如果什么都不绑定的话,不会显示表单的
happstar 2009-04-09
  • 打赏
  • 举报
回复
又没有人来帮忙了?还有人吗?
happstar 2009-04-09
  • 打赏
  • 举报
回复
你取出来的值是最开始页面初始化时的值.如果你自己再选择那个单选按钮,和给文本框填写内容,点提交按钮,这时就取不出来值了.
happstar 2009-04-09
  • 打赏
  • 举报
回复
你们可能是直接读取初始化时绑定的数据了,所以能读出来.我的这个功能是页面加载完后,在list的模板列里选择单选按钮,填写文本框内容以后,你再点击提交按钮,这时候肯定读不出来数据,就算你读出来的数据,也是初始化时的数据.而不是后来手动填写的数据.
maggie3256 2009-04-09
  • 打赏
  • 举报
回复
foreach (RepeaterItem di in ReProducts.Items)
{
if (((System.Web.UI.WebControls.CheckBox)(di.Controls[1])).Checked == true)
{
comid = ((HtmlInputControl)di.Controls[3]).Value;
}
}
happstar 2009-04-09
  • 打赏
  • 举报
回复
晕.不会吧.我就是死活取不出来.熬了一天了.
sohighthesky 2009-04-09
  • 打赏
  • 举报
回复
我晕,楼主你的代码我直接复制过去就可以运行,
能取出里面的值
happstar 2009-04-09
  • 打赏
  • 举报
回复
顶顶顶,顶起来,不信没人来帮我忙.
sohighthesky 2009-04-09
  • 打赏
  • 举报
回复
等下
happstar 2009-04-09
  • 打赏
  • 举报
回复
再顶,怎么没有人来帮忙啊.
happstar 2009-04-09
  • 打赏
  • 举报
回复
没有高手来看看这个问题吗?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧