I am using a DropDown menu and an UpdatePanel to filter out a DataGrid. The DataGrid has buttons which redirect to a different page. When I hit the back button or a link on top of the other page, it redirects me to the page with the DropDown as it should...but it gets rid of the DataGrid data and I have to make a selection from the DropDown again. Is there a way to make sure that the DropDown selection is remembered when both the link is pressed and the back button selected? Thanks for your help!
The easiest thing to do in this case is to save the dropdown selection in Session collection
and on page load, check to see if there is a saved selection and use it to reapply the selection.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SavedSelection"] = DropDownList1.SelectedIndex;
}
protected void Page_Load(object sender, EventArgs e)
{
if(Session["SavedSelection"] != null)
{
int selectedIndex = (int) Session["SavedSelection"];
DropDownList1.SelectedIndex = selectedIndex;
}
}
Related
I want to find a control in a selected row of a listview, can I do something like this:
lv_ListviewTest.Items(lv_ListviewTest.SelectedIndex).FindControl("ControlName")
What I had is:
if (e.CommandName == "Select")
{
var ctrl = (HtmlContainerControl)e.Item.FindControl("area");
ctrl.Attributes["style"] = "background-color:LightSkyBlue; color:Black; padding:0px;";
}
I can do it on Itemcommand, but I don't know how to trigger itemcommand when page re-load.
What I want to do is: when a button(not the button on the listview) clicked, the page reload and the focus will stop on the button of the listview.
Thank you very much.
you can find them below URL
Find and access controls in EditItemTemplate of ASP.Net ListView
How to use ListView control in ASP.NET
In Button click Event of Outer Button add below code
protected void MyButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in MyListView.Items)
{
var ctrl = (HtmlContainerControl)item.FindControl("area");
ctrl.Attributes["style"] = "background-color:LightSkyBlue; color:Black; padding:0px;";
}
}
Oh, I found that I can use SelectedIndex and Session variable to store it:
When item selected:
protected void lv_ListviewTest_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SelectedIndex"] = lv_ListviewTest.SelectedIndex;
}
When the other button clicked:
protected void cmd_OtherButton_Click(object sender, EventArgs e)
{
...............
Button focusbutton = (Button)lv_ListviewTest.Items[Convert.ToInt16(Session["SelectedIndex"])].FindControl("MyControlLabel");
focusbutton.Focus();
}
So I can let my listview show what I selected before like ensurevisible do.
I have created a dropdownlist in my webpage which is outside the gridview and I have added automatic refresh. My problem is that I`m unable to retain the selected value in dropdownlist after the refresh. It goes to the default settings in the dropdown. Please help.
Thanks a lot for replying..
part of my code goes this way....
page_load(...)
{
Refresh
if(!IsPostBack)
{
//calling my function which includes databind..
myfunction();
}
}
i tried the same code as you people suggested but its not working..
even now after refresh, the default values appear in dropdownlist
You probably need to implement something like this in your page_load handler:
if (IsPostback) return;
//here populate the dropdown
Let me guess your Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataBindGridView(); // loads the datasource of the grid and calls gridView1.DataBind();
DataBindDropDown(); // loads the datasource of the dropdown and calls dropDown1.DataBind();
}
Do not reload all on every postback, just if !(IsPostBack):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBindGridView();
DataBindDropDown();
}
}
If you need to refresh your GridView don't use Page_Load but the appropriate event handler. If you use an ASP.NET Timer to reload your page periodically to refresh the grid, use it's Tick event.
protected void GridRefreshTimer_Tick(object sender, EventArgs e)
{
DataBindGridView();
}
Here is the complete code. I want to display a radiogroup when I select 1 in the dropdown list box. I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'.
namespace HostelRoomManagement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "1")
{
RadioButton rb1 = new RadioButton();
rb1.ID = "RadioButton1";
rb1.Text = "C block";
rb1.GroupName = "BlockGroup";
RadioButton rb2 = new RadioButton();
rb2.ID = "RadioButton2";
rb2.Text = "C block";
rb2.GroupName = "BlockGroup";
Page.Controls.Add(rb1);
Page.Controls.Add(rb2);
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
}
}
}
I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'
You can add the radio buttons you want the user to see based on his choice to the page and set the Visibility of them to false. Then, once the user choose a value change the visibility of the radio buttons you want to true. It might be easier.
I suspect your problem is that you are referencing to SelectedValue whereas you want to be refering to selectedindex.
Hope this helps.
You have created two radio button but where have you added them on the page?
Start by creating a place holder for you radio-button lists and add these controls over there.
The dynamically created control will be lost on the post back. This means you will have to manage you dynamically created controls.
Here is a good [example]: 1 ASP.NET dynamically created controls and Postback.
I am working on an aspx page in VS 2005. I have code like this,
int RepID = 0;
protected void Page_Load(object sender, Eventargs e)
{
if(!Page.Ispostback)
{
get value from database and display it in textbox;
}
else
{
}
}
protected void Save_OnClick(object sender, Eventargs e)
{
Update Database with modified textbox Text ;
Response.Redirect(//To the same page);
}
After the Response.Redirect, i was looking for the page to refresh and get the modified value from database. Instead, it uses the else loop in Page_load and displays the old value because it doesn't go into the if loop as it is the Postback. How can i display from database after the response.redirect is used. I know i am missing a logic but i am not sure what? Thanks alott guys..
Delete
Response.Redirect(//to same page);
Your Save button click should post back to the same page it is on.
hi i need to modify my code a little bit. i have a page with a radio button list and a textarea. the textarea is populated when a users makes a radio button selection.
also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. (i.e. ?selected=0)
http://test.com/frm_Articles.aspx?selected=0
http://test.com/frm_Articles.aspx?selected=1
http://test.com/frm_Articles.aspx?selected=2
that way they can copy the url and reference it in other websites as a link. or place it in their favorites.
the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. no radio buttons or content appear on the page.
must be the postback logic i think???
what's working:
when i launch the website the radio buttons appear and index 0 is set
when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. http://test.com/test.aspx?selected=2)
if i cut and paste pointer urls within the same browser then correct data is rendered
what doesn't work (everything that deal with an false PostBack):
1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["#URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
In your code at Page_Load event before this line
RadioButtonList1.SelectedIndex = selected;
you should bind RadioButtonList1. after binding RadioButtonList you can set SelectedIndex.
my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.