Retrive data from form submission and display it on a page - asp.net

I would like to put a form on my page. There will be an input box and a submit button. When the user enter a specific id then i will take that id put it in a url and display the result from that url.
Let say user enter this: 12345678.
Then i want to take that id display the result from
http://foo.com/servlet/AccessModule?id=12345678&doc_type=pdf&sort1=capturedate&response=A on my page using asp.net technologies.
I already tried a couple of things but couldnt figure this out.
Can you please comment.

Here is how it could work. This needs some polishing but you should be able to bring it to completion yourself.
Copy this into .aspx page
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtInput"></asp:TextBox><br /><br />
<asp:Button runat="server" ID="btnSubmit" Text="Submit"
onclick="btnSubmit_Click" /><br /><br />
<asp:Label runat="server" ID="lblResult" Text=""></asp:Label>
</div>
</form>
And this into code behind (I’m assuming you’re using C#)
protected void btnSubmit_Click(object sender, EventArgs e)
{
System.Net.WebClient wc = new System.Net.WebClient();
lblResult.Text =
wc.DownloadString(string.Format("http://foo.com/servlet/AccessModule?id={0}&doc_type=pdf&sort1=capturedate&response=A", txtInput.Text));
}

<form action="www.foo.com/search" method="get">
ID: <input type="text" name="id"><br>
<input type="submit" value="Submit">
</form>

When you send an http request it reaches the server on port 8080. You can not specify the port that you are going to use.
To pass parameters in URL you have to use this
repsonse.redirect("AccessModule.aspx?id=12345678&doc_type=pdf&sort1=capturedate&response=A");

Related

Load another web page based on value of drop down list in asp.net

I am trying to redirect to another web page when the user selects the drop down list and the user should be redirected to another web page as soon as the user clicks the submit button.This is my code for drop down list and button.On clicking the "click to proceed" button the user should be taken to next web page depending on list selection.
Iam new to .Net Please help thanks in advance !!
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="-1">Select User</asp:ListItem>
<asp:ListItem Value="staff">Staff</asp:ListItem>
<asp:ListItem Value="student">Student</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Click to Proceed" />
In your Button1_Click event, use the Response.Redirect method to send people to the appropriate page.
http://msdn.microsoft.com/en-us/library/a8wa7sdt%28v=vs.110%29.aspx
Following can be helpful for you.
protected void Button1_Click(object sender, EventArgs e)
{
string sel_val = DropDownList1.SelectedValue; // use sel_val to apply if, else logic to redirect to
// some possible examples
if (sel_val != "-1") // as SelectedValue is string
{
// assuming there is staff.aspx, and you want to redirect to that page on selecting staff, this is just an example
Response.Redirect(sel_val + ".aspx"); // use this to redirect to required page
}
}
Also change aspx code like this.Note i have changed OnClick from onclick.
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Click to Proceed" />

ClientIDMode in CheckBox

What is use of ClientIDMode attribute in asp:CheckBox control? I found the values AutoID, Inherit, Predictable, and Static for that.
Mainly I am looking for how it is rendered in HTML? Can anyone explain about the attribute and the values?
It is how MS calculates ID of a .net control name itself. MSDN
Since ASP.net pages are put together on a server and sent to the client MS Will name the ids, based off of a variable amount of conditions, are they in a master page , are they in a user controller, a repeater, and so on.
Since you cannot have duplicate ids what do you do with a .net control that is put inside of a repeater? You need to have a naming algorithm for it. Some people foolishly use the rendered ID (ct100_*) instead of using getElementID to manipulate the element through javascript. I dont know why they exposed these algorithms instead of just saying everything is going to be 1 algorithm. Maybe someone has the answer to why they exposed the different algorithms.
Example of id generation:
Web form:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
lstMain.DataSource = new string[] { "a", "b" };
lstMain.DataBind();
}
</script>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ListView ID="lstMain" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkFour" runat="server" ClientIDMode="AutoID" />
<asp:CheckBox ID="chkFive" runat="server" ClientIDMode="Predictable" />
<asp:CheckBox ID="chkSix" runat="server" ClientIDMode="Static" />
</ItemTemplate>
</asp:ListView>
</asp:Content>
Result:
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFour" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFive_0" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFive" />
<input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkSix" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFour" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFive_1" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFive" />
<input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkSix" />
Pass id to JS:
<script type="text/javascript">
var autoId = '<%=someControl.ClientID%>';
var predictableId = '<%=someControl.ClientID%>';
var staticId = 'someControl';
</script>

