I have defined a placeholder in my page like this;
<asp:PlaceHolder ID="attrPlaceHolder" runat="server"></asp:PlaceHolder>
I am populating this place holder from a database table using query string productId like this;
// obtain the attributes of the product
DataTable attrTable = CatalogAccess.GetProductAttributes(productId);
// temp variables
string prevAttributeName = "";
string attributeName, attributeValue, attributeValueId;
// current DropDown for attribute values
Label attributeNameLabel;
DropDownList attributeValuesDropDown = new DropDownList();
// read the list of attributes
foreach (DataRow r in attrTable.Rows)
{
// get attribute data
attributeName = r["AttributeName"].ToString();
attributeValue = r["AttributeValue"].ToString();
attributeValueId = r["AttributeValueID"].ToString();
// if starting a new attribute (e.g. Color, Size)
if (attributeName != prevAttributeName)
{
prevAttributeName = attributeName;
attributeNameLabel = new Label();
attributeNameLabel.Text = "<li class=\"txt\">" + attributeName + ":</li>";
attributeValuesDropDown = new DropDownList();
attrPlaceHolder.Controls.Add(attributeNameLabel);
attrPlaceHolder.Controls.Add(attributeValuesDropDown);
}
// add a new attribute value to the DropDownList
attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
}
However, when inside a button click event, when I loop through this place using visual studio debugging, I saw that the visual studio studio debugger first hit the "attrPlaceHolder.Controls" word in my foreach loop, then secondly comes to 'in' keyword (in foreach loop) but it isn't hitting the first two words (i-e 'Control cnt' in my foreach loop. Here it looks;
protected void ButtonBuyNow_Click(object sender, EventArgs e)
{
// Retrieve the selected product options
string options = "";
foreach (Control cnt in attrPlaceHolder.Controls)
{
if (cnt is Label)
{
Label attrLabel = (Label)cnt;
options += attrLabel.Text;
}
if (cnt is DropDownList)
{
DropDownList attrDropDown = (DropDownList)cnt;
options += attrDropDown.Items[attrDropDown.SelectedIndex] + "; ";
}
}
// Add the product to the shopping cart
ShoppingCartAccess.AddItem(productId, options);
}
Basically I need 'options' variable to be populated but it isn't hitting the foreach loop inside, therefore I am not able to get the 'options' variable populated.
This is a serious problem in my application. Please tell me why I can't get the inside the foreach loop.
NOTE:
please note that this isn't the complete code of my entire page. My rest of the code executes correctly.
why I can't get the inside the foreach loop
Because the list is empty.
Why is the list empty? (Would be the next logical question)
Because, at ASP.Net, dynamically created controls must be re-created at Page_Init in order to exist. When you create them at this stage, the page lifecycle will bind the viewstate and will be ready for use.
If you receive a postback (from the button, for example) and don't recreate them, they simply don't exist.
Related
I have two dropdown list as part of a webform. If I load the list items during the page load event, the first list's selected value is set, but then overwritten by the second list's selected value being set. If I do the same during a non-page load event, they work fine. I am trying to figure out why the second list's selected value is overwriting the 1st during page load.
This is a C# asp.net web application. I am capturing an id from a previous page on page load (if present) and then selecting the dropdown values necessary on page load. If no id present then the user select's an option from a different drop down list and that populates the form fields and selects the appropriate values in the two relevant dropdown lists.
if (!Page.IsPostBack)
{
Load_Courses();
Load_Instructors();
try
{
// Capture course id from another page
if (Request["course_id"] != null)
lstCourses.SelectedValue = Request["course_id"].ToString();
// Load selected course
if (lstCourses.SelectedValue != "")
{
course.Load(int.Parse(lstCourses.SelectedValue));
Load_Form(course);
Session["course"] = course; // Save course
}
}
catch
{
course = new Course();
lstCourses.SelectedIndex = 0;
Clear_Form();
Session["course"] = null;
}
}
}
Load_Form(Course course)
{
// Show course data
txtName.Text = course.GetName();
txtDescription.Text = course.GetDesc();
txtStartDate.Text = course.GetStartDate();
txtEndDate.Text = course.GetEndDate();
txtRegCloseDate.Text = course.GetDateToCloseRegistration();
txtSeats.Text = course.GetNumberOfSeats().ToString();
txtLocation.Text = course.GetLocation();
lstInstructors.SelectedValue = course.GetInstructorId().ToString();
if (course.GetAssistantInstructorId() != 0)
lstAssistants.SelectedValue = course.GetAssistantInstructorId().ToString();
else
lstAssistants.SelectedIndex = 0;
}
lstInstructors.SelectedValue = 4 initially lets say, then when the lstAssistants.SelectedValue is set to a value, lets say 6, lstInstructors.SelectedValue is also set to 6. This does not occur when calling Load_Form() from outside page load event.
Greeting of the day everyone.
Hoping you are all well. I need your help to solve a simple issue ( I an new to C# + ASP.net).
I have created a ASp.net portal. When a user open that portal (web page). the Webpage give him a list of Groups whose he is the member of and the list of group whose he can add to himself..
It also contain two Drop Downs. One contains the list of groups, whose use can add to themselves. The second one contains the list of groups , whose the user is member of, and he can remove it from those groups.
Adding and removing code is working fine. But I have to refresh the page manually to show the updated information. After clicking the Add or Remove Button page is not refreshing with the updated information.
I tried to use redirected to main page using Response.Redirect("Success.aspx")
But nothing working,
protected void Page_Load(object sender, EventArgs e)
{
string uN = "domain\\michael.jackson";
//SystemResources sr = new SystemResources();
//ActiveUser usr = sr.GetUserDetails(uN).FirstOrDefault();
LabelUserName.Text = "michael.jackson";
// Getting the list of groups using of praticular OU the classOrgUnitGroup
string sdomain = ClassGroupPortalSettings.getDomainName();
string sInterstOU = "OU=testing,DC=analysys,DC=com";
classOrgUnitGroup a = new classOrgUnitGroup();
List<string> allGroups = a.GetGroupFromOu(sdomain, sInterstOU);
string grouplist = "<ul>";
foreach (string group in allGroups)
{
grouplist = grouplist + "<li><a href='showgroupmembers.aspx?group=" + group + "'>" + group + "</a></li>";
}
grouplist = grouplist + "</ul>";
lblOpenGroups.Text = grouplist;
//// 4. Finding users group membership.
string sDomain = ClassGroupPortalSettings.getDomainName();
classUserGroupMembership myMembership = new classUserGroupMembership();
List<string> myGroupsMem = myMembership.getMyGroupMembership(sDomain, "domain\\michael.jackson");
string glList = "<ul>";
string openlList = "<ul>";
string otherGroup = "<ul>";
foreach (string grp in myGroupsMem)
{
//BulletedListMyGroups.Items.Add(grp);
glList = glList + "<li>" + grp + "</li>";
if (allGroups.Contains(grp))
{
DropDownListMyGroups.Items.Add(grp);
openlList = openlList + "<li>" + grp + "</li>";
}
else
{
otherGroup = otherGroup + "<li>" + grp + "</li>";
}
}
glList = glList + "</ul>";
openlList = openlList + "</ul>";
otherGroup = otherGroup + "</ul>";
LabelOtherGrp.Text = otherGroup;
LabelOpenGrp.Text = openlList;
// LabelMyGroup.Text = glList;
foreach (string emailGroup in allGroups)
{
if (!myGroupsMem.Contains(emailGroup))
{
DropDownListOpenGroups.Items.Add(emailGroup);
}
}
}
This is code which run when a page reload.
The issue is : When I click on Remove Button or Add Button, it run the code to add or remove user from selected group. when page reload after clicking on button. The Group membership labels. and Dropdown box is not updating with code :(
I'm assuming your scenario like this, you're having two Drop down. One is full of records and the items can be select. Once items are selected, user will submit the button then records will save into database/xml. Later you're retrieving the items from db/xml and showing that in second Drop down.
Now you're issue is that the records are not showing in second Drop down after clicking the submit button,
If it so, you have two options.
1) You can pull your records as soon as insert things made into db.
2) You can pull your records in Page load itself as I mentioned below,
If(Postback) // Or If(PostBack == true)
{
// Pull your records and display in Drop down.
}
It is just the order of events which causing confusion IMO. Page_Load is called first and then control events(click, change etc). So on add/delete you need to rebind the items.
https://msdn.microsoft.com/en-gb/library/ms178472%28v=vs.100%29.aspx
Move out the data bind logic to a function and call function at the end of click event as well. Then you can reuse the function in page load and put logic to bind in page load only of its not postback because you might be rebinding unnecessarily in page load.
protected void Page_Load(object sender, EventArgs args)
{
if(!IsPostBack)
{
DataBindLogic();
}
}
private void DataBindLogic()
{
/*Put databind code here */
}
protected void RemoveBtn_Click(Object sender,Eventargs args)
{
/*Do db update */
DataBindLogic();
}
Thanks a lot everyone and and each of you for your time and help and energy, thanks a ton.
The issue is resolved, I have added another lines of code in the Postback, which checks the action of the User.
For example, if used Remove from the Group,
Then get the name of the Group and use action REMOVE
Then Update the DropDown box and Current membership box.
I did the same for the add group too, and it resolved the issue for me.
Thanks again , and whishing you a great day ahead.
Regards and Best wishes
Aman
I have a form that I would like to reuse for both adding a new record and editing an existing record. I am able to successfully load the page with the relevant data when I select a record from a GridView and I am able to update the db record appropriately. However, my issue is trying to use the form for both executions. Here is my logic in code behind: (I assign a session variable when I click on the row in GridView and this does work successfully)
protected void Page_Load(object sender, EventArgs e)
{
resultOutput.Visible = false;//Output results as to whether or not a record was added successfully is automatically hidden at page load
//Checking to see if session variable has been created
if (Session["editID"] != null)
{
//Create objects to get recipe data
dbCRUD db = new dbCRUD();
Recipe editRecipe = new Recipe();
//Grabbing session ID and assigning to a variable in order to remove the session variable
var id = Convert.ToInt32(Session["editID"]);
Session.Remove("editID");
//Call method to retrieve db data
editRecipe = db.SelectRecord(id);
//Populate results to text boxes
recordID.Text = id.ToString();
addName.Text = editRecipe.Name;
addTypeDDL.SelectedValue = editRecipe.Meal;
addDifficultyDDL.SelectedValue = editRecipe.Difficulty;
addCookTime.Text = editRecipe.Cook_Time.ToString();
addDirections.Text = editRecipe.Directions;
//Change Button Text
submitRecord.Visible = false;
changeRecord.Visible = true;
//Change Title Text
addEditTitle.Text = "Edit Recipe";
}
}
protected void submitRecord_Click(object sender, EventArgs e)
{
//Variables for execution results
var modified = "";
int returned = 0;
//Creating the recipe Object to pull the values from the form and
//send the recipe object as a parameter to the method containing insert stored procedure
//depending on Add or Edit
Recipe recipe = new Recipe();
recipe.Name = addName.Text;
recipe.Meal = addTypeDDL.Text;
recipe.Difficulty = addDifficultyDDL.Text;
recipe.Cook_Time = int.Parse(addCookTime.Text);
recipe.Directions = addDirections.Text;
//Creating object to access insert method
dbCRUD newRecord = new dbCRUD();
//Checking to see if the page is loaded for edit or new addition
if (Session["editID"] != null)
{
//If recordID exists, recipe will be passed as to UpdateRecord method
recipe.Recipe_ID = int.Parse(recordID.Text);
returned = newRecord.UpdateRecord(recipe);
modified = "has been edited.";
Session.Remove("editID");
}
else
{
//If recordID does not exist, record will be passed to InsertRecord method (new recipe)
returned = newRecord.InsertRecord(recipe);
modified = "added";
}
//Method returns 0 if successful, 1 if sql error, 2 if other error
if (returned == 1)
{
resultOutput.Text = "There was an sql exception";
resultOutput.Visible = true;
}
else if (returned == 2)
{
resultOutput.Text = "There was a non sql exception";
resultOutput.Visible = true;
}
else
{
resultOutput.Text = "\"" + addName.Text + "\" recipe " + modified;
resultOutput.Visible = true;
}
}
I have issues on the if(Session["editID"] != null) line - I am always moved to the else logic and the if logic never runs.
Here is my click method in the GridView:
protected void Grid_Recipe_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(Grid_Recipe.SelectedDataKey.Value);
Session["editID"] = index;
Response.Redirect("addRecord.aspx");
}
My question is how can I control execution during the submitRecord_Click event so that I call the appropriate method. Thanks!
Have you considered using
if(Page.IsPostBack)
{
code here
}
To detect whether you are posting back to the page? Then you could check your value of the item. I see no reason the code shouldn't be in the Session variable - have you tried putting a breakpoint in there to see if the code actually gets in there?
Also does your addRecord.aspx just add the record? If so, just add the record in this class, but use the PostBack check to see. Could you just make sure you are saving in the right context aswell:
// Outside of Web Forms page class, use HttpContext.Current.
HttpContext context = HttpContext.Current;
context.Session["editID"] = index;
...
int Id = (string)(context.Session["editID"]);
I was able to figure out my issue - which actually turned into two issues. First, I had to put my Page Load logic in a if(!IsPostBack) statement because I could not edit the page. Reason being is I was loading the originally posted data to the form on page load, which executed before my logic. Adding the if(!IsPostBack) control statement fixed this issue. From there, I'm still using a session variable to control code behind logic, only I made sure keep my session variable only between the form and the gridview. Basically, when the gridview loads and it is not a post back, the session variable is cleared. This let's me set a new session variable when I click on a row and then the session variable is cleared once I return to the grid to see the results. Thanks for the help!
I'm trying to dynamically add a RequiredFieldValidator to a RadioButtonList in a repeater, but it fails with the error:
Unable to find control id 'rblAccessory_40' referenced by the 'ControlToValidate' property of ''.
The code for this section is:
if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
{
Label lblAccID = (Label)e.Item.FindControl("lblAccID");
RadioButtonList rblCondition = (RadioButtonList)e.Item.FindControl("rblCondition");
rblCondition.ID = "rblAccessory_" + lblAccID.Text;
if (conditionList.Count() > 0)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
rfv.ErrorMessage = "Please complete the accessories section";
pnlValidation.Controls.Add(rfv);
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
}
foreach (ListItem li in rblCondition.Items)
{
li.Text = "";
li.Value = "AccessoryID_" + lblAccID.Text + "-ConditionID_" + li.Value;
}
}
}
It is definitely finding the RadioButtonList (rblCondition) because the data is binding correctly at this point:
rblCondition.DataSource = conditionList;
rblCondition.DataValueField = "id";
rblCondition.DataBind();
So I don't understand why the error says it is unable to find the control ID.
I've tried specifying the control ID manually, as below:
rfv.ControlToValidate = "rblAccessory_" + lblAccID.Text;
and have also tried:
rfv.ControlToValidate = rblCondition.ID;
lblAccID is a hidden text field used to store the ID of the row in the repeater.
Assign the control to validate property AFTER the ItemCreated event. I know this sounds weird, how would you still have the reference to the dynamically created control? I've gotten around this by keeping a reference to a List<Action> reference which I add things to during ItemCreated to be executed later.
In your control class you'll declare your List<Action> object:
List<Action> deferringControlToValidateUntilPreRender = new List<Action>();
Inside the ItemCreated event you'll have a line that looks like:
deferringControlToValidateUntilPreRender.Add(() => rfv.ControlToValidate = rblCondition.UniqueID);
And then, later, perhaps in PreRender:
foreach(var deferredAction in deferringControlToValidateUntilPreRender) action();
Since the RequiredFieldValidator is client-side code, you need to use the client id of the control. Like this:
rfv.ControlToValidate = rblCondition.ClientID;
Some more information from MSDN if you are interested:
Control.ClientIDMode Property
Also, an SO discussion on the differences between ClientID and UniqueID (as referenced in #MushinNoShin's answer, which, IMHO, is incorrect):
Why is there a difference between ClientID and UniqueID?
i'm having issues retreiving the values out of a dynamically created dropdownlist. all controls are created in the Page_Init section. the listitems are added at that time as well from an array of listitems. (the controls are named the same so should be accessable to the viewstate for appropriate setting.)
here is the function that attempts to retrieve the values:
protected void Eng98AssignmentComplete_Click(object sender, EventArgs e)
{
String myID = "0";
Page page = Page;
Control postbackControlInstance = null;
// handle the Button control postbacks
for (int i = 0; i < page.Request.Form.Keys.Count; i++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
//Response.Write(page.Request.Form.Keys[i].ToString());
if (postbackControlInstance is System.Web.UI.WebControls.Button)
{
myID = Convert.ToString(
postbackControlInstance.ID.Replace("button_", ""));
}
}
String txtholder = "ctl00$ContentPlaceHolder$Eng098Instructors_" + myID;
Response.Write("MYID: " + myID + "<br/>");
DropDownList ddInstructorCheck = (DropDownList)Page.FindControl(txtholder);
Response.Write("Instructor Selected: "
+ ddInstructorCheck.SelectedValue + "<br/>");
}
here is the output I get, no matter which instructor was selected.....
MYID: 1_1
Instructor Selected: 0
ctl00$ContentPlaceHolder$Eng098Instructors_1_1
the name of the control is correct (verified via view source)....
ideas?
You're going to a lot of work to build this fancy string:
ctl00$ContentPlaceHolder$Eng098Instructors_1_1
That is the client ID of your control, not the server id. This code is running on the server side, and so you need the server id. To get that control using the server id, you need to do this:
ContentPlaceHolder.FindControl("Eng08Instructors_1_1");
Notice I didn't look in the page, because your content place holder created a new naming container.
Also, the way your loop is set up the myID variable will always end up holding the last button in the Keys collection. Why even bother with the loop?
Based on your comments, a better way to find the id of the dropdownlist is like this:
string id = ((Control)sender).ID.Replace("button_", "Eng098Instructors_");
why not just save the control in an instance in your class so that you don't have to use FindControl?
Do you also re-create the controls during the postback? Dynamically generated/added controls must be re-created with every request, they are not automatically re-created.
Why don't you cast the sender? This should be the button that caused the postback:
string myId = "0";
Button btn = sender as Button;
if (btn != null)
myId = btn.ID
...
You need to perform something like this because the UniqueID property is the key in Request.Form.
List<Button> buttons = new List<Button>();
List<DropDownList> dropdowns = new List<DropDownList>();
foreach (Control c in Controls)
{
Button b = (c as Button);
if (b != null)
{
buttons.Add(b);
}
DropDownList d = (c as DropDownList);
if (d != null)
{
dropdowns.Add(d);
}
}
foreach (String key in Request.Form.Keys)
{
foreach (Button b in buttons)
{
if (b.UniqueID == key)
{
String id = b.ID.Replace("button_", "");
String unique_id = "ctl00$ContentPlaceHolder$Eng098Instructors_" + id;
Response.Write("MYID: " + id + "<br/>");
foreach (DropDownList d in dropdowns)
{
if (d.UniqueID == unique_id)
{
Response.Write("Instructor Selected: " + d.SelectedValue + "<br/>");
break;
}
}
}
}
}
I'm not sure why you are generating the control in code (you can still add items dynamically if you do), but the code that generates the controls would probably be a huge help here. I'm guessing you are not setting the list item value, and instead just setting the list item text. Try seeing what you get from the SelectedText field and post your control creation function.
EDIT:
In response to your comment on #Martin's post, you said "yes I recreate the controls in the Page_Init function each time the page is created (initial or postback)". Are you also setting the selected value when you create them?
You can also use controls on the page even if your data comes from a database, the controls themselves don't have to be dynamically generated.
How about this?
((Button)sender).Parent.FindControl(myid)
Edit:I misunderstood your question. But i think you should follow page lifecycle. it is common issue for dynamically created controls.
I did some research and here is some info about Dynamically Created Controls may help you...
I had 2 catches.... here's what they were.
1. I didn't clear the table I was adding to before re-creating the controls.
apparently my attention to detail was off yesterday, i'm pretty sure the ctlXX frontrunner of the control was some different number upon postback due to how I was recreating the controls.
2. I was assigning the same list to all the dropdownlist controls.
once I called the lookup upon each creation a dropdownlist control, all works well.
anyway for what it's worth....