I have an issue that i believe there is a simple solution for but being new to ASP.NET it is not very clear to me.
I have a user control that has a for loop that makes bunch of hyperlink elements in a list (looks like below>
<li><a href="blahblah.aspx?ID=1..../></li>
<li><a href="blahblah.aspx?ID=2..../></li>
<li><a href="blahblah.aspx?ID=3..../></li>
<li><a href="blahblah.aspx?ID=4..../></li>
Next, that uc is used in another page with in a <form> tag with method="post" and <asp:button> that isn't really used.
Next, when one of the links is clicked it will go to blahblah.asp and get the ID in there via Request.QueryString
So far so good.
What i want to do though (and the reason for this post) is that i want to not use ID as a query parameter. I want to pass the ID in the body. So the link would be blahblah.aspx and it wouldn't show the ID.
What would be the best way to accomplish this. I've tried couple different ways but it's not working.
Not sure if this will work for you, but instead of using html a tags, use the asp LinkButton control. You can then handle the command event of this control so have your for loop generate the following controls (or better yet use a repeater):
<asp:LinkButton id="button1" runat="server" CommandArgument="1" OnCommand="SendToBlah" />
<asp:LinkButton id="button2" runat="server" CommandArgument="2" OnCommand="SendToBlah" />
private void SendToBlah(Object sender, CommandEventArgs e)
{
Session["ID"] = e.CommandArgument;
Response.Redirect("blahblah.aspx");
}
You can then use your session argument inside your blahblah.aspx page however you see fit.
To send the data in the HTTP body instead of the query string, you need to do an HTTP post. This is easy enough, just use a form with a hidden parameter:
<li>
<form action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
<input type="submit" value="submit" />
</form>
</li>
To get the data on the target page, use Request.Params instead of QueryString.
Now, if you want to use a hyperlink rather than a "submit" button, you can always submit the form using Javascript:
<li>
<form id="myform1" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
<form id="myform2" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
</li>
<script type='text/javascript'>
window.submitForm = function(id) {
document.forms[id].submit();
return false;
}
</script>
Related
I have a search textbox that I tried wrapping in a form element:
<form id="searchForm" method="GET" action="Search.aspx">
<input name="term" type="text" id="searchTerm" class="nav-search" size="30" />
<input type="submit" value="submit" style="display:none;"/>
</form>
I can't get it to redirect to the content page (Search.aspx) with the query string (term) from the Master Page.
I tried wrapping it in an ASP:Panel and using the DefaultButton Property but that wouldn't call the Click event in code behind.
I'm perplexed as I've searched for a solution for hours and this seems like a such common task. Thanks.
Change your code to this
<input name="term" type="text" id="searchTerm" runat="server" class="nav-search" size="30" />
<asp:Button ID="searchsubmit" OnClick="searchsubmit_Click" runat="server" Text="Search" />
in Site.Master.cs add something like this
protected void searchsubmit_Click(object sender, EventArgs e)
{
Response.Redirect("~/search.apsx?content=" + searchTerm.text);
}
This will allow to search with button click, if you want to hide it, add also panel around first code, saying that this is a button to automatically press when enter is pressed Something like this:
<asp:Panel ID="UserPanel" runat="server" DefaultButton="searchsubmit">
I've a form that points to itself on POST operation to process data. This part is good. Now in the form I've a textbox and a button (X) that is used for another purpose as well. When this button X is clicked it should take the input value from the text box and point to another ASP page, process data and return the value...and this is the part I'm unable to figure out... Let me explain this in a bit more clearly..
form name=frmAccounts method=Post action="self.asp"
...
...
name ---> Textbox
function ---> textbox :: search button(X) <--> sales.asp
Description --->textbox
...
...
Form Submit ---> button
Form ends
Now..suppose I enter function as "Sales" in the textbox, then when I click on the search button it would look at another ASP page like sales.asp that will query for relevant description and populate the description box...Then the form submit will call the self.asp to do its function.
Question: How can I pass value from function textbox, via search button, to sales.asp for processing and return back the value.
Things I've tried so far with no luck
1) Functions using #include method
2) Button onClick method by passing Request.Form data
3) href option by passing the URL by adding the form data - URL does not take the value
some other methods after Googling through many forums but with no joy.
Any help please?
Like Lankymart says, do something like this:
Self.asp
<form action="self.asp" name="myform" id="myform">
<input type="text" name="name" id="name" />
<br /><input type="text" name="functionbox" id="functionbox" />
<br /><input type="button" name="search" value="search" id="search" />
<br /><input type="text" name="Description" id="Description" />
<br /><input type="submit" value="submit" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#search").click(function() {
$.ajax({
data: {'functionbox': $("#functionbox").val()},
url: '/sales.asp',
success: function(data) {
if (data) {
$("#Description").val(data);
}
}
});
});
});
</script>
sales.asp
<%# LANGUAGE = JScript %>
var functionbox = Request.Form("functionbox").Item;
var answer = getAnswer(functionbox); // your code here
%><%= answer %>
I am new to asp.net.
My question is, can a ASP.net form with runat="server", have a method attribute in it?
For example:
<form id="form1" runat="server" method="get">
.......
</form>
Is this possible?
Thanks for your answers.
I would like to share some points which I found.
By default the form with runat="server", will have method="post".
But when we request a page for the first time, (i.e) request is not a postback, the method="get".
And it becomes method="post",while postback.
I checked this by placing a piece of code in code behind:
In Page_Load():
if(Request.RequestType=="GET")
{
Response.Write("Request is a GET type");
}
else if(Request.RequestType=="POST")
{
Response.Write("Request is a POST type");
}
By default, the output
For the first request of that page: Request is a GET type
In postback: Request is a POST type
If i give the following code in the WebForm1.aspx
<form id="form1" runat="server" method="get">
For this, the output will be:
For the first request of that page: Request is a GET type
In postback: Request is a GET type
This is what I found.
Thank you very much for your responses.
yes you can try like below.
design part
you can design a form like :
<form id="form1" runat="server" method="post">
<input type="radio" name="Gender" value="male" id="test" checked="checked" />
male
<input type="radio" name="Gender" value="female" />female
<input type="submit" value="test" />
<asp:Button ID="btn" runat="server" Text="value" />
</form>
and how to get value from the form :
if (Request.Form["Gender"] != null)
{
string selectedGender = Request.Form["Gender"].ToString();
}
with this way you can get the value from form in asp.net.
i hope it will help you.
Yes,You can use use Method attribute..
The default will be Method="Post"
The form is always submitted to the page itself. If you specify an action attribute, it is ignored. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.
If you select view source in an .aspx page containing a form containg these properties...
Please refer : http://www.w3schools.com/aspnet/aspnet_forms.asp
I'm really new to ASP and have a project where I need to capture the login of the person logged into Sharepoint. I have created the following code and am able to get the login. I am having trouble pass the parameter to a form. What is the best way to do this: I know the 2nd part the POST is wrong, I don't know what to do here. Thanks for your help. Scott
<script language="C#" runat="server">void Page_Load(Object sender, EventArgs e)
{
string userName = "NA";
userName = HttpContext.Current.User.Identity.Name;
string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);
myuserid.Text = userWithoutDomain;
}
</script>
<html>
<body>
<form method="POST" action="http://srs.xxx.edu:9001/signon/authenticate.asp" id="myuserid" name="myuserid">
<input type="hidden" name="COOKIEPATH" value="/SRS/">
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input class="button" type="submit" value=" Log On " style="display:none;">
</form>
<form id="form1" runat="server">
<asp:label id="myuserid" runat="server" />
</form>
</body>
</html>
You can pass values from the codebehind to html page using for example hidden values, see here.
Usually though values are placed into form elements in the codebehind directly. If the element has the "runat=server" attribute then you can access the element in the Page_Load method using c# and just set the text value directly.
myuserid.Text =
I have a web application in which I am dynamically generating and displaying checkboxes at runtime. Each checkbox corresponds to an item. I need to find which items are selected. However I cannot set the id of the item in the ID attribute of Checkbox control while generating the control as the same item can appear multiple times on the web page.The ID attribute enables me to use the Page.Form() method but now I cannot use. Please suggest an alternate method to find which checkboxes are checked.
If you use JQUERY you can assign a common class to each checkbox and then use a "class selector" to get an array of items on a page that all have the same class. With that you may iterate through the array and identify which items have been checked.
The relevant code will look something like:
<head>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function findCheckedBoxes() {
var AllBoxes = $(".productCheckBox");
for(i=0;i<AllBoxes.length; i++)
{
if(AllBoxes[i].checked)
{
//do something with it...
alert(AllBoxes[i].value);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server" onsubmit="return validatefields();">
<input id="Checkbox1" type="checkbox" class="productCheckBox" value="one" />One<br />
<input id="Checkbox2" type="checkbox" class="productCheckBox" value="two" />Two<br />
<input id="Checkbox3" type="checkbox" class="productCheckBox" value="three" />Three<br />
<input id="Checkbox4" type="checkbox" class="productCheckBox" value="four" />Four<br />
<input id="Button2" type="button" value="button" onclick="findCheckedBoxes()" />
</form>
</body>
</html>