keep radio button selected across postback - c#

I have a gridview with radio buttons to select a single row.
The gridview is populated from a SQL DB according to a value selected from a drop down list.
What I want to do is keep the selected radio button when the user moves to a different category, so if he wants to move back via the DDL the value he selected previously is still selected.
What is the best way to do this?
The application will have users and I have read sessions could help me with this but I have no idea how it works!
Thanks in advance

I'm not sure really why you'd want to do it this way but you'd then save it in session this way:
String selectedValue = rblYourRadioButtons.SelectedValue;
Session["SelectedRadioButtonValue"] = selectedValue;
Then, on postback or whenever, on the Item Databound of the gridview you check if the current row Id matches the value stored in the session, if it does then select that value in the radiobutton list.
Personally I'd store the value in the DB rather than session.

Related

Set caption for dropdown list

I have a dropdownlist on a page for which I want to set a text so that whether or not user makes any selection, it should always be displayed to the user and not the selected value.
For example:
Here Select Language should always be visible to user even if I select any value from the list i.e. Option 1 or Option 2.
I guess it is more of a CSS task which I am not very much comfortable with.
If you are talking about a simple asp:DropDownList, you just need to insert an item in it, with an index value of 0 (to deal with server-side checks).
myDropDownList.Items.Insert(0, "Select");
dropdownlist1.Text="SomeText";
Dropdownlist need to have the data-source.
Here Select Language should always be visible to user even if I select
any value from the list i.e. Option 1 or Option 2.
Solution:
if your dropdownlist isn't autopost back enabled then
Maintain a hidden field where on change event of the dropdownlist place the selected value or selected index or selected text there in the hidden field to remember the selection.
lets says the id of your dropdownlist is sel and hidden field is hdSel, the jquery code for that look likes below
$('#sel').change(function(){
var val = $("#sel option:selected").text();
$('#hdSel').val(val);
$("#sel").prop('selectedIndex', 0);
}
);
example on Jsfiddle for your understanding
If your dropdownlist is autopostback enable then you can assign the selected value or selected text or selected index of the dropdownlist to the hidden field and reselect the first item again
Hope this help

Gridview Automatically Selects first Row after a sort

I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.
Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.

Change value of same dropdown in other rows of radgrid when one dropdown value is changed

I have a drop down with yes/no as data inside a rad grid. When the value of dropdown in row is changed, values of dropdown in other rows excluding this should be set with second item. This must be done with javascript.
You could open this link.
In addition, I would suggest you changing this line:
GridEditFormItem item = DropDownList1.NamingContainer as GridEditFormItem;
to
GridEditableItem item = DropDownList1.NamingContainer as GridEditableItem;
in order to support both edit and insert scenario.
it would require 2 steps
right click on the dropdown a pop up will appear then check the postback option
after that simply double click on the dropdownlist as a result function will get created in your filename.cs file now put the code for adding the option for the other dropdown list there and u r done

How to disable dropdown list value in the current form that was selected in the previous page

I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");

Stuck with datagridview and combo box column

I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

Categories