Dropdown_load Event and Page_load? - c#

I have to bind Drop down list when the Page load.In my previous
Project I have used Page_Load event.Now i find an event which is
Drop down_load event .I used Drop down_load and its Work well .
Is there any difference between these two events,In some cases i
have to bind more than 10 Drop down list.
so which one is best?
eg:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind my Dropdown
}
}
protected void ddlProduct_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind my Dropdown
}
}

It depends on your requirement and when you want to bind your DropDownLists. According to ASP.Net Page Life cycle Page_Load event firs first and then control events, in this case the ddlProduct_Load event.
And, this is also true according the the DropDownList Load event's definition
Fires when the page has been loaded
So, you have to make a decision where you want to bind the control.
Since you have more than 10 DropDownLists I would suggests to use their own load event to bind your control rather than making Page_Load looks too busy.

Related

Telerik combobox - viewstate without autopostback

I turned off AutoPostback on my control because I need to validate something with javascript. And if everything is ok I'm doing postback clikcing on hidden button. The problem is that combobox looses selected value on page reloading.ViewStateMode set to Enabled. I'm populating combobox in page_load event:
protected void Page_Load(object sender, EventArgs e)
{
(!IsPostback)
{
InitializeItems(); // Helper method that binds data
}
}
Before going into the internals of Telerik, you can try to resolve the original problem. You said you want to perform validation before the postback takes place.
All you have to do is register the script you want to be ran on form submit:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.Page.ClientScript.RegisterOnSubmitStatement(typeof(YourClass), this.UniqueID, "your validation script here.");
}
Then in order to cancel the postback, your validation script needs to return false.

Working with dropdownList and TextBox in Asp.net c#

How do you add values to a database using a dropdownList or a TextBox without using a button in asp.net C# application?
You need to set AutoPostBack property to true and save it on index changed event.
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
>
<asp:ListItem>option1</asp:ListItem>
<asp:ListItem>option2</asp:ListItem>
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//save data here
}
For drop down list, its simple. Set the "AutoPostBack" property for it as "true". And handle the "SelectedIndexChanged" event like shown below.
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//save data here
}
But for textbox you have a server side event "TextChanged", which you can use the same way by setting "AutoPostBack" to "true" for the textbox as well.
protected void TextBox1_TextChanged(object sender, System.EventArgs e)
{
//do something here
}
The drawback with this textbox event is it gets fired with every keystroke.
So its not good for handling the database operation with every keystroke.
jQuery & an invisible button can help.
Have an asp Button, set its visibility to false. Handle its click event on the server side like shown below.
protected void Button1_Click(object sender, System.EventArgs e)
{
////save data here
}
For your text box wire up a jQuery .blur() event like shown below & fire the click event of the hidden button from it.
$("#TextBox1").blur(function() {
$("#Button1").click();
});
This would fire the button click event on server side, when you stop editing textbox value. Do your database operation from the button click event on server side.
I think you could implement it through a page load event

Issue Related to GridView in ASP.net

I have gridview binded from database..
I have following code:
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
Now, I have checkBox and drop-down inside the gridView, When user selects some rows from checkboxes and clicks on Update Button, Page_Load event fires and calls BindGrid(); method and selected rows should get hidden.
How can I retain checkbox values after the page load event.
I don't want to use IsPostBack property in Page load because I have used Paging.
How can I solve my problem?
You should only DataBind the GridView if(!Page.IsPostback). Otherwise no events are triggered and ViewState values(like SelectedIndex etc.) are overwritten from the DataSource values.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback%28v=VS.100%29.aspx
if(!IsPostBack)
{
BindGrid();
}
You also should call BindGrid from following event-handlers:
PageIndexChanging
SelectedIndexChanged
Sorting
Make use of ISpostback..
if(!IsPostBack)
{
BindGrid();
}
call bindgrid from the pagaing event
function of paging event
{
BindGrid();
}

Problems with IsPostBack

I have an .aspx page which sends a letter to a customer if a button on that page is clicked. Onclick the page calls itself, so the mail send class is in the same file. However I do not want the mail sent when the page is simply loaded. I want it send the letter when the button is clicked, so, I'm trying with the following code:
void page_Load(Object sender, System.EventArgs e)
{
if (IsPostBack)
{
SendMail();
}
}
But it doesn't work. What am I doing wrong?
Why not use the Button's Click Event then?
What you are talking about is you want to send an email only when a specific button is clicked. Then why not register to it's click event instead of bloating your page_load with extra code?
Button's click event is raised only when that button's click causes a postback. So, that's your best option.
Make an event handler for the button's click event (Just double click the button in Visual Studio's Designer).
Using Page_Load will result in emails being sent out when the user posts back in any circumstance, not just your button click.
Looks like the page does not find the correct event handler for the Page_Load, check the case and correct it to Page_Load
See if this works (replace your page_Load method with the following code):
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (IsPostBack)
{
SendMail();
}
}
You are currently relying on AutoEventWireup functionality to hook up your page events. This is slow and problematic and may be the cause of your issue. The method I gave you overrides Page.OnLoad and should correct the problem as well.
The Page_Load event is raised every time the page is posted, well by means of postback or callback, if you want to use server side events you should call you SendMail method in the button's click event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !IsCallback)
{
/*occurs the first time the page is loaded*/
}
if (IsPostBack)
{
/*occurs every time a postback is raised (e.g. by form submission) */
}
if (IsCallback)
{
/*occurs every time a callback is raised, e.g. by generating callbacks by means of AJAX/
}
}
protected void SendMail_Click(object sender, EventArgs e) { SendMail(); }
Read more at MSDN: ASP.NET Page Life Cycle.

DropDownList.SelectedValue changes (as a child control in a FormView) aren't sticking

Okay, I have a FormView with a couple of child controls in an InsertItemTemplate. One of them is a DropDownList, called DdlAssigned. I reference it in the Page's OnLoad method like so:
protected void Page_Load(object sender, EventArgs e)
{
((DropDownList)FrmAdd.FindControl("DdlAssigned")).SelectedValue =
((Guid)Membership.GetUser().ProviderUserKey).ToString();
}
Basically I'm just setting the default value of the DropDownList to the user currently logged in.
Anyway, when the page finishes loading the SelectedValue change isn't reflected on the page. I stepped through OnLoad and I can see the change reflected in my Watch list, but when all is said and done nothing's different on the page.
I figured it out. I'm still missing exactly why it doesn't work just on FormLoad, but performing the change in the FormView's DataBound event does the trick.
protected void FrmAdd_DataBound(object sender, EventArgs e)
{
// This is the same code as before, but done in the FormView's DataBound event.
((DropDownList)FrmAdd.Row.FindControl("DdlAssigned")).SelectedValue =
((Guid)Membership.GetUser().ProviderUserKey).ToString();
}
So, I guess the general rule of thumb is that if you are having problems making changes to controls when working with databinding, try to make them immediately after it has been bound.
I had a problem with dropdownlists and making the first value say something like, "Please select a value..." but without making it an actual selectable item, nor show up on the dropdownlist. I was binding the ddl in the page_load and I have to make sure that I set the text of the dropdownlist, AFTER it's been bound with data. You've accomplished the same thing by adding it to your databound section.

Categories