Create form action as variable - asp.net

I have t following ASPX source :
<form name="AddComment" action="ViewArticle.aspx?ArticleID=<%=ArticleID %>" method="post" runat="server">
The problem is that when I click submit I got this url
http://localhost:61175/WebSite1/ViewArticle.aspx?ArticleID=%3C%=ArticleID%20%%3E
But I want get this URL :
http://localhost:61175/WebSite1/ViewArticle.aspx?ArticleID=1
If I delete the "runat="server"" command it works fine, but can this form work with the
variable and the runat server at the same time?

Maybe you can try setting the action just before the form markup like this (or in codebehind, at your choice)
<%
this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();
%>
<form name="AddComment" method="post" runat="server">

In server side controls you can't do such things as <%= ActionID %> you can only bind <%# ActionID %>.
The upper solution won't work, becouse the form is not that form. You have to give a control ID + name than setup the post url. If it's not an option you can dinamicly search for form item in the control tree, so dynamic ID-s can be handled.

Related

Using Request.QueryString in ASP.NET Embedded Code Block

I am attempting to pass a parameter from one file to another via the URL after a button is clicked. These are written with Express.js (index.ejs to items.ejs).
As it stands currently I am setting the URL parameter in a defined Javascript function:
function loadItems(page, subcategory) {
window.history.pushState(null, null, "?subcat=" + subcategory) //param set
$('#mainContent').load(page);
}
where subcategory is the changing variable.
From there I am trying to read this parameter during an ASP.NET function written in embedded code blocks.
<% if(items[i].subcategory === Request.QueryString["subcat"].Value) { %> //get param
<% if (items[i].status === "Supported") { %>
<tr class="success">
<td>Edit</td>
<td id="item name"><%= items[i].name%></td>
<td id="subcat name"><%= items[i].subcategory%></td>
<td id="item status"><%= items[i].status%></td>
<td id="item desc"><%= items[i].description%></td>
</tr>
However I am met with an error which states Request is not defined and a callback to the above if statement. It is my understanding that on the ASP.NET side of things, Request.QueryString is a part of System.Web.HttpContext.Current.
How would I go about including this into my code blocks so that I am able to pull the parameter from the URL? Or, if this is not the way to be looking at this problem, how should I go about it?
My advice would be to use code behind. Embedded code blocks are an old-school throwback from the asp days. But if you must, then you should be able to do something like this:
<%# Page Language="VB" %>
<script run=server>
Protected Function GetSubcat() As String
Return Request.QueryString["subcat"].Value
End Function
</script>
<form id="form1" runat="server">
Subcat value is <% =GetSubcat()%>.
</form>

error with form action

I have the following C# code:
AddCommentForm = string.Format("<form name=\"AddComment\" method=\"post\" runat=\"server\" id=\"add_comment_form\"><p> TITLE: <input type =\"text\" name=\"Title\" /></p><p> Contnt <textarea name=\"Content\" ></textarea></p><p> <button type=\"submit\">Submit!</button></p></form>");
this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID;
The problem is that there is an error in the second line:
System.NullReferenceException was caught
My question is how can I this error?
And why with this code it works?
<%
this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();
%>
<form name="AddComment" method="post" runat="server">
AddCommentForm = string.Format("<form name=\"AddComment\" method=\"post\" ....
Above code is not valid. AddCommentForm is a HtmlForm control - not a string. Besides, you cannot create another form tag inside a form in ASP.Net.
this.Form.Action = "ViewArticle.aspx?ArticleID=" + ArticleID.ToString();
Basically, you are Cross-Site Scripting. Although it compiles, it won't work. When you get to ViewArticle.aspx page, you'll get an error.
If you want a form inside ASP.Net, use iframe.

How do I resolve "Object reference not set to an instance of an object" error?

I am passing the following via the request object in codeBehind:
Get the Requested code to be created.
Dim Code As String = Request("code").ToString()
and below is my markup page called barcodes.aspx:
<form id="Form1" method="post" runat="server">
<img src="barcodes.aspx?code=XXXXX" alt="barcode" />
<asp:Image id="myBarCode" runat="server"></asp:Image>
</form>
Why am I getting the error above?
If Request("code") is null, calling .ToString() on it will give you a null reference exception.
Check if Request("code") is null or not. If it is null, then you will get an object reference error when calling ToString()
I think this has to deal with the fact that you are trying to get the query string value of an image on the page. Request() will only get the querystring for the 'requested page', which is the page you are on, not the image itself. Therefore your code will always be null if you are expecting it to pull from the image source
You need to actually submit the form (POST) or pass the parameters in the URL (GET) to get the request parameters via the Request object. Two easy ways (of many) to do this:
Option 1. Keep just your barcodes.aspx and barcodes.aspx.vb. In barcodes.aspx put:
<form id="Form1" method="post" runat="server">
<input type="hidden" name="code" value="XXXXX" />
<asp:Image id="myBarCode" runat="server"></asp:Image>
<asp:Button runat="server" Text="Submit"></asp:Button>
</form>
In barcodes.aspx.vb put in the Page_Load:
If PostBack Then
Dim code As String = Request.Form("code")
If Not String.IsNullOrEmpty(code) Then
' Generate your image here, a code has been specified
End If
End If
Then just hit the submit button on your aspx page.
Option 2. Split it into two aspx pages but basically same as above.
In submitme.aspx put this (for a POST, click the button):
<form id="Form1" method="post" runat="server">
<input type="hidden" name="code" value="XXXXX" />
<asp:Button runat="server" Text="Submit" PostBackURL="barcodes.aspx"></asp:Button>
</form>
Or this (for a GET, click the link):
Click Me
In barcodes.aspx.vb put this in Page_Load (works for either the GET or POST option):
Dim code As String = Request("code")
If Not String.IsNullOrEmpty(code) Then
' Generate your image here, a code has been specified
End If
In barcodes.aspx you would then simply need:
<asp:Image id="myBarCode" runat="server"></asp:Image>
You can simply remove ToString(), as Request(item key) returns String:
Dim Code As String = Request("code")
Check to see that the query string value exists:
Dim Code as String = String.Empty
If Not Request.QueryString("code") Is Nothing Then
Code = Request.QueryString("code")
End If
You could also use the string.isnullorempty() function to test the contents of the request.querystring value

custom controls and query string(asp.net)

How to call a custom control when query string is changed?
My example not work?Why?
<% if(Convert.ToInt32(Request.QueryString["id"])==6){ %>
<answer:answer_n id="give_me_top_five_news" runat="server" />
<%} %>
<% if(Request.QueryString["do"]=="registracija"){
Page.Header.Title = "HHHHH";
%>
<reg:f_reg id="custom_controls_for_registration" runat="server" />
<%} %>
Changed in what fashion? Anytime you load the page with a new URL and query string, the page will be loaded for the first time. You would have to save the info you wanted to store in the Session (for example) and then in the Page_Load event check the query string vs the Session variable to see if they are the same or different.
There may be different ways to do it, but that's what comes to mind off the cuff.

Issue with asp:ContentPlaceHolder and code blocks

When a content placeholder contains any code blocks it reports that the control collection is empty.
For instance:
MasterPage.aspx
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
APage.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain text content.
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content." %>
</asp:Content>
This will render the following:
Plain text content. Code block content.
Content1: 1
Content2: 0
Why is the master page's ContentPlaceHolder.Controls collection empty?
I want to check whether the ContentPlaceHolder has been populated (see also this question) but can't if it contains any <%= blocks.
Does anyone know a way around this?
As promised, I said I would take a look. Sorry I never uploaded last night, long day and needed to hit the hay!
So, I was checking out the ContentPlaceHolder.Controls collection differences between how they are populated. I noticed that when the code block is used, it flips to read only. At any other point, it will simply be empty or populated.
I therefore decided to throw in an extension method to check it for us:
ContentPlaceHolderExtensions.cs
public static class ContentPlaceHolderExtensions
{
public static bool ContainsControlsOrCodeBlock(this ContentPlaceHolder placeHolder)
{
if (placeHolder.Controls.Count > 0)
return true;
return placeHolder.Controls.IsReadOnly;
}
}
And then check this in the master page:
Site.Master
<asp:ContentPlaceHolder ID="Content1" runat="server" />
<asp:ContentPlaceHolder ID="Content2" runat="server" />
<asp:ContentPlaceHolder ID="Content3" runat="server" />
<div>Content1: <%= Content1.Controls.Count %></div>
<div>Content2: <%= Content2.Controls.Count %></div>
<div>Content3: <%= Content3.Controls.Count %></div>
<div>Content1 (Ex. Meth.): <%= Content1.ContainsControlsOrCodeBlock() %></div>
<div>Content2 (Ex. Meth.): <%= Content2.ContainsControlsOrCodeBlock() %></div>
<div>Content3 (Ex. Meth.): <%= Content3.ContainsControlsOrCodeBlock() %></div>
As proof-of-concept, I then added a content page:
Index.aspx
<asp:Content ContentPlaceHolderID="Content1" runat="server">
Plain Text Content
</asp:Content>
<asp:Content ContentPlaceHolderID="Content2" runat="server">
<%= "Code block content" %>
</asp:Content>
And all rendered as expected (I believe)..
TBH, while it is not perfect.. I don't think we can get much more elegance in this situation. I am not sure how other control collections are set up in these different scenarios, so I only bolted on to the ContentPlaceHolder control.. Other templated controls may or may not work the same.
Thoughts?
You can download the project from here:
http://code.google.com/p/robcthegeek/source/browse/#svn/trunk/stackoverflow/964724
Too much for a comment, here's the full code that I finally got working (adapted from #Rob Cooper's answer):
public static bool HasContent( this ContentPlaceHolder placeHolder )
{
if ( placeHolder.Controls.Count > 0 )
{
LiteralControl textBlock;
ContentPlaceHolder subContent;
foreach ( var ctrl in placeHolder.Controls )
if ( (textBlock = ctrl as LiteralControl) != null )
{ //lit ctrls will hold any blocks of text
if ( textBlock.Text != null && textBlock.Text.Trim() != "" )
return true;
}
else if ( (subContent = ctrl as ContentPlaceHolder) != null )
{ //sub content controls should call this recursively
if ( subContent.HasContent() )
return true;
}
else return true; //any other control counts as content
//controls found, but all are empty
return false;
}
//if any code blocks are used the render mode will be different and no controls will
//be in the collection, however it will be read only
return placeHolder.Controls.IsReadOnly;
}
This includes two extra checks - firstly for empty literal controls (which occur if the page includes the <asp:Content tags with any whitespace between them) and then for sub-ContentPlaceHolder which will occur for any nested master pages.
The controls collection is empty because when <%= %> script tags are present, literal controls are not added to the control tree. However, server controls will still get added. So:
<asp:Content ID="Content2" ContentPlaceHolderID="Content2" Runat="Server">
<%= "Code block content." %>
<asp:GridView runat="server" ID="gvTest" />
</asp:Content>
<div>Content2: <%= Content2.Controls.Count %></div>
will return
Content 2: 1
Rick Strahl has a great article that explains this behavior:
To make code like this work, ASP.NET
needs to override the rendering of the
particular container in which any
script code is hosted. It does this by
using SetRenderMethodDelegate on the
container and creating a custom
rendering method ...
Rather than building up the control
tree literal controls, ASP.NET only
adds server controls to the control
tree when <% %> tags are present for a
container. To handle the literal
content and the script markup, ASP.NET
generates a custom rendering method.
This method then explicitly writes out
any static HTML content and any script
expressions using an HTML TextWriter.
Any script code (<% %>) is generated
as raw code of the method itself.
Unfortunately I can't think of any elegant solution to this conundrum.

Resources