Preserve datalist data on load event in c# - c#

I have datalist containing radio buttons and check boxes. If I select some of them and then redirect to another aspx page. when I come to previous page the radio buttons and check boxes in datalist must be selected. I have selected data in session variable. How to do this using c#.

Using the ItemDataBound event I would retrieve the radio button and compare it's value to your Session variable
something like this:
protected void MyDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButton myRadioButton = e.Items.FindControl("myRadioButton") as RadioButton;
if (myRadioButton == null) { }
else
{
if (string.IsNullOrEmpty(Session[myRadioButton.Value])) {}
else myRadioButton.Checked = true;
}
}

Related

Saving entire page's controls state in session

I have many aspx pages which are a process to create a final form. eg,
Select Item Type Page -> Select item color page -> Select item quantity page -> Complete Page
Each page have a next and a previous button and when I move from the first page to the second page and the user clicks the previous button returning to the first, the control's states will be reset losing all the user's input
I thought of saving the controls in a session this way:
protected void btn_Next_Click(object sender, EventArgs e)
{
if (validateForm() == true)
{
Session["test1"] = RadioButtonList1;
Response.Redirect("nextpage.aspx");
}
}
And loading it this way:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["test1"] != null)
RadioButtonList1 = (RadioButtonList)Session["test1"];
getInfo();
}
}
However, I selected the 2nd item in the radiobuttonlist1 and when i move to the 2nd page and back to the 1st, it didn't load the exact state of the radiobuttonlist1. The debugging shows it went into the RadioButtonList1 = (RadioButtonList)Session["test1"]; code
Can you try this :
Set the value attribute for each list-item and retrieve the selected value in a session like this
Session["test1"] = RadioButtonList1.SelectedItem.Value;
and while loading
RadioButtonList1.SelectedValue = (String) Session["test1"];

disable dropdownlist option after selection asp.net webform

I want to disable my dropdownlist after a user select the droopdownlist. So, if the user enter this page, the user only lead to
dropdownlist when they have not choose before
the selected item value if they have choose before, but they can not select again (means: it disabled)
http://i.stack.imgur.com/H7uYT.png
because every selected item here will trigger some calculation to the next page.
Do you have any tutorial to do this? Or can you help me to solve this?
Just set the DowpDownList's AutoPostBack property to true and handle the SelectedIndexChanged-event. There you can disable it:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
ddl.Enabled = false;
// store the SelectedValue somewhere, f.e. in the Session
// or Response.Redirect to the next page and pass it via url-parameter
}

Hide button on drop down selection -1

I need my create button to be hidden unless a facility is selected in my dropdown. When it is at -1 message i need my button to be hidden.
Code for button
<asp:Button ID="btnCreate" runat="server" Text="Create New" Width="89px" Font-Size="X-Small" OnClick="btnCreate_Click" />
Drop down code
private void ResetForm()
{
try
{
//facility dropdown
ddlFacility2.Items.Clear();
ddlFacility2.DataSource = this.DataLayer.model.MS_spGetFacilityInfo(null).OrderBy(x => x.FacilityName);
ddlFacility2.DataTextField = "FacilityName";
ddlFacility2.DataValueField = "FacilityID";
ddlFacility2.DataBind();
ddlFacility2.Items.Insert(0, new ListItem("All Facility Records..", "-1"));
BindGrid();
}
catch (Exception ex)
{
this.SetMessage(ex.ToString(), PageMessageType.Error);
AISLogger.WriteException(ex);
}
}
in first time page load if the default value selected is -1 you can set your button visible false as default.
in your droupdown list selected index change event you can enable/dissable button based on droupdown list selected value.
Add a OnSelectedIndexChange event to your dropdown list or add a clientside event to your dropdownlist. Double Click on your ddl you will see a function named ddlFacility2_OnSelectedIndexChanged in you code behind and add the below code to it.
Add AutoPostBack=true to you ddl
protected void ddlFacility2_OnSelectedIndexChanged(object sender, EventArgs e)
{
if(ddlFacility2.SelectedIndex>-1)
{
btnCreate.Enabled = true;
}
else
{
btnCreate.Enabled = false;
}
}
You can wire up a JQuery script that can bind to your DropDownList's selected value...
In this example, the button's visibility is bound on a click from another button:
$('#Button1').bind("click", function() {
$("#Button2").hide();
});
I dont know the exact syntax to use for the binding to selected value, but the above code should be a good place to start.

trigger an eventhandler on postback c#

I am pretty new to c# programming.
I have an issue that follows:
I have a GridView on a web form, a DropDownList and a Label control. When i select a value from a DDL a Grid View is populated with rows from the database that equal the DDL condition(in my case DDL represent Countries, GV lists the Cities).
When i delete the last City from a GV using a built in GV Delete function i would like to automatically write in a Label that there are no more Cities in selected Country.
How can i achieve that?
I tried to put
protected void GridView1_RowDeleted1(object sender, GridViewDeletedEventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}
but it didn't work.
Thanks for your help
Consider using PreRender event, when that event runs the Row.Count property contains the correct value. (decremented after delete)
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}

disable or hide listbox by dropdown selection in C#

HELLO
i have one dropdown box and one listbox ,
my dropdown box values is
1-ALL
2-CUSTOM
my listbox values is retrieved from sql database
email addresses
what i want to do is if i select ALL from the dropdown box , it will disable or hide the list box in the web page. , if i choose "CUSTOM" it will enable it again.
i tried this code, but it doesn't work
if (DropDownList1.Text == "CUSTOM")
{
ListBox1.Visible = true;
}
note : i put visible = false in the properties of the listbox1
where is the problem exactly ? and where should i put this condition on the .cs page ?
You need to add an event to the dropdown box, so that your code is executed when the event fires. If you are using the designer, select the dropdown then above where the properties are there should be a little lightning symbol. Click on that and you will see all the events that the dropdown can fire. In there look for SelectedIndexChanged. Double click in there and it will create some code for you, which will look something like:
protected void mycombobox_SelectedIndexChanged(object o, EventArgs e)
{
}
Put your code in that section.
UPDATED:
If the text in the dropdown is CUSTOM then use this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "CUSTOM")
{
ListBox1.Visible = true;
}
}
You will also need to set the AutoPostBack="true" on the DropDownList1.

Categories

Resources