2012-04-09 91 views
0

我为拾取日期构建了自定义Web控件。它由3个下拉列表组成。在他们的selectedIndexChanged事件中,我检查这样的日期。以编程方式发送回发

protected void SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList ddList = sender as DropDownList; 
    if (ddlDays.SelectedIndex > 0 && ddlMonths.SelectedIndex > 0 && ddlYears.SelectedIndex > 0) 
    { 
     int day = ddlDays.SelectedIndex; 
     int month = ddlMonths.SelectedIndex; 
     int year = int.Parse(ddlMonths.SelectedValue); 
     try 
     { 
      DateTime date = new DateTime(year, month, day); 
     } 
     catch 
     { 

      if (OnDateNotValid != null) 
       OnDateNotValid("Entered date is not valid"); 
      //Here I want to invoke PosBack on the page where I use this control 
     } 
    } 
} 

我居然得到警告标签文本更改为错误的描述,但因为没有回发窗体标签不显示。这里有一些考虑因素: 1.我已经将DropDownList的AutoPostBack属性设置为false,因为每次在更改itemindex之后重新加载页面都是frasturating。 2.我试着在控件上放一个按钮,并调用它的onbuttonclick事件,但它不起作用。

现在,如果我的控件发现日期无效,我需要向页面发送回传。

P.S.当我在SelectedIndexChganged方法的初始阶段放置一个断点并更改任何下拉列表的索引时,调试器不会在该断点处停止。我已将所有下拉列表SelectedIndexChanged事件分配给此方法。

+0

使用更新面板,这和把拉布勒里面的UpdatePanel。 – 2012-04-09 09:35:23

+0

http://stackoverflow.com/questions/3591634/how-to-use-dopostback – 2012-04-09 09:52:43

+0

更新面板仍然使用了吗?这些年前我停止使用,因为它们臃肿。已经看到许多利用这些缓慢的项目,对我而言,似乎是开发Ajax风格应用程序的“简单”方式。 – mickyjtwin 2012-04-09 09:59:13

回答

1

u能回传通过JavaScript

使用__doPostback('ButtonRenderedId','');当UR的AutoPostBack = “真” javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0) 以HTML格式呈现

表示间接页面回传通过javascript更改的每个索引。

如果你的autopostback是假的你可以做回发手令。

了解更多请单击下面How to use __doPostBack()