Reputation: 12367
I've built a custom web control for picking date. It consists of 3 dropdownlists. In their selectedIndexChanged event i check for the date like this.
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
}
}
}
I actually get the warning label text to change to the error description but as there's no postback in the form the label does not show. here are some consideration: 1. I've put AutoPostBack property of the dropdownlists to false because reloading the page each time after changing the itemindex is frasturating. 2. I tried putting a button on the control and calling its onbuttonclick event but it did not work.
Now,I need to send a postback to the page if my control discovers that the date is not valid.
P.S. When I put a break point at the beginneg of SelectedIndexChganged method and change the index of any of the dropdownlists the debugger does not stop at that breakpoint. I've assigned all the dropdownlist SelectedIndexChanged event to this method.
Upvotes: 1
Views: 3459
Reputation: 1844
u can postback through javascript
use __doPostback('ButtonRenderedId','');
when ur AutoPostback="true" javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)
rendered in html.
means indirectly page postback on every indexchanged through javascript.
if ur autopostback is false u can do postback manully.
to know more click below How to use __doPostBack()
Upvotes: 2