Add item to ListBox from GridView - c#

I have a ListBox present in Home page which is populated from database .
The Home page home.aspx also has an <iframe> which is loaded with another page which displays a GridView.
The GridView has option to Add item to a table m_my_table.
This same table m_my_table is DataSource of list-box present in Home page home.aspx.
How can I add item to ListBox as soon as item is added in GridView?
Please help in doing this with or without page Reload.
How can O call listbox databind( ) function in homepage after gridview databinding function has been called in iframe page.

whenever item is inserted into the gridview , get that selected item and typecast into particular type and then add to your list.Remember , when item is insert into grid , the row is selected already because focus is on that row where you are inserting .

Related

ASP GridView and DropDownList

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.

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.

How can I retain a GridViewRow value?

My GridView is loaded with data from my database in a DataSet, which corresponds to Labels inside the GridView.
When users click the Select button on a GridViewRow, the button fills a TextBox outside the GridView from the selected row's column value.
After editing values in the TextBox outside my GridView, the user can click a button to move the new value from the TextBox back to the GridView (but not to the database).
My problem is: when I select the next row and do the same action, it clear the previous newly-added row data. It doesn't retain the previous row data.
I know it can be done by ViewState or Session, but it didn't work for me.
check for if(! ispostback) before filling data back to your grid. i am assuming that on click of select (Edit) button your page post back and then it fills value in textbox for edit and same time its refilling data in grid control also. so before filling value in grid control make sure its not postback.

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
}

Display selected row of Gridview data to next page in another Gridview?

How can I Display selected row of Gridview data to next page in another Gridview?
For example,
In listing page I want to select particular row and take that data to another page and display over there.
I have two grid view one in 1st page
and next one in 2nd page. Now when I
select a record in 1st page gridview,
than that record will display on 2nd
page gridview on submit button click
from 1st page.
Pass the key of the record via the querystring... and then in rowdatabound event, select the row when the data bound record matches the key in the QS.
For more protection, you can use session to store the variable.
Why not have 2 gridviews, one above the other? The top gridview could be made to only show 1 record which is set to the id of the 2nd gridviews selected item identifier in code. Then in 2nd gridview, have an empty selected item template so that the selected item only shows once for both gridviews. That way the 2nd gridview is free to page through the entire set whilst the first retains the selected item for display above?

Categories