I am creating a web application for my company that needs to deal with form processing and database manipulation. The application is implemented using .NET Framework 3.5 using C# and Visual Studio 2008 as the IDE and Microsoft SQL Server 2005 as the database.
Here is my problem:
I have a lot of forms
But my boss only want me to create a single page for the database
processing (easier to extend in the future)
I figured out the only way to solve this problem is by having only 1 .aspx file (that contains everything about the database) and having it invoked as a pop up window everytime a forms need to deal with the database.
Here is another problem of mine:
Due to stateless nature of HTTP, I am unable to process and pass
variable between 2 different windows.
I managed to create certain Javascript functions and have the
variable transferred on display, however it can only pass a variable
that is the primary key in the table. To process other columns in
the table is possible but as the consequence I have to write a very
long inline script in my .aspx page and after it is compiled people
can easily view how to access my company database easily. Hence, I
don't favor this (beside to deal with 1 form, I need to create a
long code already, imagine if i have more than 1000 forms!)
So there are two ways you guys can help me:
Suggest another way other than popping up a new window for my
problem, maybe even advise on how it's implemented.
If you think popping up is the solution, you mind to share some
snippets that can help me figure out the variable passing between
two different windows. I can use some advise especially from some
Javascript expert on this :).
Note: Solution must be workable in ASP.NET Framework 3.5 and tested using IE browser version: 8.
P.S: This is a short explanation about my application flow
Let's say I entered data about a product (it has few properties id, name, price, etc) into the database
Later on somehow I want to edit one or few properties of that product, so I have to launch a form which called "editor.aspx"
Instead of entering the product id (which is the primary key) into the form (and edit the data based on the entered product id) and risking to miscalculately edit the correct data, I provide a small button in the form (let's name it btSearch), that will launch a new popup window which contains the gridview of the database of all product (with selection enabled)
Now I just need to browse through the gridview, select a particular row, it will close the popup and I expect to see few data from that row appeared on my original page (in the textboxes/labels)
I hope my explanation above clears the air, thank you.
I recently wrote something like that: A database handler as aspx file. But i invoked it by using ajax / jquery.
When my aspx file is done, i write something to the response stream, some code, a json string, what ever.
Example:
$.post("yourdatabasehandler.aspx", { name: "John", lastname: "Smith" }, function(data) {
alert("Response from page: " + data);
});
In that example, name and lastname are values that are posted to your site. You can access them like that:
string name = Request.Params["name"]
// Do your database , validation and whatever logic here
Response.Write("Cool dude");
The above javascript will alert "Cool dude" after your databasehandler is done. Inside your javascript you can react to the response how ever you want - For example reload a page.
Hope that helps? Regards
"1. Due to stateless nature of HTTP, I am unable to process and pass
variable between 2 different windows."
You very wrong with this comment to start with, trying MSDN and ASP.Net "How to pass values between ASP.Net Web pages". Passing between Windows only requires a little bit more thought and possibly a little Javascript to refresh a parent windows or cause a postback on a shild window etc.
If you're using a popup window, you can always use QueryStrings to pass a value going to your popup.
window.open("popup_page.aspx?id=" + id + "&name=" + name)
to access it in popup_page.aspx
string sID = Request.QueryString("id");
string sName = Request.QueryString("name");
Update: if you're using IE the this might help you.
function ShowPopup(strMessage)
{
var returnValue= window.showModalDialog("popup_page.aspx");
}
popup_page.aspx
<asp:Button ID="btnReturnValue" runat="server" Text="Proceed" OnClientClick="window.returnValue='some message';window.close();" />
Note: Please note this only works in IE, so I suggest consider using the followings instead:
jQuery
AjaxControlToolkit ModalPopup
I personally suggest the use of jQuery. :)
Thank's for all answers, comments. rates, and feedback. Just now I found a very helpful link here. Basically the answer is based on that particular code. I just need to alter some part.
<script language="javascript">
function GetRowValue(val)
{
window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value = val;
// make sure you change the TextBoxId as respective to your creation
window.close();
}
</script>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<!-- Reserve the code below, as after you configure data source you -->
<!-- will alter this code drastically therefore-->
<!--you have to make sure to paste this code -->
<!-- again inside this Gridview element once you configure your data source -->
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" />
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<!-- This part must be reserved -->
</Columns>
Also remember to specify the connection string and the sql command in the datasource.
The rest just follow that tutorial and copy paste the code entirely.
Related
I have an aspx page of images that, when selected, a popup appears prompting for various information tid bits that I then store the information as data attributes on hidden labels through use of jQuery (i.e. data-id="####", data-difficulty="###", etc.). I acknowledge that this isn't the best way to do it necessarily but I've tried other things (see below) and nothing has worked yet.
I've been attempting, and to no avail, to retrieve the dynamically updated data attributes so the various items can be stored to my local ms sql database. The updating of the attributes works perfectly in that I can view the items being updated properly in Chrome's developer tools. Despite this when I try to pull the same attributes I can see as being updated I'm unable to retrieve the updated values in the code behind and keep getting back the initial values (generally an empty string "").
Here's the implementation on the aspx page:
<asp:Label runat="server" ID="lblSelection" data-id="" data-count="" data-price="" data-difficulty="" CssClass="selected-items" />
and here's the relevant method being called when the "Submit" button is clicked further down on the same page:
protected void SubmitClicked(object sender, EventArgs e)
{
var currentID = lblSelection.Attributes["data-id"];
var currentCount = lblSelection.Attributes["data-count"];
var currentPrice = lblSelection.Attributes["data-price"];
var currentDifficulty = lblSelection.Attributes["data-difficulty"];
if (currentID == null || currentID == "")
{
// stop and throw an informative message to the user
}else{
// keep processing ...
}
}
The trouble is that when I run the project in debug mode and inspect those elements (again, making sure that I can visually see that the attributes are actually updated in the developer tools) they're all the initial value of "". My only guess is that there's some kind of post back issue but I wouldn't think that would happen until my method had been called and fully processed. As a note, I'm populating the images onto the page and updating their attributes already through a sql call based on the id of the item:
<img runat="server" src="" data-id="12345" />
The initial loading all works perfectly so I can clearly set the properties from the code behind fine. For whatever reason though I am unable to pick up the updated attribute values in the code behind after the jQuery updates the label's attributes (following some clicking and whatnot). Any tips or thoughts on this would be greatly appreciated.
What you are trying to do cannot work because:
The value of custom attributes is not posted back to the server (as discussed here).
Even if you set the text of the Label in client code, it would also not be available in the Text property in code-behind. A Label is rendered as a span element in the page, and the content of that type of element is not posted back to the server.
A nice list of properties included in a postback is given in this topic: Which values browser collects as a postback data?
As suggested by mshsayem, you should use HiddenFields. If, for some reason, you want to do it differently, you could use hidden TextBoxes, set their value in client code, and retrieve it with the Text property in code-behind. In other words: HiddenFields are not the only controls for which a value set in client-code can be retrieved in code-behind (but they are the obvious choice if the control is not to be displayed).
I have a project using ASP.NET with a MS SQL DB, using LINQ classes and require change tracking / audit trail for specific fields. Since the project is quite big, adding the audit-trail functionality manually would be cumbersome. Also, tracking ALL changes on the data-layer would not be perfect (since I also have to handle external data, coming from JSON files sent by HTTP POST, and more ...).
My idea is to implement the usual audit-trail functionality (which is discussed and shown in various other questions/tutorials and should not be the issue here) by extending the existing ASP.NET controls (such as asp:TextBox, asp:CheckBox, asp:DropDownList, etc) with a simple property "bool auditTrail", which can be set to TRUE in my .aspx-file and then tracks the changes in code-behind. This functionality would simply speaking just store the origin page, element #ID, old + new value, logged user and timestamp.
Can this be done - more or less easily? To be honest I am quite firm in ASP.NET but not-so-firm when it comes to extending existing classes. Or maybe the whole idea has a big flaw and you can point me to the right direction? I found solutions using NHibernate, but, if possible, I would prefer to avoid external dependencies.
Thanks for your input!
To clarify what I would like, some (pseudo)code. Of course the tagprefix of the controls needs to be set accordingly etc (since I don't think that I can override the asp:WebControl classes themselves ... can I?)
page.aspx
<asp:TextBox runat="server" ID="tb1" audit="true"></asp:TextBox>
<asp:DropDownList runat="server" ID="ddl1" audit="true"></asp:DropDownList>
page.aspx.cs
no further code regarding the 2 controls...
AuditControls.cs
public class TextBox : System.Web.UI.WebControls.TextBox
{
public bool Audit { get; set; }
}
public class DropDownList : System.Web.UI.WebControls.DropDownList
{
public bool Audit { get; set; }
}
So long, that's the raw outline of what I would like to have in my application. What's missing is how I could - in a solution as generic as possible - add the functionality for the audit trail itself. Storing the value when the Control is rendered (/filled with data), and also retrieving the value history of the Control in order to maybe show it in another (popup-) element. Maybe it's even a good idea to load the data in a seperate element:
page.aspx
<asp:TextBox runat="server" ID="tb1" audit="true"></asp:TextBox>
<asp:MyHistoryViewer runat="server" ID="history_tb1" TargetID="tb1" />
AuditControls.cs
public class MyHistoryViewer
{
public string TargetID {get;set;}
// program logic to retrieve the value history of the control with ID=TargetID from DB
// render it as a, let's say HTML table...
}
And finally, in order to store all changed values, some sort of "invisible" ChangeTracker could be sitting on the .aspx page, having his eye on all my Controls with property audit set to true. This Changetracker will fire once the page gets a Postback, checking if the current WebControl field values (be it TextBox/DropDownList/...) is different to the latest value. If not, it writes the data to the DB...?
Thinking out loud - any suggestions or hints, do you have experience with this? Any better way to do things?
Also "Thinking out loud"... I'm thinking that the controls you suggested in your question are server controls. this means that on selecting/clicking they will produce a postback. #konrad_pe can I suggest that perhaps a HttpModule would be a good idea to tackle your problem? since HttpModules are hit for every request you could query the form data for the control that caused the request and take it from there.
I read similar here:
Get which control...
I hope this gives you another point of view and you find it interesting enough to explore. Good luck!
I have html label contol without runat="server"
Does it possible to get inner text from code behind c#?
Label:
<label id="lblClanName">Text Here</label>
Thanks
Every time an ASP.Net page is posted back to the server it is recreated from scratch using the custom code contained in the page (such as calls to a database), the HTTP post/get collections (which include ViewState), any custom data in Application, Cache, Session, static objects, etc.
If the value does not exist in any of those locations, the server doesn't have access to it. A common trick to pass data from the client is to simply use a hidden field. If you want something more elegant, you can use asynchronous AJAX to send/receive data from the server.
Or in this case, you could just add runat="server" to an asp:Label. ViewState will maintain the value between postbacks, though it will not reflect changes made client-side unless (once again) the data is somehow passed back to the server.
Note that ViewState is typically a bad thing because it essentially doubles the size of your data (or more) and (in my opinion) encourages sloppy design.
i don't think you can do it.either you can use js get the lable,and call js method from code behind
Short answer: no.
To access this from your code-behind, you will minimally need to add runat="server" to your label. This will allow you to access it using Page.FindControl(String).
The preferred approach, if you are able to modify the front-end code, would be to use an <asp:Label />. This will allow you easy access by just using the control's ID in the code-behind, specifically its Text property.
Do you want to know how to parse a string value for the inner html, or do you expect your web page do have text written to the label at runtime?
string labelHtml = "<label id="lblClanName">Text Here</label>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(labelHtml);
string innerText = doc.DocumentElement.InnerText;
Why do you need the text between a label, is this for a live web page? This sound like a bad design more than a requirement.
I use .net 4 and c#.
I have a asp.net Page wit an HyperLink.
<asp:HyperLink ID="uxLink" ClientIDMode ="Static" runat="server"></asp:HyperLink>
In my code behind I use the HyperLink ID="uxLink" to change some properties.
In my browser the code generate is:
<a id="uxLink" href="/blog/5/test.aspx">Test link</a>
I would need omit the id from the source code generated in the browser so it should look:
Test link
I tried to play with ClientIdMode but with no succe... Any idea how to do it? Thanks
If you remove ClientIDMode ="Static", the framework will generate a "big unique" ID for the element, but still, there will be an ID.
Having an ID is fundamental if you want to handle the elements in client-side script. The IDs should not be removed.
If you don't want them, just don't use ASP.NET components, use the <a> tag itself.
Update
By the way, supposedly there's a way to do what you want in ASP.NET 4. It's described here: http://www.codeproject.com/Tips/208323/Disable-ClientID-for-any-control-on-ASP-NET-Page?display=Print
I haven't tried it, I'm just pointing you into a direction. You should be aware of the implications of not having a ClientID, though. Specially for Postback controls. Read the article.
The easiest solution is to change the ID propriety for the control in code behind before rendering the page to NULL.
in my example:
myLink.ID = null;
This would generate no ID at the Browser resulting in:
Test link
I find this useful trick on this website:
http://www.dotnetperls.com/remove-id
Kentico is a C# / Asp.NET Content Management System that we use and I'm trying to implement authorize.net SIM integration (redirecting the user to the authorize.net servers to make purchase through a form post). Kentico uses master pages so it's proving to be a beast. First issue was getting the form to even post to the authorize.net Servers. I was able to do this using the following.
<script type="text/javascript">
theForm.action = "https://test.authorize.net/gateway/transact.dll";
</script>
Easy Enough (theForm == the master page form), now the issue lies in the fact that I originally was using code behind to populate the hidden input fields and it changes all of the names of these input fields. This makes it impossible for authorize.net to know what you are doing.
Has anyone done any integration like this before? And if so, what is the most appropriate way to solving this problem?
I have a few ideas but they all involve what I consider extremely dirty methods for getting it to work.
you will need to follow these steps to take the names of your input fields under your control:
1) Use your code to set the payment gateway URL
2) Place ASP.NET Literal control on ASPX page, something like:
<asp:Literal runat="server" ID="myFields" />
3) Go to code behind and initialize the literal with HTML code of all your input fields. For each input field set its custom identifier. e.g:
myFields.Text += "<input type="text" id="carnumber" name="cardnumber" />"; ....
After the button is clicked, user is redirected to the payment gateway URL where the POSTed data from your input fields are available under the required identifiers. I hope you will find it helpful.