Modal popup on dropdown list selection - asp.net

Is it possible to popup a modal(AJAX) on drop downlist selection.
I have a user control which has a ddl and another usercontrol which has that modal popup.On selection of specific item i need to popup modal.
Modal popup is in another usercontrol.

I did an example of how to do it entirely client side using a ClientEventPool - http://www.aaron-powell.com/blog/january-2009/fun-with-a-client-event-pool-and-modal-popups.aspx
For AJAX work avoid postbacks at all costs!

If I am reading you correctly, you will need to use either chained events or event bubbling to force the drop down selection to fire an event. Then your second user control must listen for that event, and fire the event that "shows" the modal popup.
Without testing code, your structure on the primary control might look like this:
public delegate void DDLHandler(int selectedValue);
public event DDLHandler DDLChanged;
public void DDLChanged(int selection)
{
if (DDLChanged != null)
{
DDLChanged(selection);
}
}
Then you drop down control has it's event wired to call the handler
protected void ddlOne_SelectedIndexChanged(object sender, EventArgs e)
{
//fire event handler for fetching value for this selection
DDLChanged(Int32.Parse(ddlMeasurementOptions.SelectedValue));
}

Related

ASP.NET, Automatically make button click from page load

Is there a way to make a button click event run from the page load?
Yes, you can do that.
if your button has runat="server" you can access it from codebehind.
you could insert a click action on the button in the page_load event. but it's better to create a function with all actions to call both from button click and page_load.
<asp:Button id="btn1">Click here</asp:Button>
void Page_Load(object source, EventArgs e)
{
doThis();
}
void btn1_click()
{
doThis();
}
void doThis()
{
//click actions
}
Another way to do it is by javascript. find the button when document is ready and fire the click on the button.
You could wrap the logic behind the button click event in a function and call that function from the page load.

focus on a user control inside an aspx page after clicking a button inside the user control

I have a user control inside an aspx page, after the aspx page loads, when I click some button inside that user control, I want the focus to come back to user control after that button click action is complete and the aspx page loads again.
You need to have an event in your user control that will allow the .aspx page to subscribe to that event so that it can set focus to the element in the user control after the form posts back, like this:
public class UserControlClass
{
// Define event that will be raised by user control to anyone interested in handling the event
public event UC_Button1ClickEventHandler UC_Button1Click;
public delegate void UC_Button1ClickEventHandler();
// Mechanism to allow event to be raised by user control
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (UC_Button1Click != null)
{
UC_Button1Click();
}
}
}
Now in your .aspx page, you need to subscribe to the event in the user control and say what method will actually handle the event, like this:
userControl1.UC_Button1Click += Button1_Click;
Finally, the click event handler needs to exist, like this:
public void Button1_Click(object sender, EventArgs args)
{
// Set focus here to user control element, text box for example
((TextBox)userControl.FindControl("TextBox1")).Focus();
}

dropdown list in user control and auto postback after new item selected

I have a dropdown list in User Control
How Can I get selected value of dropdown list of user control in page when user select another item (auto postback is true)?
I tried to store selected value of ddl in a public member from Selected Index Changed event handler. But this handler executes after page load of container page. I need to load data in page based on selected value in ddl of user control.
Thanks
User Control's code
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
_SelectedPageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
}
int GetSelectedPageSize()
{
return _SelectedPageSize;
}
There are a number of ways to accomplish what you're looking for. The first is simply to re-order your events in the containing page. If you use the PreRender event rather than the PageLoad event, your drop down selection action will be complete and the information will be readily available.
A second method, which probably more extensible, would be to raise a custom event from your usercontrol that your page listens for and handles. Then the action would be taken directly at the point where the information is immediately available. This allows any containing structure (whether it's a page, usercontrol or something similar) to subscribe to the event and handle whatever is needed.
A third method, a little more rigid, would be to have a function in the containing page that is called by the usercontrol once the data is complete. This requires the usercontrol to have knowledge of the specific page type that it will be included in (making it less extensible) so I wouldn't recommend it.
Edit: Here's an idea for implement option #2 with a custom event:
public partial class MyUserControl: UserControl
{
//All of your existing code goes in here somewhere
//Declare an event that describes what happened. This is a delegate
public event EventHandler PageSizeSelected;
//Provide a method that properly raises the event
protected virtual void OnPageSizeSelected(EventArgs e)
{
// Here, you use the "this" so it's your own control. You can also
// customize the EventArgs to pass something you'd like.
if (PageSizeSelected!= null)
PageSizeSelected(this, e);
}
private void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
_SelectedPageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
OnPageSizeSelected(EventArgs.Empty);
}
}
Then in your page code you would listen for the event. Somewhere in the page load you would add:
myUserControlInstance.PageSizeSelected += MyHandinglingMethod;
And then provide the method that handles the event:
protected void MyHandlingMethod(object sender, EventArgs e)
{
// Do what you need to do here
}

Dynamic Event Handler lost after postback

I have a asp.net page with a button, the event the button needs to perform on it's click event needs to vary, dependant on what options are selected further up the page. The button itself is included in the master page, and it's actions are affected by selections made in the child page.
To do this, I have a method in the master page, that is called by the child page, when a users presses a button, that passes the Eventhandler to be used by that button, that is then assigned to masterpage eventhandler for that button:
public void assignEvent( EventHandler saveEvent)
{
this.SaveButtonEvent+= saveEvent
}
Then, when the save button on the master page, it calls that eventhandler
protected void save_Click(object sender, EventArgs e)
{
if (this.SaveButtonEvent != null)
{
this.SaveButtonEvent(this, e);
}
}
The event handler is assigned ok in the first section of code, however because pressing the save button causes a postback, the SaveButtonEvent event handler is set back to being null, and so nothing happens.
How can I preserve the contents of SaveButtonEvent Event Handler during postback, or is there a better way to be doing this?
EDIT:
I can get this to work by saving the EventHandler to the session, but this doesn't seem like a great idea.
When the user presses the button in the child page, store a value (in the viewstate) indicating what event handler should be passed. Then, in the load event of the child page, if it is a postback, always check the stored value and assign the event handler accordingly by calling the master page method.

Row Editing in the grid doesn't work in first click

I have a user control which contains a grid and three buttons for add,edit and delete.
I have placed this user control on an asp.net page.
I have OnClick events for these buttons.
When i click on add and delete buttons it's working fine but when i click on edit button,the onclick event of edit button is fired but the row in the grid doesn't appear in the edit mode, i have to click two times.
I don't know where is the problem.The onclick event handler for edit button is as follows:
protected void btnEditBankAccount_Click(object sender, EventArgs e)
{
grdBankAccounts.EditIndex = grdBankAccounts.SelectedIndex;
grdBankAccounts.RowSelectingEnabled = false;
}
Anyone please help.
my user control has a method which binds the grid to the data source, it's as follows
public void SetSupplierData(SupplierType Supplier)
{
if (Supplier != null)
{
ViewState["SupplierID"] = Supplier.SupplierId;
grdBankAccounts.DataSource = Supplier.BankAccounts;
grdBankAccounts.DataBind();
Session["BankAccounts"] = Supplier.BankAccounts;
}
}
the SetSupplierData method is called from the page where i have my user control.
In order to get this "in-place editing" in grids to work, I typically have to data-bind twice:
once in the OnInit or OnLoad method so that the button click event handlers have the data available to work on
in the OnPreRender method again to show the new values / new state (editing or not)
Marc

Resources