I have two DropDownList which are being populated from a database(SQL) on Page_Load. Now I want to take the Text/Value selected from each DropDownList and insert them into a database. But when I click on the button and the OnClick Event is action the DropDownLists go back to the original state and not the selected values get inserted, instead I get the first Text/Value of the DropDownList all the time. How can I prevent this from happening? Any help will really appreciate it.
I'm using C# for this.
Thanks
In page load, load up the dropdowns like this
protected void Page_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropDowns();
}
}
Basically button click causes postback and if you are populating controls in Load event without check for postback, it will repopulate your controls resetting it's state.
Your DDL is getting rebuilt on every page load. You need to wrap your ddl data source calculation in an
if (!IsPostBack)
or put it in a part of the lifecycle that only loads once, like the OnLoad()
Related
I populate a gridview called grdOrder by using a method which uses a series of other classes and stored procedures (it's so ugly that even I don't understand because it's given by our professor and he's confused us on whole new level)
I then want to delete an item from the database based on value (OrderId) which is in the second rows in the GridView once it is populated.
In the row editor of Gridview I created a button and when I double click the gridview in design view it creates the follow
protected void grdOrder_SelectedIndexChanged(object sender, EventArgs e)
So that is what I think should be firing when I hit the button remove on the Gridview. So I put a break underneath that method and it does not fire when I do that on the form in the browser. So it's not working basically.
If you want to see more of what's going on here is the full codes
http://pastebin.com/iSvyJCPQ - Aspx page
http://pastebin.com/LVRvaCnu - Code Behind (C#)
use the Gridview.RowCommand event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
So I'm currently working with a C# ASP page in which I have a DropDownList and a GridView. I'm initializing both the GridView and the DropDownList (Along with their connected data sources) in the PageLoad event.
I've got the DropDownList set to AutoPostback=true. I'm changing the select statement for the GridView in the DropDownList_SelectedIndexChanged event. The end result is that the page loads again and then the select statement is changed, by which point the GridView has already loaded again. This basically means that it the GridView changes take two page refreshes to update.
Is there a way to avoid having to refresh the page twice? I tried simply updating the DataSource and the GridView in the Page_LoadComplete function instead but by that point it was too late to update the page this time around, meaning it still required another refresh.
So you may try this in your page load....
if(!IsPostBack)
{
// only then bind your grid View...
}
and in your DDL's selectedindex changed event bind your grid to what ever select result set you may want to bind to...
what this will do is, your grid view bind code will get executed only for the first time in your page load event...and on any subsequent postbacks you may cause with your dropdown...you can bind your gridview in the selected index changed event...this will avoid binding your gridview twice...
I have a gridview in an updatepanel where also my dropdownlist is. From the Trigger of the dropdownlist I am refreshing my gridview with the slected value. All that is working fine. The problem is that I am also displaying the gridview row count on the page which is also inside the updatepanel. The update seems to be happening to it, one selection too late.
protected void Drop_Change(object sender, EventArgs e)
{
String Value = AjaxDrop.SelectedValue;
GridView1.SelectParameters["Target"].DefaultValue=Value;
RowCount.InnerText = GridView1.Rows.Count.ToString();
}
I think its happening one selection behind because the parameter updates the gridview rowcount too late for the RowCount value to have it, what is a work around to get the actual value after parameter passed. Only way I can think of is using javascript and I wonder if that would even work. My desired solution would be to keep it all on server side though.
At the time you're calling this, the GridView hasn't done it's databinding, therefore the value is not valid. Move your RowCount.InnerText updating to somewhere that gets executed after the DataBinding event (e.g. Page_OnPreRender), or force the DataBinding to occur before you update the row count.
It's worth noting that GridView.Rows.Count isn't a reliable source of information if you are using GridView paging - as this will be the number of rows on one page, even if there are more rows returned by the query.
I got a question about the command behind a ButtonField (image type) in a GridView.
Got a GridView called gvIngevuld in wich I show rows of data and 1 ButtonField row.
Now I have to put code behind these buttons (Each of them the same) to put some things in PDF format.
The problem is I don't know how to put code behind these buttons ?
The code I have :
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png" CommandName="pdf_click" />
As you can see I used the CommandName="pdf_click" property but when I click one of the buttons there's only a postback but nothing happens.
Anyone who can help me out here ?
in case needed, code behind :
protected void pdf_click(object sender, EventArgs e)
{
lblWelkom.Text = "Succes!";
}
Thanks.
You should use RowCommand event of gridview. Set the commandArgument to your item's unique key. (By default, it passes the row's index)
void gvIngevuld_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="pdf_click")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = gvIngevuld.Rows[index];
//gbIngevuld is your GridView's name
// Now you have access to the gridviewrow.
}
}
Also, If you have set the DataKeyNames of the gridview, you can access it as follows:
int index = Convert.ToInt32(e.CommandArgument);
int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);
Just an addition, I wrote and tried exactly the same however the button still not working...
...it took some time till I realise that I forgot a thing, which was
OnRowCommand="<YourGridViewName>_RowCommand"
inside your GridView element in the **.cs* file
The CommandName property does not define the method to call. For that you need to create a method that is bound to the command event of the control.
I don't know too much about about GridViews i'm afraid but I would think the easiest way to do this would be to select it in design view in Visual Studio, go to properties, click on the events button (looks like a lightning flash) and double click on the RowCommand event propety. This should automatically create a method that is bound to a command button event.
You can then use the GridViewCommandEventArgs parameter to access the CommandName, CommandArgument and CommandSource properties to work out which buttom caused the event to fire.
Let's say I have a DropDownList of product categories and a ListView of products based on the category selection in the DropDownList. When a user visits the page, there is a possibility of conconrency issue as new product belonging to a new category may be added to the inventory as the user is browsing.
When the user selects a different category to view(a SelectedIndexChanged event) and causes a postback, I want the DropDownList to update the list of categories to include the new category being concurrently added and at the same time still able to make a change of selected index. The DropDownList does not seem to support this updating while postback. So how do I handle this concurrent issue?
You need to bind and rebind regardless of checking postback dropdown in Page_Init event. It will not cause any change in selected index of the dropdown and you can continue your work properly, the page init method called before loading view state. means your dropdown index will be selected after this method and you can access it in dropdown_selectedIndexChanged method.
For more information on ASP.Net page life cycle
Edit 1: have a look on sample code
protected void Page_Init(object sender, EventArgs e)
{
// here you bind your dropdown
// don't check IsPostBack
DropdownList1.DataSource = db.GetCategories();
DropdownList1.DataTextField = "TextField";
DropdownList1.DataValueField = "ValueField";
DropdownList1.DataBind();
}
ASP.Net loads controls viewstate after Page_Init and before Page_Load event, so DropDownList1's selectedIndex will not be affected, and you got desired results.
just check if the page-call is a postback with IsPostBack and then call dataBind() on your dropdownlist
Of course you can very well change the data source after the post-back. You can even refresh the data in it by calling DataBind() method on the DropDownListbox.
Another way of doing this if you can't bind in Page_Init for some reason (as in my situation):
var selected = ddList.SelectedValue;
ddList.DataSource = DBHelper.GetCategories();
ddList.DataBind();
ddList.SelectedValue = selected;
You'll still need to handle situation when selected category was deleted.