I have an asp.net CheckBox, now I want to reload page after check or uncheck and use CheckBox.Checked information to choose sql query for gridview. I have put code like this in Page_Load method:
if (CheckBox1.Checked)
{
query = "select ...";
}
But nothing happen. I set AutoPostBack also. Tried to use event. Don;t know how this system works:/
EDIT:
Checkbox works ok, but the problem is in something different. After I click checkbox, in Page_Load method I will use my query to setup SqlDataSource. Looks like page is reloaded, but gridview is not refreshed. When i click on gridview's column mame (to sort this column), gridview is refreshed by new sql query. So i need to think how to refresh grid view after click check box.
It seems that you are not using IsPostBack property on page load event. If you not use this your CheckBox will be reset on every page load
Try this way
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Here do your stuff.
}
}
Related
I have a bunch of GridView on one page, and each have their own coordinating SqlDataSource.
They all load their coordinating data when initializing the page (on the page load). I want to Load the data for specific SqlDataSource(s) on the page when the user clicks a button, so that it is not trying to load ALL the data for all the different grids all at the same time when the page initially loads.
Note: I need to also have Paging working, and anything I have tried so far has broken the paging functionality.
Looking to tell the SqlDataSource what data to bind to it after the user clicks a button, and then have the specific GridView Bind that data to it and have the Paging functionality work with the data it was told to load.
Environment: C#, WebForms, ASP.NET, .NET 3.5, Controls SqlDataSource / GridView
From what I can tell it correctly binds the data, but the 'gridview' control keeps disappearing when I click one of the pages at the bottom for the paging.
Note: If I click the button again, it will show the correct page and the correct data for the page.
protected void btn_LoadData2_Click(object sender, EventArgs e) { Gridview2.DataSourceID = "DataSource2"; Gridview2.SelectCommand = "SELECT * FROM dbo.DataNeeded"; Gridview2.DataBind(); Gridview2.DataBind(); }
Add a Selecting event to each of your data sources. Then in the code behind check if IsPostBack and if IsPostBack is false then cancel the operation.
protected void MyDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
e.Cancel = true;
}
I have a problem with listbox. I would like to fill it with data and select Value (listbox.SelctedValue), using postback, after textbox is filled and Page.Validate() is fired. I try to use it in Page_Load. Everything works fine until I dont mark another user. It goes back to the the first one. I know its because I mark the first one again and again it in Page_Load, but how can I mark user after postback in other place? I cant use any buttons.
To be more clear, I have one text box, which causes postback after user put text there. After that I would like to check if Page.Isvalid and is yes, add that user to listbox (which also causes postbacks) and mark him. Without any buttons. How can I do it only once, using autopostback, not every PageLoad ?
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Page.IsValid)
{
//Mylistbox.SelctedValue = set Your Selected Value
}
}
}
I have a dropdown list which is populated on page_load using a linq query.
If I change the value in the dropdown then click a button to run an update query on the record, the original value is still there, if I step through in debug mode I can see the selected value is not changing at all
Here is how Im binding the data to dropdown
dlBookingRef.DataSource = d.BookingRef();
dlBookingRef.DataMember = "booking";
dlBookingRef.DataBind();
and here is the line in the function which gets the data from the form
item.booking_ref = dlBookingRef.SelectedValue;
Any idea why it's retaining its original value?
thanks
Put the binding code in if(!IsPostBack), it looks like that in button event as page load gets called due to that your dropdown list gets reset, so bind the dropdown only when page is not posted back:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
dlBookingRef.DataSource = d.BookingRef();
dlBookingRef.DataMember = "booking";
dlBookingRef.DataBind();
}
}
I have this button event:
protected void AddDetails_Click(object sender, EventArgs e)
{
DataSetTableAdapters.SelectFriendsTableAdapter sft = new DataSetTableAdapters.SelectFriendsTableAdapter();
try
{
sft.AddFriend(current, newFriend, false);
}
catch
{
error.Text = "Something happened. Bummer!";
}
}
in the try section, I'm adding entries in the Database. In the page there are Labels / Textboxes with the corresponding values.
Everything works fine. However, I need to refresh the page in order to see the changes after I click on this submit button.
I have added if(!IsPostBack) at the beginning of the PageLoad code, but I still need to visit another page / come back to see the changes.
Any ideas?
Thanks.
Thank you for your replies. I'm using a ListView:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSetTableAdapters.SelectFriendsTableAdapter sdt = new DataSetTableAdapters.SelectFriendsTableAdapter();
DataTable tab = sdt.SelectFriends();
ListView1.DataSource = tab;
ListView1.DataBind();
}
}
, so the ListView content should get updated.
If you don't want to refresh the page and you're using asp.net webforms you can use an UpdatePanel and place all your controls inside of it, etc... That will keep the changes made in the form / display when you submit the info. I'm assuming the form is on submitting the changed/new data as you didn't specifically state how the data was being updated/changed.
If you don't want to use an update panel, then when the page is posting back you will have to set the values for the UI controls that you want to update with the values from the 'newFriend' object (I'm assuming that has the changed values).
Found it!
The solution for this issue is to add this code at the end of your submit button click event:
Server.Transfer("currentpage.aspx");
I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.
The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in MyRepeater.Items)
{
MyLabel = (Label)item.FindControl("MyLabel");
}
}
The button is located in the FooterTemplate of the repeater.
<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />
Thanks in advance.
Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)
Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?
THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.
example:
You request the page. On Load Fires. You bind the data to the repeater.
You maniupulate the data in the reapter then click your button
The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
the onclick event is triggered and your code tries to retrieve values that no longer exist.
Make sure your databinding code in your onLoad event is nested within an postback check
if (!Page.IsPostBack)
{
Repeater.DataSource = Datatable;
Repeater.DataBind();
}
I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.
private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
//populate dataTable
if (!IsPostBack)
{
//databind to repeater
}
}
protected void Submit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repeater1.Items)
{
DataRow row = myTable.Rows[item.ItemIndex];
}
}
Ideal? Certainly not but it works.
Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.