DataBind based on Parameter - c#

I have a DataSource assigned to a DropDownList (DD2) that has a WHERE clause specified by the contents of another DropDownList (DD1). When I change the DD1, however, that DD2 does not reload its data. Is there a way to get DD2 to reload when DD1 is changed?
I've tried:
protected void DD1_SelectedIndexChanged(object sender, EventArgs e)
{
DD2.DataBind();
}
But this appears to do nothing.

Move the logic of setting the DD2 DataSource and DD2 DataBind() to a new function and call that instead. Your problem is likely that the datasource is getting incorrectly set.
protected void DD2Bind()
{
DD2.DataSource = //fetch data source
DD2.DataBind();
}
protected void DD1_SelectedIndexChanged(object sender, EventArgs e)
{
DD2Bind();
}

Related

calling multiple dropdownlist index change methods within the another ddl index change

I am using visual studio 12, coding in asp.net using c#.
I have 3 dropdownlists in my code, all of which are bound by lists that I created.
I need some advise as to which method is better to call the postbackvalues of ddl's to perform a task.
Option 1
When a user selects an item from drop down list 3, the postbackvalue is sent from Dropdownlist3_SelectedIndexChanged to the dropdownlist2_selectedindexchanged by calling the method. Only after I have both the postbackvalues I would like to produce a chart. Regardless of what the chart holds and regardless of what the data is in the drop down list.
So something like
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to have the postbackvalue of drop down list 3 here so i can use its value and dropdownlist2's postbackvalue to produce a chart.
}
and in the dropdownlist3
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to call DropDownlist2_SelectedIndexChanged(...) method so I can send the postbackvalue of DDL3 for use in DDL2.
}
Option 2:
Define a global variable that stores the postbackvalue of Dropdownlist3 and use that value in Dropdownlist2_SelectedIndexChanged method for further use such as produces a chart.
I have read a lot about global variables but do not understand the con's about them.
I am not sure if this is what you are after, but perhaps having a third method which is called that handles the updating of the chart...
for example
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
private BuildChart()
{
var ddl3Value = DropDownList3.SelectedValue;
var ddl2Value = DropDownList2.SelectedValue;
if(ddl3Value != null && ddl2Value != null)
{
//build chart.
}
}

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

restoring dropdown selected values in c# after refresh by retrieving values from database

I have created a dropdownlist in my webpage which is outside the gridview and I have added automatic refresh. My problem is that I`m unable to retain the selected value in dropdownlist after the refresh. It goes to the default settings in the dropdown. Please help.
Thanks a lot for replying..
part of my code goes this way....
page_load(...)
{
Refresh
if(!IsPostBack)
{
//calling my function which includes databind..
myfunction();
}
}
i tried the same code as you people suggested but its not working..
even now after refresh, the default values appear in dropdownlist
You probably need to implement something like this in your page_load handler:
if (IsPostback) return;
//here populate the dropdown
Let me guess your Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataBindGridView(); // loads the datasource of the grid and calls gridView1.DataBind();
DataBindDropDown(); // loads the datasource of the dropdown and calls dropDown1.DataBind();
}
Do not reload all on every postback, just if !(IsPostBack):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataBindGridView();
DataBindDropDown();
}
}
If you need to refresh your GridView don't use Page_Load but the appropriate event handler. If you use an ASP.NET Timer to reload your page periodically to refresh the grid, use it's Tick event.
protected void GridRefreshTimer_Tick(object sender, EventArgs e)
{
DataBindGridView();
}

How to call a method that has parameters from a click event

I have a method that gets a column value from an output gridview. How do I call this method in a button click event so that I can use the column value? Below is the method and button click event.
string mailAdd;
public void get_value(object sender, GridRecordEventArgs e)
{
mailAdd = e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//call get_value here
}
you cant ' because this method expect the EventArgs from the Sender which is the Grid object in your case , what I'd recommend is in
Get_value set the e.record["emailAddress"].toString() to a session and call in button
public void get_value(object sender, GridRecordEventArgs e)
{
Session["emailAddress"]= e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//you can use this
string _myEmail=Session["emailAddress"];
}
regards
is the part of the grid row? if yes then you should be using ItemCommand, if the button is separated from grid, then you may need to ascertain (dynamically) the row from which you want to read the email address. If you have the info, then you can easily access grid's content by row index and columnname, to get the email address, then rest is obvious.

ASP.NET gridview binding doesn't work / control doesn't show up

another beginner problem. Why isn't the following code with an asp.net page not working?
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
}
GridView is the GridView control on that asp page. However, no grid shows up at all. It's both enabled and visible. Plus when I debug, the GridView.Rows.Count is 0. I always assumed you can just add generic Lists and all classes implementing IList as a DataSource, and the gridview will then display the content automatically? Or is the reason here that it's been done in the page_load event handler. and if, how can I fill a grid without any user interaction at startup?
Thanks again.
You should call DataBind().
You forgot to call the GridView's .DataBind() method. This is what will link the control to its data source and load the results.
Example:
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
this.GridView.DataBind();
}
Unlike in winforms, for ASP developement you need to specifically call GridView.DataBind();. I would also break out that code into a separate method and wrap the initial call into a check for postback. That will save you some headaches down the road.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
List<string> list = new List<string>();
list.Add("Teststring");
bindMydatagrid(list);
}
}
protected void bindMydatagrid(List<string> list)
{
gv.DataSource = list;
gv.DataBind();
}

Categories

Resources