disable dropdownlist option after selection asp.net webform - c#

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
}

Related

Disable checkbox column based on user authorization

I have a grid and in the grid I have two Item Template columns - Checkbox 1 and Checkbox 2.
I want to disable the whole Checkbox 1 column based on if a user is an admin or general user.
So say, if a user is an 'admin', I want both columns to be enabled and user can check/uncheck both the columns.
But, if a user is 'general user', I want to disable 'Checkbox1', so that user can only check/uncheck Checkbox 2. But can see values for 'Checkbox 1' column and can't edit it or make changes.
Presently, I am achieving this behavior as below-
protected void grdchkbox_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkbox1 = (e.Row.FindControl("Checkbox1") as CheckBox);
if (IsGeneralUser(empid))
{
chkbox1.Enabled = false;
}
}
}
The problem with this is the page load time.
It is checking for user's role everytime a row is bound which means making db calls at each row bound.
Also, it's disabling a checkbox for each row separately.
Is there a way, I can disable the whole checkbox 1 column in one go?
depending on how your gridview is setup, you could try just disabling editing on the column or making it read only.
You could also simply save the users status in a session variable, so that you only have to make the db call the first time, which should speed up the whole process as well.
Using a session variable would look something like:
protected void grdchkbox_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkbox1 = (e.Row.FindControl("Checkbox1") as CheckBox);
//we check to see if we already have the users status saved in session
var isUser = HttpContext.Current.Session["IsGeneralUser"];
//if we dont, we run the IsGeneralUser method and save the result in session
if(isUser == null)
{
isUser = IsGeneralUser(empid);
HttpContext.Current.Session.Add("IsGeneralUser", isUser);
}
if (Convert.ToBoolean(isUser))
{
chkbox1.Enabled = false;
}
}
}

Add dynamic ASP.NET controls via a DropDownList to the page

- Primary Info:
In my recent project, I need to have a page with a DropDownList with some items like 'firstName' , 'lastName' , 'Age' and etc. I want to add optional controls to the page when every item selected by user. For example when user select the 'Age' another dropdownlist created dynamically with these values : 'Less than 10'
'Between 10 and 30'
'more than 30'
Here is a button that add this user selection to listBox and let user to choice another options. (I made a query at last according to user choices and send it to db)
- What I do:
I create a dropDownList and set it's AutoPostBack property to true and adds some items in it and user must select one of those item. then I add user SelectedValue of dropDownList in a Cache variable before page post back happens:
protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
{
Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
}
When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder:
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
- What is the problem?
When I try add new control in current Button_Click event, control added successfully to page but I can't find it by placeHolderFirstItem.Controls.Find("textBoxName") actually placeHolderFirstItem.Controls.Count is always zero. So I can't get textBoxName.Text values.
I try to google that for any solution and I found some solution that I must add controls in Page.OnInit so I add controls in overridden OnInit(e):
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before DropDownList_SelectedIndexChanged !
so how can I add new controls to place holder exactly when user change the dropDownList value and how can I read new controls value?
Thanks in advance,
Mohsen.
- Updated:
Here is the better solution
(http://forums.asp.net/p/1959726/5596531.aspx?p=True&t=635244790943067485&pagenum=1)
When you are dynamically adding controls, you have to reload the controls into the control tree everytime thereafter for it to appear. With the help of viewstate, you could change your code sample to have:
ViewState("ShowTextbox") = true
And then in your init routine:
protected override void OnInit(EventArgs e)
{
if (!Page.IsPostBack) return;
if (ViewState("ShowTextBox") == true) {
var textBoxName = new TextBox
{
ID = "textBoxName",
CssClass = "str-search-textbox-highlight",
ViewStateMode = ViewStateMode.Disabled
};
placeHolderFirstItem.Controls.Add(textBoxName);
}
}
Please note it's much easier to have a control on the control tree, and then show/hide by setting Visible to true/false, because of these ASP.NET control tree issues.

Checkboxlist loop is not working

I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work
Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.

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.

Preserve datalist data on load event in 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;
}
}

Categories

Resources