Dynamically add control disappear after postback - c#

I have an aspx page with the following code:
<asp:Table ID="JsonContent" runat="server">
</asp:Table>
In my behind code at Page_Load i add Items to Table.
Table consist two rows:
Row num 0 is Cell that contain TextBox
Row num 1 is Cell that Contain TextBox
Additionally i have a button save, when the user click the button i need to go over all table row and write the data at Text Prop to File.
The problem is that when i click the Button Save the event page_load raise and all text box control is not already exist so i can no write the edited data to file.
I read about similar post with this issue, but could not get a solution.
Thank!

First of all, you should learn about asp.net pages web cycle : https://msdn.microsoft.com/en-us/library/ms178472.aspx
And i would say that #TimSchmelter has the perfect solution for you in the comments :
"You should (re-)create all dynamically added controls in Page_Init in every postback because all objects are disposed when html is sent to the client. But even if you recreate them there they will persist the user-input if you assign the same ID as before"
This means you have to use the function page_init (wich is similar to page_load but not automaticly generated, you have to write it manualy)
I did this once too, and I confime that Tim's solution will work.
Just make sure that your page_init() fucntion is reached.

Related

Page can only have one server-side form tag

I'm facing a problem with one of my pages. I have a FormView control on the page that I use to enter new rows into a database table, and a GridView control on the same page to update/delete rows from the same database table.
The FormView control has validators on it to validate any input being thrown at the database, and it seems to interfere with the GridView when I try to edit a row. When I try to save the edit, the validation control on the FormView gets fired and an error comes up because the textbox input is blank, so the GridView cannot save the modified table data.
Perhaps a visible example will help:
I had an idea where I encase these controls in different forms, hoping that the submit of one form won't fire anything in the other but then I got an error saying I can't have more than one form on the page with runat="server", which as far as I can tell is required.
How can I get around this?
Thanks.
You can only have one Form.
Use different ValidationGroups for your FormView and GridView instead.
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
Yes, this is a known limitation (feature?) of webforms. You can only have 1 form with a runat="server" tag.
In your scenario you can use ValidationGroup names to group validation together so only "like" validation is executed when a control of the same validation is triggered.
What I did is I had two forms coded as postback to the page and parsed out the postback variables in Page.Load myself. Weird? Yes. Works? Yes.

GridView is not shown in the content page

