Working with dropdownList and TextBox in Asp.net c# - 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

Related

DataBound ASP:CheckBox events not triggering as expected bacause of DataBinding occuring on Page_Load()

I am stuck into an ASP page.
The page contains an ASP:Container, containing several CheckBoxes.
I need to add an event on these CheckBoxes OnCheckChange so whenever we click one of the CheckBoxes, it unchecks all of the other checkboxes.
I could use a RadioButton, but using CheckBoxes has other advantages for me that are out of this subject so I do not want to use radio buttons.
That would theorically work fine just like this:
<ASP:Repeater id="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
<ItemTemplate>
<asp:CheckBox id="MyCB" runat="server" OnCheckedChanged="MyCB_CheckedChanged" AutoPostBack="true" />
<br/>
</ItemTemplate>
</ASP:Repeater>
and into the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyRepeater.DataSource=DS;
MyRepeater.DataBind();
}
}
protected void MyCB_CheckedChanged(object sender, EventArgs e)
{
UncheckallOtherCheckBoxes();
}
However, to have other regions of this page to work as expected, I do have to bind the DataSource on every page load and not only the first time.
So I need to change Page_Load to
protected void Page_Load(object sender, EventArgs e)
{
// if (!IsPostBack)
{
MyRepeater.DataSource=DS;
MyRepeater.DataBind();
}
}
And this causes everything to fail! :)
Because when I click on a checkbox, it triggers Page_Load before MyCB_CheckedChanged(). Si it DataBinds first and thus, the databinding sends some checkboxes events that are not the ones performed by the user.
Is there a way to solve this????
Thx in advance.
Try binding your datasource later in the page lifecycle, if possible (onPreRender, for example). The control events for your checkboxes are handled after PageLoad and before PreRender and some other events. See https://msdn.microsoft.com/en-us/library/ms178472.aspx for details.

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.

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.

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