I have a very large finance table, which will be repeated 4 times on a page, across many different pages. I don't like the idea of just copying the table across pages. Am I correct in thinking that I can create a repeater which repeats it? But also at the same time amend figures in the backend?
I have looked around, but wasn't to sure whether the amending of figures was possible as well as more efficient?
<div class="span3 finance-boxes">
<table class="table table-bordered table-hover finance-table">
<tr><td>Title</td></tr>
<tr><td class="fig">Figure</td></tr>
<tr><td>Title</td></tr>
<tr><td class="fig">Figure</td></tr>
<tr><td>Launch Finance Details</td></tr>
</table>
</div>
As I understand from your explanation you need to repeat the same piece of HTML on many places with only difference of some values of some cells. So I suggest you create a new ASCX control.
Webusercontrol is very similar to asp.net page, but has .ascx extension and can be inserted onto page or other control. So to create it use Add -> New Item -> Web User Control in VisualStudio.
The web user control will have markup file .ascx and code-behind .ascx.cs file.
Place your html on markup file:
<div class="span3 finance-boxes">
<table class="table table-bordered table-hover finance-table">
<tr><td>Title</td></tr>
<tr><td class="fig" runat="server" ID="cell1"></td></tr>
<tr><td>Title</td></tr>
<tr><td class="fig" runat="server" ID="cell2"></td></tr>
<tr><td>Launch Finance Details</td></tr>
</table>
</div>
Now in the code-behind you can access your cells by cell1 and cell2 and you can change their inner html by cell1.InnerHtml property.
You can create public properties in your code behind file to have access to these cells from the page:
public string Cell1Text
{
get
{
return cell1.InnterHtml;
}
set
{
cell1.InnterHtml = value;
}
}
Then you can place your web user control on your page.
You can read more about this here:
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx
Finally you will be able to access and modify your control's properties by this piece of code:
myUserControlName.Cell1Text = "NEW TEXT";
Related
I have a HTML page which is dynamically generating by server. The application has an IDE to generate and design pages then deploy the server. The server displaying this pages in an iframe. We can use all c# methods as well as Page_Load and Page_PreRender events in pages. But I can't modify source code of the asp.net page (I mean can't add runat="server").
What I want to do, finding a html tag by css class (#form1 > span) before pre-render then add a new css property in code behind.
<form id="form1" action="DocumentViewer.aspx" method="post" autocomplete="off">
<span>
<table>
<tr>
<td></td>
</tr>
</table>
</span>
Without runat="server" you cannot access the control in code behind. Best way to do it is to inject the jquery script from code behind to do the same work.
Please take a look at this answer.
I have 2 page.
When double click row data in first page will be go to second page.
second page is for update purpose.
when successful update in second page and i want back to first page, the data show not updated in first page but when refresh the data will show updated record.
How can i get the updated record show in first page without click refresh?
below is first page aspx code:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr style="cursor: hand;" onmouseover="this.style.backgroundColor='#E6F0FF'"
onmouseout="this.style.backgroundColor='#FFFFFF'"
onclick="javascript:window.location.href='V_Updated.aspx?mail=<%#Eval("mail") %>'">
<td class="line_table_td" style="text-align: center;"><%#Eval(" mail ")%> </td>
<td class="line_table_td" style="text-align: center;"><%#Eval(" remarks ")%> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
second page aspx button back code:
<td class="btn_bg" onclick = "history.back();">Back</td>
Better to use AJAX for updating data, if it is suitable for your project. This way you do not have to use two different pages.
http://www.webblogsforyou.com/ajax-introduction-how-to-use-ajax-in-asp-net-with-example/
Thanks!
Don't go back using the browser history (e.g. using history.back()). Different browsers handle caching differently, so it's difficult to control if your page would get refreshed. Provide a proper link to the previous page instead.
Simply put a link in second page like below :
<a href="firstpage.aspx" > Back </a>
I have the following table in an ascx user controll:
<tr runat="server" id="rowChangeSerNo">
<td colspan="2">
<table id="tblChangeSerNo" runat="server">
</table>
</td>
</tr>
<tr id="row" runat="server">
<td>
<asp:Button ID="btChangeSerNo" runat="server" Text="Update" OnClick="btChangeSerNo_onClick" />
</td>
</tr>
I create the tblChangeSerNo dynamically with text boxes prefilled with the current values in the db. The idea of the control is to allow the user to update the values of the DB with new values. The problem is that when the btChangeSerNo_onClick method is called:
The table is not rendered, since I do it on pre-render
Even if I rendered the table on Page_Load I could not access updated values of the user because they are lost.
How can I solve this problem?
The best practice way will be to use Grid control instead.
If you prefer to stick with your own code, follow those steps:
Store all the text boxes in global fields, e.g. public List<TextBox> m_tableTextboxes = new List<TextBox>(); and when creating add to that list.
Have the code creating the dynamic controls in the Page_Load but execute it only when not is PostBack: if (!Page.IsPostBack) { ... }
In the button click event, read the values from your global field.
Done something similar in the past, so the concept should work.
I have an asp.net page where I have the below markup. Basically this markup is generated from codebehind by reading records from a table and looping through them. For each record in table, there will be a div block.
Basically this form is to read/show settings for a user. The settings entries are stored in a table.
<div id='divContainer' runat='server'>
<div id='div1' runat='server'>
<table>
<tr>
<th>Name</th>
<td><input type='text' id='txtName1' value='something' /></td>
</tr>
</table>
</div>
<div id='div2' runat='server'>
<table>
<tr>
<th>Domain name</th>
<td><input type='text' id='txtName2' value='something' /></td>
</tr>
</table>
</div>
<div id='div3' runat='server'>
<table>
<tr>
<th>URL</th>
<td><input type='text' id='txtName3' value='something' /></td>
</tr>
</table>
</div>
<div id='div4' runat='server'>
<table>
<tr>
<th>Some other value is enabled ?</th>
<td><input type='checkbox' id='chk4' /></td>
</tr>
</table>
</div>
</div>
The id's of each input element will be unique. Now in codebehind I want to read the values of each input element to save the changes user made. How can I read the elements here? Since the mark up is generated in codebehind as a string and appended the the INNER HTML of the external div, I can't read values like we do for a control which we drag and drop in the IDE.
If these are being sent back to the page in a standard HTTP POST then you can read the values in the Request.Form NameValueCollection.
Essentially, all of the server controls that become form elements get translated into standard HTML form elements just as you have there, albeit with more markup generated by .NET to help it identify them. Then it automatically maps their values back to the server controls for you on the postback, but the values themselves are still just in a standard HTTP POST, which you can access manually.
(This is also a common method used when posting a form from one ASP .NET application to another.)
If you want to grab your values for the generated controls you have to do 2 things.
Generate the input controls with a runat='server' tag for each control (otherwise they will not be included in the Request.Forms collection.) This is probably the step your missing.
<input type='text' id='txtName1' runat='server' value='something' />
Grab your values from the Request.Form collection on postback
string txtValue1 = Request.Form["txtName1"];
It really should be that easy. I tested this against your code using a DIV as the container and a simple javascript to inject the control string into the innerHTML. If your getting any issues you may have to debug and see if the dynamic control ID has changed due to inserting them into naming container or something.
So the brunt of the story is that when you dynamically add a control after Page_Init then POSTBACK values can not be inserted back into those controls.
CF: http://www.15seconds.com/issue/020102.htm and http://msdn.microsoft.com/en-us/library/ms178472.aspx
Some of the other answers here suggest "oh, add a runat=server to the control" but when you create it in the codebehind, and not in the Page_Init, then that makes ZERO difference.
Let me know if that's not how you're creating the controls or if that's not how you're using them and I'll revise this answer on more details. It really all boils down to how you're trying to access the values.
Generally, you'd place those input controls you're creating dynamically (in this case, a TextBox) inside something like a panel control (the container). Then after the user has posted their data, you'd loop that container panel.Controls collection and retrieve each TextBox text.
Be aware that some caveats apply when working with dynamically created controls because ASP is of stateless nature.
This page shows how to implement this:
Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes
I didn't test it but I can suggest that:
Add your dynamic controls with runat="server" tag inside another controls with runat="server"(such as panel control). Then you can access them like this:
Textbox t = (Textbox)panel1.controls.findControl("dynamicControlId");
According to the new 4.0 framework overview, one should be able to add the attribute RenderOuterTable="false" to a control that supports the attribute and see CSS friendly code be spit out - in other words no HTML tables.
To test this, I threw a login control into a basic fresh webpage with the following code:
<asp:Login ID="Login1" runat="server" RenderOuterTable="false"></asp:Login>
The result? Crappy HTML table output, which supposedly doesn't happen with this attribute set to false. Here is the output:
<table cellpadding="0">
<tr>
<td align="center" colspan="2">Log In</td>
</tr>
<tr>
<td align="right"><label for="MainContent_Login1_UserName">User Name:</label></td>
<td>
<input name="ctl00$MainContent$Login1$UserName" type="text" id="MainContent_Login1_UserName" />
<span id="MainContent_Login1_UserNameRequired" title="User Name is required." style="visibility:hidden;">*</span>
</td>...
Hopefully you get the point. How can these controls be stopped from outputting tables? This is super annoying.
Convert the Login control to a template. It will give you full control of the layout without a table in sight (including the outer table which previously got generated even if you used the template option).
It doesn't say that it will get rid of all tables, just that it will get rid of the extra outer table that was used to apply styles. Try getting rid of the property and note the extra table that wraps the one you quoted above.