Assign TextBox Value to the session in MVC - asp.net

In MY MVC Application I used input Type Textbox
and i need to assign that value to the Session how?
Im using code like
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] =textbox1; %>
<% HttpContext.Current.Session["Age"] = textbox2; %>
But i got error pls help on this....

Why would you prefer Session instead of hidden input box? Well, I think you should use name rather than id of your textbox.
Try this and let me know if there is problem.
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] = namebox; %>
<% HttpContext.Current.Session["Age"] = agebox; %>

have you tryed this:
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] = textbox1.value; %>
<% HttpContext.Current.Session["Age"] = textbox2.value; %>
Edit: try
<% HttpContext.Current.Session["Name"] =Request.Form["textbox1"]; %>

Try this:
<input type="text" value="#Session["Name"].ToString()"/>

Related

Classic ASP multiple checkbox click redirect to same page with value

I am doing some search based on check box value. If I select one checkbox I need to one value if I select second checkbox also need 2 checkbox values.
Currently I have code with radio buttons.Need to convert them to checkboxes with multiple selections. Based on request value, it will pass to a stored procedure.
<% if request("view")="Key CIA Initiative" then %>
<input type="radio" checked="checked"
onclick="javascript:document.location='Index.asp?view=Key CIA Initiative'" />
<input type="radio" onclick="javascript:document.location='Index.asp?view=High'" />
<input type="radio" onclick="javascript:document.location='Index.asp'" />
<% else %>
<% if request("view")="High" then %>
<input type="radio" onclick="javascript:document.location='Index.asp?view=Key
Initiative'" />
<input type="radio" checked="checked" onclick="javascript:document.location='Index.asp?
view=High'" />
<input type="radio" onclick="javascript:document.location='Index.asp'" />
<% else %>
<input type="radio" onclick="javascript:document.location='Index.asp?view=Key CIA
Initiative'" />
<input type="radio" onclick="javascript:document.location='Index.asp?view=High'" />
<input type="radio" onclick="javascript:document.location='Index.asp'" />
<%end if%>
<%end if%>
When the radio buttons are clicked, it redirects to same page with request value ,then I am passing to the stored procedure
If request("view")="Key CIA Initiative" Then
strSQL = "Exec sp_get_Project_list 'Key CIA Initiative' "
Else
If request("view")= "High" then
strSQL="Exec sp_get_Project_list 'High' "
Else
strSQL="Exec sp_get_Project_list "
end if
end if
You will have to do something like this:
<%
Response.write "Request(view)="& request("view") & "<br />"
'Now, if request("view") has nultiple values, How are you going to call the sp?
'pass both values to it?
If Trim(Request("view"))<>"" Then
If InStr(Request("view"),"All")<>0 Then
strSQL="Exec sp_get_Project_list"
Else
strSQL = "Exec sp_get_Project_list '"& Request("view") &"'"
End if
Response.write "strSQL ="& strSQL & "<br />"
End If
%>
<form>
<input type="checkbox" name="view" value="Key CIA Initiative" <% if InStr(request("view"),"Key CIA Initiative")>0 then %> checked="checked"<%End If%> />Key CIA Initiative <br/>
<input type="checkbox" name="view" value="High" <%if InStr(request("view"),"High")>0 then %> checked="checked"<%End If%> />High<br/>
<input type="checkbox" name="view" value="All" <%if InStr(request("view"),"All")>0 then %> checked="checked"<%End If%> />All <br/>
<input type="submit">
</form>

Why my checkBox statement doesnt works?

Why my checkBox statements doesnt works?I've used session as you seen(because my homework so dissmiss if it usefull or not):
<form id="ShoppingCart" action="Final.aspx">
<input type="checkbox" runat="server" id="CallOfDutyCheckBox" />
<%
if (CallOfDutyCheckBox.Checked)
Session["Username_CallOfDutyCheckBox_price"] = "5";
%>
<input type="submit" value="Buy It" />
</form>
<form id="Final">
<div>
<%
Response.Write(Session["Username_CallOfDutyCheckBox_price"]);
%>
</div>
</form>
Do you understand when you click the checkbox, the code at the server does not run yet? Click submit to submit the form with the value of the checkbox.

asp.net request.form

public ActionResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Customerid"]);
objCustomer.CustomerCode = Request.Form["code"];
objCustomer.Amount = Convert.ToDouble(Request.Form["amount"]);
return View(objCustomer);
}
This is my action in controller(MVC 2 aspx):
<form action="DisplayCustomer" method="post">
Customer id:-
<input type="text" id="Customerid" /><br />
Customer Code:-
<input type="text" id="code" /><br />
Customer amount:-
<input type="text" id="amount" /><br />
<input type="submit" value="click here" />
</form>
This is the view. When someone hit the submit button it is directed to a new page:
<div>
<b> ID</b> <%= Model.Id %><br />
<b> Code </b><%=Model.CustomerCode %><br />
<b> Amount</b> <%=Model.Amount %><br />
</div>
I always get 0 in Id and Amount while blank in CustomerCode.
Any clue? Why this is happening? What is wrong?
Your issue here is that you set the id, and not the name. Set the name to get the post back value. Eg:
<input type="text" name="Customerid" />
read also: HTML input - name vs. id in the line "Used on form elements to submit information"

changing HTML tag properties using inline server tags

I have the following submit button:
<form id="form1" runat="server">
<input type="submit" id="submit" value="Submit" runat="server"/>
</form>
How do I change e.g. the background colour of the button itself by using inline server tags after the button has been clicked?
I've tried using the attribute style="" in the submit buttons declaration with inline server tags <%%>, but I just get a Parser error message saying "Server tags cannot contain <% ... %> constructs."
Expected code to be used to verify button click:
<%
if (Request["submit"] != null)
{
// CODE HERE
}
%>
<form id="form1" runat="server">
<%
if (Request["submit"] != null)
{ %>
<input type="submit" id="submit" value="Submit" runat="server" style="background:#ffffff"/>
<% }
else{ %>
<input type="submit" id="submit" value="Submit" runat="server" style="background:#aaaaaa"/>
<%}%>
</form>
or
<form id="form1" runat="server">
<% string color="";
if (Request["submit"] != null)
{ %>
color="background:#aaaaaa";
<% }%>
<input type="submit" id="submit" value="Submit" style="<%=color%>"/>
</form>
If you use the second one you the submit button can not have runat="server" attribute
its not proper way to mix the server side code with the HTML markups..
use code behind for such things..
try the following..
if(Request["submit"]==null)
submit.Style.Add("background", "red");
else
submit.Style.Add("background", "white");

creating dropdownlist from ViewData

Does anyone have any idea why the code below doesn't give me any value but instead gives me "System.Web.Mvc.SelectListItem"?
If I don't do a foreach but instead substitute the ViewData with this
<%= Html.DropDownList("PersonOnCallCheckBoxList") %>, I get the correct value. Please help.
foreach (var person in ViewData["Person"] as IEnumerable)
{
%>
<input type="checkbox" value="<%= person %>" /><%= person %><br />
<%
}
Because person is a SelectListItem.
use person.Text to get the displayed text and person.Value to get the backing value
Html.DropDownList is built for working with SelectListItems so it does the right thing, but if you are manually working with the Items you'll have to get the Value and Text yourself.
<%
var list = this.ViewData["Persons"] as SelectList;
foreach (var person in list)
{
%>
<input id="cbPerson" type="checkbox" value="<%= person.Value %>" />
<label for="cbPersonOnCall"><%= person.Text %></label><br />
<%
}
%>

Resources