I have a gridview in my Master page.I have taken an item template inside it. I have given some text to the controls initially.
In the design view of the content page gridview is visible but while running the page it's not showing gridview.
when i deleted gridview and put some simple text in place of it ,it's working .
i tried setting z- index and some peculiar things but nothing seems to work.Please help me out
There may be number of causes - you might not bind it or datasource is empty or its visibility state is hidden or you have placed GridView inside the ContantPlaceHolder in master page.
Confirm the below cases to show the gridview, listed below:
a. Must have to put "DataSource" and then call DataBind() method, like
gridViewToShow.DataSource = dataSourceName; // may be table or custom data source
gridViewToShow.DataBind();
b. Data must exists in the "DataSource"
c. Locate data field( <%# Eval("DataFieldName") %> ) to Gridview column, like in aspx code
c. Make it visible (or don't make invisible, by default it is true)
d. If you use CssClass, then make sure there has "display:block;" or has no "display:none;"
Hope these would be very helpful.
Try setting the EmptyDataText to something and see if it shows the text. If it does, it means it is not DataBound to anything, or the DataSource has no rows. Good luck!

Issue with page life cycle (asp.net)

I have got this event in my page aspx.cs:
public void deleteBtn_Click(object sender, CommandEventArgs e)
{
UsefulStaticMethods.DeleteComment(int.Parse(e.CommandName));
}
I am trying to delete a comment from the page. The deletion is successful. However, the website interface doesnt update itself after that event happens.
My Page Load is responsible to drawing all the comments on the page with a dynamic button (delete comment).
I know when the delete button fires, the page Load fires before.. and thats a bit is a problem..cause the page load recreates the page interface, while the deleteBtn_click deletes the comment, and I want to update the interface straight away... "Refresh" the page without the comment that was deleted..
If i execute a function to draw the whole table again, it will draw another comment list along with the comment list drawn at the page Load event.
I cant not refuse not to draw the comment list at page_load, cause i need everything recreated at postback time (including the dynamically created button).-cant use !Ispostback
The question is how can i achieve this/overcome the issue?
Typically, if your using data-binding then you can just re-bind the control in question. Perhaps, you should modify your function that draws the comment list to clear the existing list (possible by removing rows from table control or clearing control collection from container panel or placeholder (you can introduce a placeholder control just for clearing purpose)).
Yet another hack to refresh the page is to restart the page-life cycle by doing Server.Transfer to the same page. Generally, I wouldn't recommend this approach unless page code structure is very complicated and refreshing the data would take many lines of code.
You need to rebind control. Suppose your button is in Grid than you need to Rebind() grid. If not than there is one more way. Put Contents in Update panel and set Update panel Trigger with Delete button. SO when Delete button is clicked that update panel cause Update.
Use data list or repeater server control to display comments and than bind the server control again after deletion. Use !Ispostback on Page_Load.

C#/ASP.NET/JS - onclick event shows row, postback hides row

rbnResubmission.Items.FindByValue("Yes").Attributes.Add("onclick", "getCheckedRadioFieldResubmission(this)");
rbnResubmission.Items.FindByValue("No").Attributes.Add("onclick", "getCheckedRadioFieldResubmission(this)");
So I have these click events for showing rows in a table - that part works fine.
Here's an example of what the code does (I'm not showing the "No" option)
function getCheckedRadioFieldResubmission(radio){
if(radio.value == "Yes"){
document.getElementById('<%=ApprovingInstituteRow.ClientID%>').style.display ="block";
document.getElementById('<%=ApprovalNumberRow.ClientID%>').style.display ="block";
document.getElementById('<%=IRBApprovalRow.ClientID%>').style.display ="block";
document.getElementById('<%=ExpectedDateRow.ClientID%>').style.display ="none";
}
}
The form needs to go through validation, and if there's any problems, because the event to show these rows only happens from "onclick" - they will disappear upon postback. what can I change to make them appear permanently?
I'm guessing you're databinding the radio button list during onLoad. You have to rebind it on postback.
You'll probably want to add the same functionality of your getCheckedRadioFieldResubmission function into the onload event of the html <body> tag. However, since the this object will refer to the body element and not the radio, you'll need to put something of this sort in there to find the radio:
var radio = document.getElementById('radio_id');
This will work if your radio list has a maintained state. As in, it keeps it's state after Post Back.

Formview Being Cleared

My problem is that all the textbox's my formview are getting cleared when I hit the submit button.
I currently have a page with a small section that has an update panel around it. This small section adds an address to my databse. To the left of this form there is a gridview that is tied into the formview. So if i click on an item in the gridview its contents fill the address section(formview) with the correct data.
When I hit add the data gets validated in the c# code behind, and if all the info is correct the address is inserted. If there is an error the entire form is cleared and the error message(label) is displayed.
I have already done this many times in other pages, but none have had the gridview tied to the formview, and they have all worked. I tried removing the gridview and the form still erases itself.
Is there some reason that .net thinks it should be clearing the form? When in other cases it decides it won't? If so what are these cases, or what general tips should I try to solve this?
in the page_load are you using if(!Page.IsPostback) { ... } so if it's a postback nothing gets re-bound?
is the ViewState enabled?
Yup many hours later I found that a single panel that was wrapped around the section had a EnableViewState="false" added to it. Sad part is that I know I didn't add that because I didn't even know what it was until craig here mentioned it. Visual Studio must have added it sometime.

Categories