ASP.NET listbox selectedValue in PageLoad - c#

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
}
}
}

Related

Gridview Textbox doesnot maintain Enable/Disable state but maintains Text during postbacks

I have a textbox inside a gridview.
It was initially set to Disable state.
I am enabling and disabling it in javascript.
But the problem arises during the postbacks.The grid view was able to persist the textbox data but not the Enable/Disable values.During every postback it comes back disabled even when it is enabled before the postback.
I have explored around the use of hidden field to maintain the state in these scenarious but I wish there is something less tiresome...
Can some one enlighten me on this issue...
Seeing the code could really help but generally take off the Enable="False" from the HTML then in the Page_Load event add something like:
Protected void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
tetxBox.enabled = false;
}
}

how to see DB changes without refreshing page in ASP.NET C#

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");

Checkbox and page reload

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.
}
}

Gridview Rowcommand event firing but show hide controls including Gridview not happening

Here is what I am trying to do in two simple steps:
1) New Row (trNewPost) which has table inside and controls in it to add new post or to update existing post.
Default Visible=false;
2) Add Button to make above row visible = true;
3) trMyPosts has Gridview in it and displays all the posts.
Default visible = true.
When user click on editing any row of the gridview (RowCommand event) I just want to hide this grid (trMyPosts) and show trNewPost.
That's all. events firing, but nothing happening.
I think you've got viewstate problem.
One of the things you can do is this :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// do things here
}
}
Because whenever anything happens, the page posts back. By encapsulating your Page_Load with ! Page.IsPostBack, you prevent those things from happening over and over again.
Now, if your variable is a global variable, you will have this same problem. Consider instead using a Session variable.
Also, I just wanted to show you this piece of code just in case :
protected void HideShowClicked(object sender, EventArgs e)
{
// toggle the visibility of the control
// (that is, if visible then hide, if hidden then show)
myControl.Visible = ! myControl.Visible;
}

Accessing RepeaterItem Controls in an EventHandler

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.

Categories

Resources