Asp.net dynamic controls from l2e - asp.net

G'day, I have an aspx page that has its html stored in a sql server 2008 table which uses linq to entities to retrieve & display in a placeholder on the page. This all displays correctly except for an asp button that will not display. Viewing the source of the page shows that the button is there but it is not on the page.
Page = Page.Replace("!LOGINBUTTON!", "< asp:Button id='login' text='Login' runat='server' />")
How do I get it to display?
Thanks.

Whatever code you are replacing with !LOGINBUTTON! is .ASPX code. It is not a pure HTML to display button.
When ASP.net actually render page asp:Button Converted with HTML <Input Type="Button" ..... with appropriate Javascript functions(for postback).
In this case if you want to see button with its postback event you need to write
Page = Page.Replace("!LOGINBUTTON!", "< intput type='Button' id='login' text='Login'/>")
I hope it will work.

Related

using classic asp code inside asp.net?

I have a application that running both classic asp and asp net. inside the classic asp page, I have a combo box and depends on the selection of the combo box I need to do something inside my asp.net page. For instance, if inside my classic asp page, I have a combo box and inside the combo box; book is selected than when I enter a price for book as a zero inside my asp.net page I supposed to get an alert. Is there any way to do that?
asp.net code
if (Convert.ToDecimal(Values["myBookPrice"]) == 0)
{
//You cannot use 0 price for books!
}
Let say that you have some input control on any page, html, asp what ever, with some parametre that you wish to pass to an asp.net page
if the input control have a name attribute like
<form method="post" action="thenewpage.aspx">
<input name="nameOfInput" type="text" id="idofinput" />
<input type="submit" name="btnGo" value="Create Box" id="btnGo" />
</form>
and you have a button that make post back to an asp.net page, then you get that parametre on asp.net side using the Request.Form and the name of the input control as:
Request.Form["nameOfInput"]
If you make a call the page with url parameter, you need to use some kind of javascript to dynamically create the URL string for the call.
The issue that you have here, is that the asp.net page will hit the validation of the page security because is not going to find the page validation informations he needed to secure the page.

UpdatePanel refreshing with <input type="image"> but not type="button

