ASP GridView and DropDownList - c#

I have an asp page with a GridView control. One of the columns in the grid view is a dropdownlist. the drop down list are filled using the RowDataBound event on the page load event. However, one of the options in the list, when selected, will popup a dialog that will allow the user to add a new item to the list, and that newly added item becomes the selected item for that drop down. This part I have working fine, but I want to be able to add the new item to the drop down list for every row in the grid. How do I, in a way, re bind the drop down list in all rows after the page has loaded without having to loop through each field?

If all the DDL's have a shared DataSource then it should just be a matter of forcing a postback after adding the new listitem. But how are you adding the new listitem?
If your popup is simply adding a new html <option> tag to an html <select> tag, this is not the same thing as updating the DDL DataSource.
For all the DDL's to show the new listitem your popup would have to perform a postback that updates the DDL datasource(s) and all the DDL controls in the GridView would need to be rebound.
This would happen automatically on a postback assuming that the DDL controls are connected to their datasource via the DataSourceID property. If you are making use of the DataSource property then you will have to call DataBind() explicitly for each DDL control, which I think you said you are doing in the GridView RowDataBound event.
Alternatively you could loop through all your ddl controls clientside and add the same item to each control, but you would lose all those changes the next time the page refreshed and I don't think that's what you want.

Related

How to get data from database to CheckboxList control inside a form view control?

I am working on asp.net user control. I have used a gridview and a formview control.
on selecting a row in gridview it will hide the panel containing the grid and will display the panel containing the form view which is using the Grid's selected value as its key value and form loads in edit mode. for some extra use i had to place a checkbox list control in my form view control. and used SQL datasource to fetch data from databese to chkbox list. and used the same data key as formview control. Now my form view control works properly but my Checkbox list is not working properly as it cant get the selected value from grid view.
Thanks in advance for help.
You should bind your CheckBoxList on the DataBound event of your FormView.
Since you would need the same data key, you could use the DataKey property of the FormView.
If any additional data fields are required, you always have the DataItem property.
Done in this order, your CheckBoxList should work as expected.
I used a session variable and it worked for me.
i used a session variable to store the selected value of grid in its selected index changed event and then used it as key value in checkbox list.

Timing a gridView Update in C#

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...

ASP.NET GridView focus selected row

I am fairly new to ASP.NET. I have a GridView control which has many rows. Based on selection of each row I need to enable/disable few list and text controls on the collapsible panel in a ASCX.
The issue I am facing is whenever I select any row the GridView sets the focus on the first row all the time due to postback.
How do I set the focus on the selected row?
Can I use GetPostBackClientHyperlink in the RowDataBound somehow to register a client-side script?
Reply with some code is much appreciated.
Add a handler to the SelectedIndexChanged event of your GridView and insert the following code into this handler:
GridView1.SelectedRow.Focus(); // where GridView1 is the name of your GridView
You can also use jQuery. I added a <div> with an ID that is only shown when a row is using the edit template.
$('#hiddenDIV').focus();

asp.net using c# DropDownList item not selected

ASP.NET using C#:
I've got a DropdownList and a Button.
In Page_Load I fill the DropdownList manually via SQL-Query with some items from the database.
After click on the button I want to Alert the selected item of the list but every time the first item alerts instead of the selected item.
So simple, but what am I doing wrong?
Without your code it's hard to say, but I'm guessing on your page load, your reloading your dropdownlist, which is removing the selected item, and giving you the first item every time.
If this is the case, check for the request being a postback, and don't repopulate the drop down list.
when click on button happen postback and run page_load and fill dropdown list agin then show first item that selected.
if (!IsPostBack)
{
//fill the dropdownlist
}

Dynamic gridview based on dropdown

I've got a bit of an chicken and an egg.
I need to bind a gridview to a set of database values. The gridview is dynamic and the columns, cells are created at run time.
As such, I need to re-bind the grid on every postback in the pre-init, init events after very post back.
However, the data used to populate the grid uses a value from a dropdown box on the same page. As such, the value of dropdown is not accessible through viewstate until after the init event (i.e. the selected index is always the first item in the list until after on init).
How can I get access to the value of the drop down in time to rebind the grid before the pageload event?
If the Gridview always has the same number of columns, you could have the dynamic query generate column names as aliases, and then just rebind the Gridview on dropdown change?
you can use and HtmlHidden control for store the value you need. It doesn't depend by the viewstate and it's available into the page init

Categories