How can I load the Same ASP Grid View with different conditions? - c#

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();
}

Related

Cannot get value from database-bound dropdownlist

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

how to bind more than one dropdownlist without refreshing?

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

Keep page contents while post back

When ever i do postback the page get blanked, i have seen some pages, where page is not being blank while post back
can some give me idea how it can be done
Thank You
private void Page_Load()
{
if (IsPostBack)
{
// It is a postback
}
else
{
// It is not a postback
}
}
refer this link
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
Did you bind data on post back?
example:
protected void Page_Load(object sender, EventArgs e)
{
textBox1.Text = "";
}
try adding if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
textBox1.Text = "";
}
}
Try using update panels. This way you only have to post back the areas that require post back. You will not feel the flicker.

Validate data in Radgrid before changing pages

I want to validate the users changes on a page before allowing them to go to another page. If the validation fails I want to stop the pager from changing the page.
For example:
protected void rgOrderItem_PageIndexChanged(object source, GridPageChangedEventArgs e)
{
if (Mapvalues(false))
{
rgOrderItem.CurrentPageIndex = LastPageIndex;
rgOrderItem.DataBind();
}
}
This does not work. The pager changes regardless. Anyone know how to stop a page change event?
Thanks, Tony
Please try with the below code snippet.
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
if (Mapvalues(false))
{
e.Canceled = true; //Prevent to execute pagging functionality
}
}
protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
{
if (Mapvalues(false))
{
e.Canceled = true; //Prevent to execute pagging functionality
}
}

Creating, Using and Discarding Temporary Value not working

I am trying to assign a ViewState value in my application with a SelectedIndexChanged function. Once it's assigned the postback will use the value to change some data and then set the value to zero but I can't seem to get it to work correctly. The controls are all created dynamically on Page_Load.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
CreateAttributeControls();
TempProductVariantId = 0;
}
Create Attribute Controls
public void CreateAttributeControls()
{
...
var ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
ddlArtistArtworks.AutoPostBack = true;
...
}
ArtistArtwork_SelectedIndexChange
protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
TempProductVariantId = int.Parse(ddl.SelectedValue);
}
TempProductVariantId ViewState Save
public int TempProductVariantId
{
get
{
if (ViewState["TempProductVariantId"] == null)
return 0;
else
return (int)ViewState["TempProductVariantId"];
}
set
{
ViewState["TempProductVariantId"] = value;
}
}
When I load the page everything is fine. I change the DropDownList's value, It posts back, and the value is not set. Change it again the value is set and continues to change as I change the value of the DropDownList.
Any guidance on this would be greatly appreciated.
Note: I have tried changing when CreateAttributeControls() is called. In OnPreRender for example. I was given this to understand the lifecycle of the page Life Cycle
That's because you are essentially recreating the dropdown on every postback..
try this
public void CreateAttributeControls()
{
...
DropDownList ddlArtistArtworks;
if (!IsPostBack)
{
ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.AutoPostBack = true;
}
else
{
ddlArtistArtworks = (DropDownLise)divAttribute.FindControl("ddlArtistArtworksTest");
}
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
...
}
For dynamically added controls, the event handler has to be linked up everytime so that has to be done outside the if-block, unconditionally.

Categories

Resources