I have an UpdatePanel control in my ASP.NET application (actually several, but that's not the concern).
<asp:UpdatePanel ID="UpdatePanelDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
...More code here
Inside the panel, there exists an asp:Table object:
<asp:Table ID="tblGIFs" runat="server"></asp:table>
This table gets programmatically generated via some code from codebehind:
TableRow htr = new TableRow();
TableCell htc = new TableCell();
...<snip>
tblGIFs.Rows.Add(htr)
And that's all fine, except in one of the cells I am trying to add a button (with an image):
htc3.Text = "<input ID=\""+ii.Value+"\" OnClick = \"btnGIFSearch(this)\" type=\"image\" value=\"/Image/Fiu.png\" />";
When this image is clicked, the onclick fires fine, but the entire UpdatePanel gets refreshed (and consequentually, the asp:Table disappears).
If I use a type="button", it works completely fine and there's no issue with refreshing/losing the content in the table.
I've run the page with breakpoints on every location that I'm touching the table from codebehind to no avail, there's really nothing more that I can see that could cause the UpdatePanel to be cleared programtically.
Anyone seen similar behavior with these tags/controls and any idea of a resolution? A workaround is obviously using type=button and just styling the button, but I rather not do that if I can get type=image behaving correctly.
An input type "image" is like a submit button. It's submitting the form, because that's what they do. If your code-behind is not rebuilding the table in the case of a postback that you don't expect, the table won't be rebuilt, and hence disappear (keep in mind whenever you build controls dynamically, you must do so every request). If you don't want the button to postback, have your OnClick handler return false to cancel the post. If you DO want the button to postback, then you have a different problem, probably related to using dynamic controls incorrectly.
Solved with dynamically created controls.

userControl repeated with Form tag disappearing

I have a Contact userControl that have "save contact" as submit button and fields inside form tag we repeat this userControl with Code 20 times in one page
My problem is the Form Tag in the first userControl is hiding somehow --- i checked the userControl with developer Tool IE9 , firebug Firefox7 and the Form is not appearing in the first userControl and its appearing with the rest 19 Controls
I tried to View Source and take html copy in new html file in VS -- i found form is exist
i dont know if iam clear enough but please advice if you need more info
If you're running a form tag at the server, inside a user control, and then displaying this user control more than once on the page, then you will have multiple form tags running at server on one page, which is not allowed by ASP.NET
Take the form tag outside of the user control like:
<form runat="server">
<uc:Contact />
<uc:Contact />
<uc:Contact />
</form>

ASP.Net PostBack & Button Internal

i worked for long time with windows apps using .net. for last 2 year i am working with asp.net. we often work with asp.net button control. when we click on button then postback occur and right server side event called by asp.net engine. i how link & image button causes postback when user click on those button.
when we work with link & image button then __doPostback js function is called and that causes form submit to server and request the same page and asp.net engine detect which control causes the postback from the hidden input control called eventTarget and invoke right event handler for that control.
but i dont know when we work with asp.net button control like
<asp:Button ID="SampleButton" runat="server"
Text="Submit"
OnClick="ButtonClick" />
when we click button then also form submit but how. i know that in this case __doPostback does not invoke because for button __doPostback is never rendered in the page. so in this case form submit but how asp.net engine detect which button causes postback and invoke right event handler?
how we get the data from textbox like txt1.text when postback occur. is it extracted from viewstate....am i right.
please answer for my 2 question. try to explain here instead of giving any url...thanks.
It's pretty simple with respect to how controls postback. They do it in the same fashion as pure html solutions. There is a Form tag on the page. When the button is pressed it causes the form to submit a postback to the server.
Notice the action target of the form tag, it points to my aspx page
This is all generated by the aspx page itself.
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA0OTM4MTAwNGRk5H5eaMNZp5tsD/GQ2iYj2p8J0as=" />
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKmxpCiBgKSotaIC5iWtiHNi+oxYiuBULPgr5d0/WFu" />
</div>
<div>
<input type="submit" name="btn1" value="click me" id="btn1" />
</div>
</form>
Yes asp:button works differently by default. It outputs a standard HTML Submit button which posts back to the Action URL specified on the form. It therefore doesn't use events in the same way as other controls.
However if you add a OnCommand and assign CommandName/CommandArgs to the control, it will then cause the same event based callback behaviour as other controls. The same is true of OnClick. I assumed this used the same process as other controls but actually it's different.
It looks like it uses __VIEWSTATE and __EVENTVALIDATION to determine which form posted back, and the name of the submit button id is posted in the form as part of part of standard HTML form behaviuor. This is what is used to trigger the server-side events.

When does an HTML input tag post back on enter?

I have a couple of asp.net pages with TextBox controls on them. Some of them postback on enter (within a TextBox control) and others don't. For the life of me, I can't figure out why or why not...
I don't think this has anything to do with asp.net. Not using ajax etc. Tried playing with a very simple html page and still can't figure it out.
Maybe within or not within a <form> tag? But all my asp:textbox controls are within the standard form tag and some do and some don't. Not related to autoPostback from what I can see.
EDIT:
Sample markup which causes a post back:
<html>
<body>
<form>
<input type=text />
</form>
</body>
</html>
I have asp.net pages that definately don't post back. The code is extensive and I can't figure out why not.
EDIT2:
How can I prevent a postback?
If you only have one textbox and don't wan't this behavior, you could always put a hidden input on the form.
<html>
<body>
<form>
<input type="text" ></input>
<input type="text" style="visibility:hidden"></input>
</form>
</body>
</html>
I've tried this in IE7 and FF3 and it works.
Generally most web browsers will detect when a form has only one textbox and will automatically submit the form when enter is pressed.
If you have multiple text boxes or multiple form buttoms, the behaviour varies. ASP.Net has the "defaultButton" mechanism for buttons, meaning you can specify which button is the submit button for a given panel in the case of the enter key being pressed. You could check out this post for more information on setting up your forms to correctly post back.
As Boo mentioned, this won't be triggered by multi-line textboxes.
If the behaviour is really crappy in your form between browsers, you may need to look at a javascript solution to catch the enter key being pressed, and "manually" submitting the form via javascript.
If the textbox control is set to autopostback it will postback on every keypress (thus allowing the firing of the TextChanged event).
Next, text boxes that have their Multiline property set to false will appear to postback on Enter.. what is actually happening is a form submit as defined by the behaviour within the browser.
Boxes that have Multiline set to true will not cause a form submit on Enter.

Resources