用户自定义控件问题.一个页面加载Page_Load时候,控件控件无法初始化??里面有Panel隐藏与显示,无法联动?

meadking 2009-11-04 11:09:54
我有一个City_Select_All_In_One.ascx用户控件
是选择国家,地区,省份,市,县的.
控件有个方法
this.City_Select_All_In_One1.setLocationCode(国家,地区,省份,市,县);
参数默认为空值

当有国家代码的时候,(地区,省份,市,县)三个下拉框隐藏...
当有国家,地区,省份的时候,显示这三个DropDownList.其它两个市,县Panel是隐藏的!

这个控件在测试页面用按钮点击事件,调用setLocationCode方法,控件会显示出我们需要的面板
(国家,地区,省份,市,县)Panel都会自动展开.....一切正常...
protected void btn_submit_Click(object sender, EventArgs e)
this.City_Select_All_In_One1.setLocationCode(国家,地区,省份,市,县);

但是我在测试页面的页面加载初始化时候调用,控件Panel面板就没有反应....不会响应setLocationCode方法....
protected void Page_Load(object sender, EventArgs e)中
this.City_Select_All_In_One1.setLocationCode(国家,地区,省份,市,县);

没有一点反应..

这个是不是和用户控件的生命周期有关系?
Page_Load中控件没有初始化成功,无法调用控件事件setLocationCode方法???

实在没有方法.no clue...
解决后另外开贴加分....200分
...全文
768 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaomifeng007 2010-11-01
  • 打赏
  • 举报
回复
.......
meadking 2009-11-05
  • 打赏
  • 举报
回复
现在好了,,,,,

控件属性
 public string TerminusCode
{
get { return _terminus_code; }
set { this._terminus_code = value; }
}


貌似无法存值...会丢失状态...(调用属性之前,已经set这个属性的值了...)
只有用方法才能返回真正的值
public string getTerminusCode()
{
return this.DropDownList.SelectedValue;
}
meadking 2009-11-04
  • 打赏
  • 举报
回复
#region SaveStateComplete 第九步
protected override void OnSaveStateComplete(EventArgs e)


我用这个方法,却无法保存控件的状态???
meadking 2009-11-04
  • 打赏
  • 举报
回复
谢谢哦...有qq吗?
wuyq11 2009-11-04
  • 打赏
  • 举报
回复
执行顺序
控件的Init事件
控件所在页面的Init事件
控件所在页面的Load事件
控件的Load事件
控件所在页面的PreRender事件
控件的PreRender事件
meadking 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
在调用控件的页面调用setLocationCode(string code)方法
设置下拉框值
[/Quote]

你可能没有好好看问题,控件功能已经实现了!
就是调用方法的时候响应问题而已...
meadking 2009-11-04
  • 打赏
  • 举报
回复
http://www.cnblogs.com/oletan/archive/2008/11/04/1326587.html

终于解决了问题,爷爷的...生命周期问题...

#region 调用方法事件...初始化控件...
protected override void OnSaveStateComplete(EventArgs e)
{
base.OnSaveStateComplete(e);
this.Web_Site_Select.setSiteCode("Wa", "Wa-80", "Wa-80803");
this.City_Select_All_In_One1.setLocationCode("Ycn330723");
}
#endregion
送分了..................
wuyq11 2009-11-04
  • 打赏
  • 举报
回复
在调用控件的页面调用setLocationCode(string code)方法
设置下拉框值
meadking 2009-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
    </ContentTemplate>
        </asp:UpdatePanel>
</div>
[/Quote]

我的问题是,为何在测试页面的页面加载初始化时候调用,控件Panel面板就没有反应....不会响应setLocationCode方法....
页面的 Page_Load

setLocationCode方法用[color=#0000FF]按钮点击事件调用是好的啊[/color]
wuyq11 2009-11-04
  • 打赏
  • 举报
回复
用户控件是否动态加载
if(!IsPostback)
{
//调用this.City_Select_All_In_One1.setLocationCode方法,
参数默认为空值,应该先设置DropDownList国家,地区,省份,再显示Panel
使用ajax通过DropDownList级联选择很简单
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</div>
meadking 2009-11-04
  • 打赏
  • 举报
回复
部分的控件代码...



public partial class UserControls_City_Select_All_In_One : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDDL(DDLzhou_code, new NewMd.Core.WorldCityList().zhouByDisp(), "citypostcode", "cityname");
this.DDLzhou_code.SelectedIndex = 5;//默认为亚洲
this.SetDDlDefaultText(DDLzhou_code, "亚洲");

//验证控件
//this.validate_zhou.Enabled = true;
//this.validate_guojia.Enabled = false;
//this.validate_diqu.Enabled = false;
//this.validate_sheng.Enabled = false;
//this.validate_dijishi.Enabled = false;
//this.validate_xianjishi.Enabled = false;


//选择国家面板=中国
this.DDLzhou_code_SelectedIndexChanged(null, null);
this.Chkzhou_code.Checked = true;
this.Chkzhou_code_CheckedChanged(null, null);
//显示省份面板=未选择
this.DDLguojia_code_SelectedIndexChanged(null, null);
//this.Chkguojia_code.Checked = true;
//this.Chkguojia_code_CheckedChanged(null, null);

}
}
private void BindDDL(DropDownList ddl, DataSet dt, string strValueField, string strTextField)
{
ddl.DataSource = dt;
ddl.DataValueField = strValueField;
ddl.DataTextField = strTextField;
ddl.DataBind();

}