Server side validation if Javascript disabled on user's browser

I have developed a small form with 3 input type = text and one input type = submit button. The end user fills the form and submit it, but no data inserted into backend table. (probably empty form is submitted). I get to know that Javascript is disabled on user's browser. Now i want to do server side validation. How i validate my form on server side? I need a piece of code to validate form on server side (code behinde) ? I need code in asp.net
You really should not ask for straight forward code on this forum. But either way here is a great resource for asp.net form validation: MSDN - Validating ASP.NET Server Controls
Essentially, assuming you are using C#, you might do something like this:
<%# Page Language="C#" %>
<script runat="server">
void Button1_Click(Object sender, EventArgs e) {
Label1.Text = "Page is valid!";
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="RequiredFieldValidator1" runat="server"
ErrorMessage="Required!"
ControlToValidate="TextBox1">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click"
runat="server" Text="Button"></asp:Button>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
My recommendation is to try and Google some and look for tutorials - there are pretty informative ones out there available with easy step by step directions.
Happy coding!

HTML forms in ASP.NET

Hello all how to use HTML text field instead of ASP.NET textbox.?
How to write C# code to make use of HTML forms instead of ASP.NET
EDIT:
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Text1" type="text" runat="server" />
<asp:Button ID="Button5"
runat="server" Text="Button" OnClick="button5_click" />
<div>
<script runat="server">
protected void button5_click(object sender, EventArgs e)
{
Text1.Value = "Hello";
}
</script>
</form>
Instead of using or whatever server control, just type in the standard type tags or whichever HTML control you want.
If you want to be able to access them from your code-behind, then just include a runat="server" value with each HTML tag and make sure you give it an ID.
You need to retrieve the value from the HTTP request's Form collection:
<input type="text" name="MyTextField" />
Code behind:
string value = Request.Form["MyTextField"];
This question doesn't make much sense. You really should clarify and flesh out the question and explain what you are trying to do.
In some ways, an ASP.NET textbox is an HTML text field that C# code can make use of. So what's the problem with an ASP.NET textbox?
You can create an instance of a HtmlInput control in code, or alternatively add a runat="server" attribute to an <input> element in your aspx file, you can then access this from server-side code using it's ID.
As an alternative, you may wish to take a look at http://asp.net/mvc to get better control of your HTML markup.

ASP.NET : Reading form variable values in the action page of search form

I have a website where i want to implement search functionality.So i added the below code to have a search box in my html page
<form id="search" method="post" action="Results.aspx">
<input id="txtSearchKey" type="text" name="txtSearchKey" />
<input id="Submit1" type="submit" value="submit" /><br />
<br />
</form>
In Results.aspx, I want to read the value user has entered in the txtSearchKey text box. What is the ideal way to do this ? I used
string strKey = Request.Form["txtSearchKey"].ToString();
But it throw a null reference exception.
I don't want to have all pages in ASP.NET.I want to have only the result page as ASP.NET
Could be because you do not have a NAME attribute on the textbox field. That's the value that is used as the key in the Request.Form collection. An input field without a name attribute will not be submitted, I think.
e.g.:
<input id="txtSearchKey" type="text" name="txtSearchKey" />
You can get your txtSearchKey field by this :
string strKey = PreviousPage.Request.Form["txtSearchKey"].ToString();
But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :
<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />
Because in ASP.NET, to have more then one form is not allowed.
Is there any reason you don't use
form runat="server"
and then drag a TextField and a Button in this form. Then doubleclick the button and write code you want.
If you want to do it your way you need to give your s a name="txtMySearchKey" for it to work
The way you are going about things is not really the way you work in ASP.NET web forms. The preferred way is to use asp.net server controls and events to abstract the process you are trying to achieve. For instance, your form should really be something like this (note the runat="server" attribute that enables you to reference the controls programmatically):
<form id="form1" runat="server">
<div>
<asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
<asp:TextBox ID="TxtSearchKey" runat="server" /><br />
<asp:Button ID="ButtonSubmit" Text="Submit" runat="server"
onclick="ButtonSubmit_Click" /><br />
</asp:Panel>
</div>
</form>
Then, in your code behind you would handle the ButtonSubmit_Click event like this to enable you to get the value from the TxtSearchKey textbox:
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
string strKey = TxtSearchKey.Text;
}
See the Quickstart example for the TextBox control for more info.
Just do not use .toString() after the Request.form... it will not give a null reference after that.

Resources