Telerik combobox - viewstate without autopostback - c#

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.

Related

Set the 'onClick' to call a client method when dynamically adding control in Code Behind

I am dynamically adding a RadioButtonList in the Code Behind. I want it so that the 'OnClick' does not call JavaScript, but instead it calls a method in my code behind.
Is this possible?
In addition, is there a way to set it up so that this say control has runat="server"?
You can use the Button Click Event
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click(v=vs.110).aspx
Yes, it is possible. Also, there is no need to have runat="server" since your are creating the control in code.
You need to set your RadioButtonList object's OnSelectedIndexChanged event. As #Robert mentioned, if you are creating controls dynamically you need to wrap them in the Page_Init().
protected void Page_Init(Object sender, EventArgs e) {
RadioButtonList radiobuttonlist = new RadioButtonList();
radiobuttonlist.SelectedIndexChanged += radiobuttonList_CheckedChanged;
//Set the AutoPostBack to true proptery to that the user action
// will immediately post-back to the server
radiobuttonlist.AutoPostBack = true;
}
private void radiobuttonList_CheckedChanged(object sender, EventArgs e) {
//Code you want to execut when a user selects a different item
}
Reference: https://msdn.microsoft.com/en-us/library/System.Web.UI.WebControls.ListControl.SelectedIndexChanged(v=VS.110).aspx
Be sure to add the control in the Page_Init() portion of the code, not in Page_Load() or later. Set up the event handler += line inside the Page_Init() setup. The control should run server side if you do this. I'm not sure if runat="server" will be explicitly set but the control will behave that way.

Dropdown_load Event and Page_load?

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.

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

Updating asp.net textbox on postback from database displays old value

Asp Textbox inside update panel displays value from database in page load. Button inside update panel triggers postback. Database procedure changes value to be displayed. Textbox text is updated from database in Page_Load, if (!Page.IsPostBack). It is confirmed that at the end of Page_Load the textbox has the updated value. Displayed value on screen does not change to updated value.
Based on other posts, I have tried moving the update of the textbox text to the OnPreRender event with the same result.
My only work-around so far is to re-create the control with a new ID on each postback so it will not be repopulated from posted data (using timestamp appended to ID) and finding the control by the base name using Regex. This way I can display the right value and read it on next postback, but it seems to be a cumbersome workaround.
What is the proper .NET way to update a textbox during postback and have the value "stick"?
If you use your texbox just for displaying purpose you shouldn't use if (!Page.IsPostBack)
If you need to use your textbox value. Then
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
TextBox1.Text = %YOUR_INITIAL_DB_VALUE%;
}
protected void Button1_Click(object sender, EventArgs e)
{
string userInput = TextBox1.Text;
TextBox1.Text = %YOUR_NEW_DB_VALUE%;
}

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.

Categories