//选择国家代码
protected void DDLguojia_code_SelectedIndexChanged(object sender, EventArgs e)
{
this.DDLdiqu_code.Items.Clear();
this.DDLdiqu_code.Items.Add(new ListItem("请选择地区", "0"));
BindDDL(DDLdiqu_code, new NewMd.Core.WorldCityList().diqu(DDLguojia_code.SelectedValue), "citypostcode", "cityname");
DDLdiqu_code_SelectedIndexChanged(null, null);
}

//选择地区代码.联动
protected void DDLdiqu_code_SelectedIndexChanged(object sender, EventArgs e)
{
this.DDLsheng_code.Items.Clear();
this.DDLsheng_code.Items.Add(new ListItem("请选择省份", "0"));
BindDDL(DDLsheng_code, new NewMd.Core.WorldCityList().sheng(DDLdiqu_code.SelectedValue), "citypostcode", "cityname");
DDLsheng_code_SelectedIndexChanged(null, null);
}
//选择省份代码.联动
protected void DDLsheng_code_SelectedIndexChanged(object sender, EventArgs e)
{
this.DDLdijishi_code.Items.Clear();
this.DDLdijishi_code.Items.Add(new ListItem("请选择地级市", "0"));
BindDDL(DDLdijishi_code, new NewMd.Core.WorldCityList().dijishi(DDLsheng_code.SelectedValue), "citypostcode", "cityname");
DDLdijishi_code_SelectedIndexChanged(null, null);
}

//选择地级市.联动
protected void DDLdijishi_code_SelectedIndexChanged(object sender, EventArgs e)
{
this.DDLxianjishi_code.Items.Clear();
this.DDLxianjishi_code.Items.Add(new ListItem("请选择县级市", "0"));
BindDDL(DDLxianjishi_code, new NewMd.Core.WorldCityList().xianjishi(DDLdijishi_code.SelectedValue), "citypostcode", "cityname");

}

private DropDownList GetSelectCity()
{
DropDownList Retval = DDLzhou_code;
if (Panel6.Visible)
{
return DDLxianjishi_code;

}

if (Panel5.Visible)
{
return DDLdijishi_code;

}
if (Panel4.Visible)
{
return DDLsheng_code;

}
if (Panel3.Visible)
{
return DDLdiqu_code;

}
if (Panel2.Visible)
{
return DDLguojia_code;

}
if (Panel1.Visible)
{
return DDLzhou_code;

}
return Retval;
}
#endregion

//获取城市代码
public NewMd.Model.City_CitySelect_all GetCity()
{
return 选定的城市代码;
}


/// <summary>
/// 提供数据回显的代码
/// </summary>
/// <param name="code">输入城市代码</param>
/// <returns>返回设定是否成功,UI是否回显</returns>
public bool setLocationCode(string code)
{
//安全检查位数
if (code.Length != 9)
{
return false;
}


if (code.Substring(1, 8) != "00000000")
{
this.Chkzhou_code.Checked = true;
this.Chkzhou_code_CheckedChanged(null, null);
}
if (code.Substring(3, 6) != "000000")
{
this.Chkguojia_code.Checked = true;
this.Chkguojia_code_CheckedChanged(null, null);
}
if (code.Substring(4, 5) != "00000")
{
this.Chkdiqu_code.Checked = true;
this.Chkdiqu_code_CheckedChanged(null, null);
}
if (code.Substring(5, 4) != "0000")
{
this.Chksheng_code.Checked = true;
this.Chksheng_code_CheckedChanged(null, null);
}
if (code.Substring(7, 2) != "00")
{
this.Chkdijishi_code.Checked = true;
this.Chkdijishi_code_CheckedChanged(null, null);
}


//绑定洲的代码...1
BindDDL(DDLzhou_code, new NewMd.Core.WorldCityList().zhouByDisp(), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLzhou_code, code.Substring(0, 1) + "00000000");
DDLzhou_code_SelectedIndexChanged(null, null);

//国家2
BindDDL(DDLguojia_code, new NewMd.Core.WorldCityList().guojiaBydisp(code), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLguojia_code, code.Substring(0, 3) + "000000");
DDLguojia_code_SelectedIndexChanged(null, null);

//地区4
BindDDL(DDLdiqu_code, new NewMd.Core.WorldCityList().diqu(code), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLdiqu_code, code.Substring(0, 4) + "00000");
DDLdiqu_code_SelectedIndexChanged(null, null);

//省份5
BindDDL(DDLsheng_code, new NewMd.Core.WorldCityList().sheng(DDLdiqu_code.SelectedValue), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLsheng_code, code.Substring(0, 5) + "0000");
DDLsheng_code_SelectedIndexChanged(null, null);

//地级市7
BindDDL(DDLdijishi_code, new NewMd.Core.WorldCityList().dijishi(DDLsheng_code.SelectedValue), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLdijishi_code, code.Substring(0, 7) + "00");
DDLdijishi_code_SelectedIndexChanged(null, null);

//县级市
BindDDL(DDLxianjishi_code, new NewMd.Core.WorldCityList().xianjishi(DDLdijishi_code.SelectedValue), "citypostcode", "cityname");
this.setDDLDefaultValue(this.DDLxianjishi_code, code);


return true;
}
}

62,046

社区成员

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

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

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

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