|
Hallo
I have about 100 asp.net forms. I have a master page and many pages that refer to the master. The .aspx pages differ, but the .aspx.vb codebehind of all is essentially identical. The .aspx_vb has some parameter declarations and constants at the top, but the remainder of the .aspx_vb is identical generalised code - handling events like page_init, page_load etc.
Obviously this is inefficient to maintain.
Can someone offer advice of what techniques I should investigate to be able to centralise the common code into one code base and maintain it once.
I have written extensions to existing controls previously - for example I have written extensions that inherit GridView and DropDownList. I also use User Controls extensively.
Can someone point me to examples or articles to assist me solve this problem?
thanks, Grant
|
|
|
|
|
You can use Classes that are essential in object-oriented programming. These will have functions that you can call to in any of your pages. I found numerous tutorials by searching writing and using classes in aspx[^]
|
|
|
|
|
|
|
The "easiest" way (IMO) to get some reuse is to identify methods that can be made static; with ease; i.e. don't have dependencies that can't be easily made into "parms".
Those can be put in a static class / dll and shared that way. I have a lot of "builders" and "adapters" in that category.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Thanks Gerry. Helpful to know that there is no silver bullet. Based on this advice I'm isolating some functions out, and also building a class that inherits from the base.
|
|
|
|
|
Hi all,
We are currently having problem with data exported to excel.
When we review the exported file, some show values with exponential format.
To try to work around that, I have the code below with stringbuilder to fix the exponential format issue.
Public Sub GetExcel(ByVal dt As DataTable)
Dim fileName As String = "file" & DateTime.Now.ToString("MMddyyyy") & ".xls"
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.ContentType = "application/vnd.ms-excel"
Dim stringWriter As StringWriter = New StringWriter()
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
Dim dtExportExcel As DataGrid = New DataGrid()
dtExportExcel.DataSource = dt
dtExportExcel.DataBind()
dtExportExcel.RenderControl(htmlWrite)
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
sb.Append("<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:xlExcel8"" xmlns=""http://www.w3.org/TR/REC-html40""> <head><style> table { mso-number-format:'0'; } </style></head> <body></html>")
sb.Append(stringWriter & "</body></html>")
Response.Write(sb.ToString())
Response.[End]()
End Sub
When I run the code, I get an error on this line:
sb.Append(stringWriter & "</body></html>")
The error says, Operator '&' is s not defined for types 'StringWriter' and 'String'
Any ideas what this means?
Thanks in advance
modified 5-Jul-23 21:43pm.
|
|
|
|
|
It means you cannot add a simple string to a StringWriter object, as it makes no sense. Furthermore you cannot append a StringWriter object to a StringBuilder . You can only append strings (or the string values of objects) to a StringBuilder. See StringWriter Constructor (System.IO) | Microsoft Learn[^] for the correct way to combine the two.
|
|
|
|
|
Ok, thank you very much for your response.
So, this would have been the correct way?
sb.Append("</body></html>")
|
|
|
|
|
Yes, You can only add strings, or "things" that can be expressed as strings.
|
|
|
|
|
Thank you. I saw a similar code but written in C# that has exact same code and users say it worked for them.
Thank you for your help. I will try this and hopefully, it works.
|
|
|
|
|
Sorry about the other message. But looking at your code again I do not see why the StringWriter , or the HtmlTextWriter , are there, as apart from the following two lines:
Dim stringWriter As StringWriter = New StringWriter()
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWriter)
You never refer to either object (other than in the line with the error).
|
|
|
|
|
Right, you are correct.
I have actually removed them from there but added them some place else like:
dtExportExcel.RenderControl(htmlWrite)
Response.Write(stringWriter.ToString())
|
|
|
|
|
As a simple fix:
Dim sb As New System.Text.StringBuilder()
sb.Append("<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:xlExcel8"" xmlns=""http://www.w3.org/TR/REC-html40""> <head><style> table { mso-number-format:'0'; } </style></head> <body>")
Dim stringWriter As New StringWriter(sb)
Dim htmlWrite As New HtmlTextWriter(stringWriter)
Dim dtExportExcel As New DataGrid()
dtExportExcel.DataSource = dt
dtExportExcel.DataBind()
dtExportExcel.RenderControl(htmlWrite)
sb.Append("</body></html>")
Response.Write(sb.ToString())
Response.[End]()
However, note that you are not "exporting an Excel file"; you are rendering HTML content, but lying to the browser and claiming that it's an Excel file. Excel will display a warning message, and then do its best to import that HTML into a new spreadsheet, but you'll get extremely limited control over the results.
If you actually want to export an Excel file, with precise control over the formatting and layout, then use a library designed to do that - for example, ClosedXML[^] or the Open XML SDK[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
WOW, knowledge is power.
Thank you very much sir and great to hear from you again
With your advise, I was able to successfully use ClosedXML and it is working great.
I still have this solution you provided here as a back up.
Thank you again.
|
|
|
|
|
I have a new Blazor app with two Pages/Blazor components: index.razor and config.razor. The config.razor page lists the database objects that can be edited, and clicking on an object item in the list calls an async Task called ShowDetails that retrieves the details of the selected object to be displayed in an EditForm on the page.
However, after retrieving the object data Blazor returns to index.razor. There is no code that navigates away from the component. No errors are caught. Is this normal behavior? Is there something I can add to stay on the config.razor page?
There are no solutions, only trade-offs. - Thomas Sowell
A day can really slip by when you're deliberately avoiding what you're supposed to do. - Calvin (Bill Watterson, Calvin & Hobbes)
|
|
|
|
|
I found the answer and solution elsewhere, posted below in case this is useful to anyone else.
The object in the second blazor component being clicked on was an HTML anchor element:
<a href="#" class="nav-link btn-link" @onclick="(()=>ShowDetails(app.Id))">
Because an anchor is a navigation element, it has a default action which is to navigate somewhere, in this case to "#" which means to stay on the current page. Blazor is a single-page application so navigating to itself reloads the default "page" which is Index.razor.
To stop that default action the solution is to add the @onclick:preventDefault directive to the anchor element:
<a href="#" class="nav-link btn-link" @onclick="(()=>ShowDetails(app.Id))" @onclick:preventDefault>
Of course, another solution to consider is using a non-navigation HTML element such as a button.
There are no solutions, only trade-offs. - Thomas Sowell
A day can really slip by when you're deliberately avoiding what you're supposed to do. - Calvin (Bill Watterson, Calvin & Hobbes)
|
|
|
|
|
I have TestDTO.cs file inside this file, i have 5 field added as string and serialized that field. then one method created that method getting the called this DTO values. i have now new requirement is added more three fieled in that TestDTO.cs. But now if i add the fields in my class file. i am getting getting error as below:
<pre>Error in line 1 position 276. 'Element' 'additionalLimitAmountField' from namespace 'http://schemas.datacontract.org/2004/07/ACE.Claims.HFC.ACC.Types.Policy' is not expected. Expecting element '_x003C_ClassTest_x003E_k__BackingField'.</pre>
can someone facing this type of issue in the code. so anyone can provide help, tell me how to get one.
|
|
|
|
|
As a guess.
You are attempting to deserialize objects that are completely mismatched. Has nothing to do with your changes. You could test that by removing ONLY the new fields and see if the error remains.
But if not then you have stored serialized data. Or at least a serialized form that is coming with the old form.
You defined your new form such that the new elements are required. If they are not explicitly optional then they are required. So the receiver is trying to read it and the required fields are missing.
There is an annotation form for the DTO that allows you to specify that the forms are optional. Google for that.
|
|
|
|
|
Hi, I'm having a hard time finding a simple enough (ELI5) direct answer to on here and the interwebz
If I have the following html fragment on a razor page. How does the clicked button bind to the viewmodel Gender property (transfer data to/from viewmodel property and radiobutton input element)? If I understand it correctly, this binding happens server-side after a POST. This fragment below would have been inside a form, so ALL (male, female in this case) the radio button input types will get sent together with the hidden element. The expression parameters is what tells server-side to associate the radio buttons to the viewmodel property. Also, doesn't the expression already tell what to bind the radiobutton to (ie the hiddenfor is not necessary)?
<pre lang="Razor">
@model Student
<div>
@Html.RadioButtonFor(m => m.Gender,"Male")
@Html.RadioButtonFor(m => m.Gender,"Female")
@Html.HiddenFor(model => model.Gender)
</div>
|
|
|
|
|
RadioButtonFor renders an <input type="radio"> with the name set to the property name, and the value set to the specified value. If the model property matches the specified value, then the radio button will be checked .
<input type="radio"> - HTML: HyperText Markup Language | MDN[^]
When the form is submitted, the name and value of the checked radio button (if any) will be sent to the server, and the model binder will use it to populate the property.
Adding the extra HiddenFor for the same property should not be necessary.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Been some years now since I worked with .Net Core 3, been busy with PHP 8, and was surprised at the new stuff in .Net Core 6, and it's insatiable appetite to point out null conditions, so I'm playing along with it for now. But I just can't wrap my head around this one, and I'm starring at it like a deer in the headlights. So I asked VS 2022 for suggestions, and it suggested converting my old code to these new statements. I read the documentation .. Resolve nullable warnings | Microsoft Learn and I didn't see any anything that sort of matched what I'm doing here. I tried ...
Right hand side ?? "_database maybe null" but it said that it can't be applied to operands of type IMongoCollection<account> and strings.
Perhaps someone else has run into this and can give me a pointer or a solid. But I'll keep researching this to see if I can find something.
My old code example from .Net Core 3
<br />
public IMongoCollection<ACCOUNTS> Accounts
{
get { return _database.GetCollection<ACCOUNTS>("Accounts"); }
}<br />
My new code that VS 2022 Suggested
internal class MongoDbContext : IDisposable
{
private IMongoDatabase? _database;
<pre>
public void MongoDBContext()
{
var rootObject = AppSettingsJson.GetSettings();
var dbConnection = rootObject?.Settings?.DbConnection;
var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
var client = new MongoClient(connString);
_database = client.GetDatabase(dbConnection?.Database);
}
public IMongoCollection<ACCOUNTS> Accounts => _database.GetCollection<ACCOUNTS>("Accounts");
public IMongoCollection<SHIPPERS> Shippers => _database.GetCollection<SHIPPERS>("Shippers");
public IMongoCollection<CONTACTS> Contacts => _database.GetCollection<CONTACTS>("Contacts");
public IMongoCollection<PALLETS> Pallets => _database.GetCollection<PALLETS>("Pallets");
public IMongoCollection<CUSTOMERS> Customers => _database.GetCollection<CUSTOMERS>("Customers");
public IMongoCollection<BILLOFLADING> BillOfLading => _database.GetCollection<BILLOFLADING>("BillOfLading");
public IMongoCollection<SHIPPINGADDRESSES> ShippingAddresses => _database.GetCollection<SHIPPINGADDRESSES>("ShippingAddresses");
public IMongoCollection<countries> Countries => _database.GetCollection<countries>("Countries");
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
On the internet, I was looking at another persons code, and noticed he used _database as a property, then I combined that with the help on Resolve nullable warnings | Microsoft Learn, to satisfy cSharp, or the compiler or both. Not sure if it runs yet, haven't gotten that far. But changing the type offered documented solutions for this. It's gonna be a hard week learning this new stuff.
namespace SimpleBol.Context.MongoDb
{
internal class MongoDbContext : IDisposable
{
private IMongoDatabase _database { get; set; } = null!;
private MongoClient _client { get; set; } = null!;
public void MongoDBContext()
{
var rootObject = AppSettingsJson.GetSettings();
var dbConnection = rootObject?.Settings?.DbConnection;
var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
_client = new MongoClient(connString);
_database = _client.GetDatabase(dbConnection?.Database);
}
public IMongoCollection<ACCOUNTS> Accounts => _database.GetCollection<ACCOUNTS>("Accounts");
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Why use private properties and not fields?
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
I'm willing to learn, what would you have done with fields?
I used the private property because that's what MongoDB sort of suggested in some new sample code to get started.
In 2020, I was just getting started with .Net Core 3 and cSharp, when I picked up a PHP 8 project, and haven't written a line of cSharp in almost 3 years now, so I'm having to sort of start all over again with this project.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|