I'm using asp.net 4.5 web forms with VS2017 and using dropdownlist to get the values
from the database and try to get the value but it keeps giving me only the top value, so I'm asking for help.
The way I binded the value is this :
protected void Page_Load(object sender, EventArgs e)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
and it works nicely and gets all the list in customer table and populates the data in cust column in the id : customerDropDownList.
And then I tried to get the value from customerDropDownList by having a testBtn
with testLbl attached and used
protected void testBtn_Click(object sender, EventArgs e)
{
testLbl.Text = customerDropDownList.SelectedValue;
}
and it only selects the top element always.
I suspect this has to do with the lifecycle of asp.net and am studying about it
but cannot find the clear answer to solve this.
Could anyone help me on this?
Probably because you are resetting the dropdown list on each page load. Try only setting the ddlist the first time.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
}
you are missing Page.IsPostBack in page load event
more on Ispostback here
With your code are you not noticing your dropdown list is doubled on the click ?
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
var db = new dbContext();
var CustItem = db.customer.ToList();
customerDropDownList.DataSource = CustItem;
customerDropDownList.DataTextField = "cust";
customerDropDownList.DataValueField = "cust";
customerDropDownList.DataBind();
}
}
Make sure to load the dropdown only once. your page load will be called on the button click as well.
Keep a break point and play around !! Good luck
Related
I have this code :
private void frmWeld_Load(object sender, EventArgs e)
{
List<Weld> lst = _weldRepository.Get().ToList();
gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true };
}
I want to load my data into devExpressGridView
As you can see my data is loaded but the gridview can't show the data and my break point doesn't pass from gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true} and my program remains with this state.
Why?
I just add new column to my gridview after this the problems occurreded.
I use entity framework ,when i change the database my application creates a new database using code first and after that my data is lost but the problem that i said is solved.
Since your code is executing in Load event, add ForceInitialize() to your code.
private void frmWeld_Load(object sender, EventArgs e)
{
// your previous code
gridControl.ForceInitialize(); <- add this line
}
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
protected void Page_Load(object sender, EventArgs e)
{
bindbranches();
bindbranches1();
}
public void bindbranches()
{
DataTable dtbranch = new DataTable();
dtbranch = objsupplyBAL.getbrnch();
ddlbranch.DataSource = dtbranch;
ddlbranch.DataBind();
ddlbranch.Items.Add(new ListItem("--select--", "0"));
ddlbranch.SelectedIndex = ddlbranch.Items.Count - 1;
}
public void bindbranches1()
{
DataTable dt = new DataTable();
dt = objsupplyBAL.getbrnch();
ddlbranch1.DataSource = dt;
ddlbranch1.DataBind();
ddlbranch1.Items.Add(new ListItem("--select--", "0"));
ddlbranch1.SelectedIndex = ddlbranch1.Items.Count - 1;
}
My dropdownlist's are not binding without refreshing.If i select one dropdownlist another one is refreshing. What i have to add extra to my code. Can any one tell...
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindbranches();
bindbranches1();
}
}
if you add this...it's work properly ...first try this...
The reason that is happening is you are running the code every time the page postsback, try the following to only populate the items once (on the initial page load) :
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
bindbranches();
bindbranches1();
}
}
Alternatively you can also handle the Page.Init event to run this code, this will change the dropdowns when the page is first loaded and will keep the values throughout subsequent postbacks :
protected void Page_Init(object sender, EventArgs e) {
bindbranches();
bindbranches1();
}
If you want the second dropdown to refresh only when the first item is selected, try the following solution :
protected void ddlbranch_SelectedIndexChanged(object sender, EventArgs e) {
bindbranches1();
}
And bind ddlbranch_SelectedIndexChanged to the selected index changed event of your ddlbranch control. This will only run the code when the page is initially loaded and when the user selects an item from the ddlbranch dropdown
You will have to set AutoPostBack Property of the drop down to true if you want to fill another drop down on change of one drop down so on change event will start executing
Loot # http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx
I have the following global List -
public partial class About : System.Web.UI.Page
{
List<string> changes = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> changes = new List<string>();
}
I am trying to add string values to changes like so -
protected void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
DataListItem item = (DataListItem)chk.NamingContainer;
TextBox txt = (TextBox)DataList1.Items[item.ItemIndex].FindControl("aliasTextBox");
string text = txt.Text;
changes.Add(text);
ViewState["array"] = changes;
}
So I am trying to store all changes made in the dataList into changes so that when a user clicks -
protected void Button5_Click(object sender, EventArgs e)
{
List<string> changes = (List<string>)ViewState["array"];
foreach (string text in changes)
{
WebService1 ws = new WebService1();
ws.WebMethod(text);
}
}
However when it comes to clicking the button, I get a null reference exception on the - changes.add(text) section. How can I store all the values in changes to be available on button click?
The changes that you are making to a variable will not persist after the postback. You should save the value of this list 'changes' to a viewstate, session or application so that you can retain the values of the list even after the postback.
Add this code in the CheckBox1_CheckedChanged event after updating the list
ViewState["Somename"]=changes;
Add this code in the Button5_Click before accessing the list
changes= (List<string> )ViewState["Somename"]
Use IsPostBack
and change your List<string> changes = new List<string>(); to List<string> changes = null;
Add this code to your constructor
if(!IsPostBack)
{
changes = new List<string>();
}
Other wise you can use ViewState
I have a DDL and a ASP .net Grid view in my aspx page. I have two methods getALLProgram and getProgramBy name, both are working fine. My problem is: when the page is loaded for the first time, I want to call the getAllprogram method, after that if a User selects a program from DDL I want my getprogramByname method to be called.
How here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindProgramDDL();
BindGrid();
}
//BindProgramDDL();
}
protected void BindGrid()
{
string strProgramCode = DDLProgram.SelectedIndex.ToString();
List<FormGridEntity> gridEntities = new List<FormGridEntity>();
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
//throw new NotImplementedException();
}
protected void BindProgramDDL()
{
List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = FormSaleSubmit_BAO.GetAllPrograms();
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
The Code is incomplete. i am still working on it. But I not getting the logic How will I make this happen that I have told you here. I hope I made my question clearly, if it confusing, please let me know what else I should provide here.
You should check in your BindGrid if any program has been selected or not and route the call as per that. For example,
protected void BindGrid()
{
...
if (DDLProgram.SelectedIndex >= 0)
{
// program selected
var programCode = DDLProgram.SelectedValue;
data = GetProgramByName(programCode);
}
else
{
// get all programs
data = GetAllPrograms();
}
// bind data with grid
}
You can either call BindGrid in page_load unconditionally (i.e. in post-back scenarios also) or invoke it on your DDL change.
how about writing getProgramByname on a selected index changed event of a drop down list and getALLProgram on page load event ?
I hope, I was clear on what your doubt and the above mentioned suggestion did helped.
Just change these 2 things
protected void BindGrid()
{
List<FormGridEntity> gridEntities = (DDLProgram.SelectedIndex==-1)
?FormSaleSubmit_BAO.GetAllPrograms()
:FormSaleSubmit_BAO.GetProgramByName(DDLProgram.SelectedValue);
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
}
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
BindGrid();
}