我在updatepanel中创建了控件但是FindControl不能获取控件,我的代码是这样的<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="True" Width="120px"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" Width="120px" OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
</asp:ListBox>
<asp:ListBox ID="ListBox3" runat="server" AutoPostBack="True" Width="120px" OnSelectedIndexChanged="ListBox3_SelectedIndexChanged"></asp:ListBox>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:Table ID="tableshow" runat="server">
</asp:Table>
<span id="shownone" onclick="shownone();">查看更多...</span>
<asp:Table ID="tablenone" runat="server" CssClass="mycss">
</asp:Table>
</asp:Panel>
<asp:TextBox ID="LblNone" runat="server" Text="" CssClass="mycss"></asp:TextBox>
<input type="hidden" id="lblvalue" runat="server" value="" />
</ContentTemplate>
</asp:UpdatePanel>protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable AttrTable = Category_attributeManager.Get_Category_attribute_By_CategoryId(Convert.ToInt32(this.ListBox3.SelectedItem.Value),1);
DataTable AttrValueTable = null;
foreach (DataRowView dv in AttrTable.DefaultView)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableCell tcdt = new TableCell();
DropDownList drl = null;
Label lbattr = new Label();
lbattr.ID = "lbl" + dv["attrId"].ToString();
lbattr.Text = dv["attrName"].ToString() + " : ";
tc.Controls.Add(lbattr); AttrValueTable = Category_valueManager.Get_Category_value_By_AttrId(Convert.ToInt32(dv["attrId"]));
if (AttrValueTable.Rows.Count != 0)
{
drl = new DropDownList();
drl.DataSource = AttrValueTable.DefaultView;
drl.DataTextField = "title";
drl.DataValueField = "valueId";
drl.AppendDataBoundItems = true;
drl.Items.Add("==请选择==");
drl.ID = "AttrDropDownList" + dv["attrId"].ToString();
drl.DataBind();
tcdt.Controls.Add(drl);
this.LblNone.Text += lbattr.ID + "-" + drl.ID + ",";
}
else
{
TextBox tbox = new TextBox();
tbox.ID = "AttrTextBox" + dv["attrId"].ToString();
tcdt.Controls.Add(tbox);
this.LblNone.Text += lbattr.ID + "-" + tbox.ID + ",";
}
tr.Cells.Add(tc);
tr.Cells.Add(tcdt);
this.tableshow.Rows.Add(tr);
} DataTable NoneTable = Category_attributeManager.Get_Category_attribute_By_CategoryId(Convert.ToInt32(this.ListBox3.SelectedItem.Value), 0);
DataTable NoneValueTable = null;
foreach (DataRowView dv in NoneTable.DefaultView)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableCell tcdt = new TableCell();
DropDownList dr = null;
Label lbattr = new Label();
lbattr.ID = "lbl" + dv["attrId"].ToString();
lbattr.Text = dv["attrName"].ToString() + " : ";
tc.Controls.Add(lbattr);
NoneValueTable = Category_valueManager.Get_Category_value_By_AttrId(Convert.ToInt32(dv["attrId"]));
if (NoneValueTable.Rows.Count != 0)
{
dr = new DropDownList();
dr.DataSource = NoneValueTable.DefaultView;
dr.DataTextField = "title";
dr.DataValueField = "valueId";
dr.AppendDataBoundItems = true;
dr.Items.Add("==请选择==");
dr.ID = "AttrDropDownList" + dv["attrId"].ToString();
dr.DataBind();
tcdt.Controls.Add(dr);
this.LblNone.Text += lbattr.ID + "-" + dr.ID + ",";
}
else
{
TextBox tbox = new TextBox();
tbox.ID = "AttrTextBox" + dv["attrId"].ToString();
tcdt.Controls.Add(tbox);
this.LblNone.Text += lbattr.ID + "-" + tbox.ID + ",";
}
tr.Cells.Add(tc);
tr.Cells.Add(tcdt);
this.tablenone.Rows.Add(tr);
}
}在Button事件中怎么也获取不到控件
protected void Button1_Click(object sender, EventArgs e)
{
string[] str = this.LblNone.Text.Split(',');
string attr = ""; for (int i = 0; i < str.Length; i++)
{
if (str[i].IndexOf("AttrDropDownList") != -1)
{
string [] cstr = str[i].Split('-');
string LabelName = cstr[0];
string DropDownListName = cstr[1];
// Label l1 = (Label)this.UpdatePanel1.ContentTemplateContainer.FindControl(LabelName); //都是null???
Label l = (Label)this.Panel1.FindControl(LabelName); //都是null???
DropDownList d = (DropDownList)this.Panel1.FindControl(DropDownListName);
if(d.SelectedIndex != 0)
{
attr += l.Text + "-" + d.SelectedItem.Text + ",";
}
}
else if (str[i].IndexOf("AttrTextBox") != -1)
{
string[] ctxt = str[i].Split('-');
string LabelName = ctxt[0];
string TxtName = ctxt[1]; Label lb = (Label)this.Panel1.FindControl(LabelName);
TextBox t = (TextBox)this.Panel1.FindControl(TxtName);
if(""!=t.Text)
{
attr += lb.Text + "-" + t.Text + ",";
}
}
}
}
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="True" Width="120px"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" Width="120px" OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
</asp:ListBox>
<asp:ListBox ID="ListBox3" runat="server" AutoPostBack="True" Width="120px" OnSelectedIndexChanged="ListBox3_SelectedIndexChanged"></asp:ListBox>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:Table ID="tableshow" runat="server">
</asp:Table>
<span id="shownone" onclick="shownone();">查看更多...</span>
<asp:Table ID="tablenone" runat="server" CssClass="mycss">
</asp:Table>
</asp:Panel>
<asp:TextBox ID="LblNone" runat="server" Text="" CssClass="mycss"></asp:TextBox>
<input type="hidden" id="lblvalue" runat="server" value="" />
</ContentTemplate>
</asp:UpdatePanel>protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable AttrTable = Category_attributeManager.Get_Category_attribute_By_CategoryId(Convert.ToInt32(this.ListBox3.SelectedItem.Value),1);
DataTable AttrValueTable = null;
foreach (DataRowView dv in AttrTable.DefaultView)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableCell tcdt = new TableCell();
DropDownList drl = null;
Label lbattr = new Label();
lbattr.ID = "lbl" + dv["attrId"].ToString();
lbattr.Text = dv["attrName"].ToString() + " : ";
tc.Controls.Add(lbattr); AttrValueTable = Category_valueManager.Get_Category_value_By_AttrId(Convert.ToInt32(dv["attrId"]));
if (AttrValueTable.Rows.Count != 0)
{
drl = new DropDownList();
drl.DataSource = AttrValueTable.DefaultView;
drl.DataTextField = "title";
drl.DataValueField = "valueId";
drl.AppendDataBoundItems = true;
drl.Items.Add("==请选择==");
drl.ID = "AttrDropDownList" + dv["attrId"].ToString();
drl.DataBind();
tcdt.Controls.Add(drl);
this.LblNone.Text += lbattr.ID + "-" + drl.ID + ",";
}
else
{
TextBox tbox = new TextBox();
tbox.ID = "AttrTextBox" + dv["attrId"].ToString();
tcdt.Controls.Add(tbox);
this.LblNone.Text += lbattr.ID + "-" + tbox.ID + ",";
}
tr.Cells.Add(tc);
tr.Cells.Add(tcdt);
this.tableshow.Rows.Add(tr);
} DataTable NoneTable = Category_attributeManager.Get_Category_attribute_By_CategoryId(Convert.ToInt32(this.ListBox3.SelectedItem.Value), 0);
DataTable NoneValueTable = null;
foreach (DataRowView dv in NoneTable.DefaultView)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableCell tcdt = new TableCell();
DropDownList dr = null;
Label lbattr = new Label();
lbattr.ID = "lbl" + dv["attrId"].ToString();
lbattr.Text = dv["attrName"].ToString() + " : ";
tc.Controls.Add(lbattr);
NoneValueTable = Category_valueManager.Get_Category_value_By_AttrId(Convert.ToInt32(dv["attrId"]));
if (NoneValueTable.Rows.Count != 0)
{
dr = new DropDownList();
dr.DataSource = NoneValueTable.DefaultView;
dr.DataTextField = "title";
dr.DataValueField = "valueId";
dr.AppendDataBoundItems = true;
dr.Items.Add("==请选择==");
dr.ID = "AttrDropDownList" + dv["attrId"].ToString();
dr.DataBind();
tcdt.Controls.Add(dr);
this.LblNone.Text += lbattr.ID + "-" + dr.ID + ",";
}
else
{
TextBox tbox = new TextBox();
tbox.ID = "AttrTextBox" + dv["attrId"].ToString();
tcdt.Controls.Add(tbox);
this.LblNone.Text += lbattr.ID + "-" + tbox.ID + ",";
}
tr.Cells.Add(tc);
tr.Cells.Add(tcdt);
this.tablenone.Rows.Add(tr);
}
}在Button事件中怎么也获取不到控件
protected void Button1_Click(object sender, EventArgs e)
{
string[] str = this.LblNone.Text.Split(',');
string attr = ""; for (int i = 0; i < str.Length; i++)
{
if (str[i].IndexOf("AttrDropDownList") != -1)
{
string [] cstr = str[i].Split('-');
string LabelName = cstr[0];
string DropDownListName = cstr[1];
// Label l1 = (Label)this.UpdatePanel1.ContentTemplateContainer.FindControl(LabelName); //都是null???
Label l = (Label)this.Panel1.FindControl(LabelName); //都是null???
DropDownList d = (DropDownList)this.Panel1.FindControl(DropDownListName);
if(d.SelectedIndex != 0)
{
attr += l.Text + "-" + d.SelectedItem.Text + ",";
}
}
else if (str[i].IndexOf("AttrTextBox") != -1)
{
string[] ctxt = str[i].Split('-');
string LabelName = ctxt[0];
string TxtName = ctxt[1]; Label lb = (Label)this.Panel1.FindControl(LabelName);
TextBox t = (TextBox)this.Panel1.FindControl(TxtName);
if(""!=t.Text)
{
attr += lb.Text + "-" + t.Text + ",";
}
}
}
}
解决方案 »
- POP3,怎麼用C#通過POP3抓取到GMail的郵件信息
- 用Asp.net 2.0+SQL Server 2005开发的B/S程序,如何将SQL Server2005数据库一块发布到互联网上申请的域名空间中?是不是…
- LINQ 好使吗?
- 用javascript怎样实现下面的效果?
- 静态页面生成问题
- 找不到类型或命名空间名称“da”(是否缺少 using 指令或程序集引用?)
- ASP.NET页面使用证书调用调用Web Service的问题
- 在c#中如入用foreach为数组元素赋值
- 如何用正则匹配出下面的数据呢?
- javascript和asp.net如何共用一个Cookie
- 关于AutoCompleteExtender
- vs2005制作好的安装程序,在安装过程中,怎样运行程序.比如创建数据库的脚本.或者还原数据库的脚本程序.
(控件类型) a = (控件类型)xx..FindControl("id")
a.属性
Panel1不是UpdatePanel它没有ContentTemplateContainer这个属性,报错了
我上面的代码就用这个方法的,可是它找不到控件,断点中看到的都是null
没有办法啊,水平不够,呵呵情况是这样的,有很多产品,但是每个产品的属性可能都不一样,
比如我选了(一级)服装-(二级)女装-(三级)女上装,
那么它的属性有 款式`面料`品牌(这三个是显示出来的),隐藏的还有尺码`季节`图案`颜色`领子`......,我只能用两个表格一个显示,一个隐藏了
而且属性有可能有很多种,比如款式有长袖`短袖`无袖等等,这需要Dropdaowlist,如果我换了男装的可能属性又完全不一样了,只能动态实现了
正确
用FindControl是找不到的