通过PANEL动态添加TEXT和DropDownList,无法获取值!

westhdd 2009-09-15 12:07:27

<asp:Panel ID="Panel2" runat="server">

<center>
<b>添加非法信息</b>
<div style="width:600px;">
非法信息名称:<asp:TextBox ID="txterrname" runat="server" Width="350"></asp:TextBox> <br />
<div>

<div >

请输入非法信息记录行数:

<asp:TextBox runat="server" ID="txtTextCount"></asp:TextBox>



<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtTextCount" ValidationGroup="CreateTextBox" Display="Dynamic"

runat="server" ErrorMessage="只接受数字!" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>  

<asp:Button runat="server" ID="btnCreate" Text="创建记录非法信息记录行数" ValidationGroup="CreateTextBox"

onclick="btnCreate_Click" />  

</div>

<div runat="server" id="divControls" > </div>

<div runat="server" id="divMessage"> </div>
<asp:Panel ID="Panel4" runat="server">
</asp:Panel>
</div>

</div>

<asp:Button ID="btninsert" runat="server" Text="确认添加"
onclick="btninsert_Click" />
</center>

</asp:Panel>



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Error_ManagerError : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}

}
protected void btnadd_Click(object sender, EventArgs e)
{
if (this.txtaddtype.Text.Length < 1)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('对不起,非法信息类型不能为空!');</script>");
return;
}
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into errorinfo(name) values(@name)", cn))
{
cmd.Parameters.AddWithValue("@name", this.txtaddtype.Text);
int rows = -1;
try
{
cn.Open();
rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('恭喜你,非法信息类型添加成功!');</script>");
this.txtaddtype.Text = string.Empty;
return;
}
}
catch (Exception)
{

throw;
}
}
}
}

private DataSet GetErrorInfo()
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlDataAdapter cmd = new SqlDataAdapter("select * from errorinfo", cn))
{
DataSet ds = new DataSet();
try
{
cn.Open();
cmd.Fill(ds);


}
catch (Exception ex)
{
cn.Close();
throw new Exception(ex.Message);
}
return ds;
}
}
}
private void CreateTextBoxLis(int num)
{
Panel p;
TextBox t;
Label l;
DropDownList dr;
for (int i =0; i < num; i++)
{
//创建Panel
p = new Panel();
p.ID = "ptxt" + i.ToString();

//创建Lable
l = new Label();
l.ID = "lable" + i.ToString();
l.Text = "详细信息" + i.ToString();

//创建TextBox
t = new TextBox();
t.ID = "t" + i.ToString();
Page.Form.Controls.Add(t);
t.Attributes.Add("runat", "server");

//创建DropDownList控件
dr = new DropDownList();
dr.ID = "dr" + i.ToString();
dr.Attributes.Add("runat", "server");
dr.DataSource = GetErrorInfo();
dr.DataTextField = "name";
dr.DataValueField = "id";
dr.DataBind();

Panel4.Controls.Add(p);
Panel4.Controls.Add(l);
Panel4.Controls.Add(t);

Panel4.Controls.Add(dr);

}
}




protected void btnCreate_Click(object sender, EventArgs e)
{

if (!IsPostBack)
{



if (txtTextCount.Text == null || string.IsNullOrEmpty(txtTextCount.Text))
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('很抱歉,你输错了!');</script>");
return;
}
int txtCount = int.Parse(txtTextCount.Text);

// 注意:每次PostBack时,都需要重新动态创建TextBox

CreateTextBoxLis(txtCount);



}

}
protected void btninsert_Click(object sender, EventArgs e)
{


AddError();


}
private void AddError()
{
TextBox txt;
DropDownList dr;

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("insert into error(nickname,website,eid) values(@name,@website,@eid)", cn))
{
cmd.Parameters.AddWithValue("@name", this.txterrname.Text);

int f =int.Parse(this.txtTextCount.Text);
for (int i = 0; i < f; i++)
{
txt=this.Panel4.FindControl("t"+i.ToString())as TextBox;
dr=this.Panel4.FindControl("t"+i.ToString())as DropDownList;
//Response.Write(((TextBox)this.FindControl("t" + i.ToString())).Text);
cmd.Parameters.AddWithValue("@website", txt.Text);
cmd.Parameters.AddWithValue("eid",dr.SelectedValue );

}
int rows = -1;
try
{
cn.Open();
rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
Response.Write("<script>alert('添加成功')</script>");
}
else
Response.Write("<scirpt>alert('添加失败')</script>");
}
catch (Exception)
{
cn.Close();
throw;
}

}
}
}
protected void btnseach_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = false;
Panel3.Visible = true;
txtseach.Text = string.Empty;
}
private void GetShowError()
{
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select error.id,nickname,website,name,state from errorinfo inner join error on errorinfo.id=error.eid", cn))
{

try
{
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception)
{
cn.Open();
throw;
}

}
}
}
private void GetShowErrorByName(string name)
{

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select error.id,website,errorinfo.name as name,state,nickname from error inner join errorinfo on error.eid=errorinfo.id where nickname LIKE '%' + @no + '%' or website LIKE '%' + @url + '%'", cn))
{
cmd.Parameters.AddWithValue("@no", name);
cmd.Parameters.AddWithValue("@url", name);
try
{
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception ex)
{
cn.Close();
throw new Exception(ex.Message);
}
}
}
}

protected void brnseach_Click(object sender, EventArgs e)
{
if (this.txtseach.Text != null)
{
GetShowErrorByName(this.txtseach.Text);
}
else
{
GetShowError();
}
}

}
...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxmonsy 2009-09-16
  • 打赏
  • 举报
回复
真长。。。。
victarary 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 westhdd 的回复:]
protected void btnCreate_Click(object sender, EventArgs e)
{

if (!IsPostBack)
{



if (txtTextCount.Text == null || string.IsNullOrEmpty(txtTextCount.Text))
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('很抱歉,你输错了!');</script>");
return;
}
int txtCount = int.Parse(txtTextCount.Text);

// 注意:每次PostBack时,都需要重新动态创建TextBox

CreateTextBoxLis(txtCount);



}

}
[/Quote]
你在这个函数只会在点了按钮后才执行,那么页面一旦刷新又没有点按钮,Panel就没有加入控件,在后面AddError也就找不到了
westhdd 2009-09-16
  • 打赏
  • 举报
回复
楼上,我就是用你的那个方法找的,在我第二次提交的时候页面的控件还在,没有值
mainbob 2009-09-16
  • 打赏
  • 举报
回复
我也遇到同样的问题,msg是panel外label控件
msg.Text = (this.InputPanel.FindControl("model") as dropdownlist).SelectItems.Text;
总是取不到panel中dropdownlist控件model的值,哪位高人来帮忙指点一下,谢谢啦
wuyq11 2009-09-15
  • 打赏
  • 举报
回复

TxtBox txt=this.Panel1.FindControl("TextBox1") as TextBox;
DropDownList ddl=this.Panel1.FindControl("DropDownList1") as DropDownList;
feixian49 2009-09-15
  • 打赏
  • 举报
回复
看看这个帖子:
http://topic.csdn.net/u/20090914/13/7b89058d-8d85-45e8-8d71-4e0fc513d1d2.html
jerry_zuo 2009-09-15
  • 打赏
  • 举报
回复
LZ 这家伙 也太长了 ,看的累~~~

调试嘛。调试能解决的就不是问题了。
saishangpeiqian 2009-09-15
  • 打赏
  • 举报
回复
还是调试一下,太长了

62,041

社区成员

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

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

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

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