So i have a querystring where i save a search value from a input textbox. When i write, what i wish to search for and press enter, i get redirected and then i have the input from the search in the querystring. I want to populate a listview using the values from the querystring. But for some reason i dont get the query string value?
Here is how i try to get the value from the querystring:
if (Request.QueryString["txtSearchInput"] != null)
{
string input = Request.QueryString["txtSearchInput"].ToString();
SearchForItems(input);
}
And here i the URL that i try to get the querystring from:
http://localhost:46202/Users/AllProducts?ctl00%24txtSearchInput=213
in this example i have entered 213 as the search criteria.
And last here is the inputbox where i enter the values:
<input runat="server" id="txtSearchInput" name="search" placeholder="GTIN, Brand or Article nr" />
changed the input box to this:
<input name="search" placeholder="GTIN, Brand or Article nr" />
and the queryrequest to this:
if (Request.QueryString["search"] != null)
{
string input = Request.QueryString["search"].ToString();
SearchForItems(input);
}
now it works. As stuartd said, the id tag messed up the querystring.
Related
I'm a beginner. I'm still studying. I have made this code, which works as intended.
However, for each time I go back to another page, it can not, of course, save it to a list.
It disappears directly after I'm gone from this page.
The server page looks like this
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult pizaCart(string pizaName, string pizaDesc, string pizaPrice)
{
List<pizaModel> cartList = new List<pizaModel>();
toCart.CartList = cartList;
pizaName = Request.Form["pizaName"];
pizaDesc = Request.Form["pizaDesc"];
pizaPrice = Request.Form["pizaPrice"];
cartList.Add(new pizaModel { name = pizaName, desc = pizaDesc, price = pizaPrice });
return View(toCart);
}
html page looks like this.
<form action="piza" method="post">
<input class="n" type="text" name="pizaName" id="pizaName" value="" /><br />
<input class="n" type="text" name="pizaDesc" id="pizaDesc" value="" /><br />
<input class="n" type="text" name="pizaPrice" id="pizaPrice" value="" /><br />
<button class="btn">add</button>
</form>
"I have tried to google it and look for it lots of places, but havent found any good enough answer"
-- hmm i probably need a loop somewhere?
As you can see, it's a very simple way to post data to list. Is it possible that I can keep adding to my list? (Maybe it has something to do with lifecycle). Thank you very much for your time.
When you call
new List<pizaModel>()
...you are creating a new list. A new list has zero elements.
Immediately after that you call
cartList.Add(new pizaModel { name = pizaName, desc = pizaDesc, price = pizaPrice });
...which adds the current item to the list, which results in a list with one item.
There is no notion in this code of adding to an existing list, and it will never contain more than one item.
You will need to figure out some way of keeping the list from action to action. For example, you could persist the list in the browser and then post the whole list as JSON. Or you could store the list in a session variable or database table on the server side.
You may use sessions as below
Session["myPizza"] = cartList ;
Then cast it from the end of your Action result as below
var SelectedList = (List<pizaModel>)Session["myPizza"];
I'm trying to get my selected value/text from and HTML select option but I can't get it! I already tried with answers posted in this website but none works.
This my code:
<select id="Select" name="Select" runat="server">
<option value="1">Option 1</option>
</select>
I tried these C# codes:
//With this I get Index out of range error message:
string name = nombre.Items[nombre.SelectedIndex].Text;
//With this I get nothing:
string name = this.nombre.Value.ToString();
//Nothing here:
string name = this.nombre.Value;
What can I do? It's necessary using html select control.
to get a value,
string sVal = dropdownName.Items[dropdownName.SelectedIndex].Value;
to get a text,
string sText= dropdownName.Items[dropdownName.SelectedIndex].Text;
You would probably be better off using an asp:DropDownList, but you can still reference straight selects. Here's code that will grab the value and text from your HTML above:
string value = Select.Items[Select.SelectedIndex].Value;
string text = Select.Items[Select.SelectedIndex].Text;
Be sure you're using the correct ID name (Select is shown in your html, but nombre is used in your code behind).
Here is my logic in a code snippet.
I am trying to login, if data comes from web-page and if it matches with the database to proceed allowing to login
[HttpPost]//post method
public ActionResult Index(FormCollection collection)//using formcollection
{
var logindata = amcs.Logins.Where(a => a.Admin_Email_Id == collection["Admin_Email_Id"] && a.Admin_Password == collection["Admin_Password"]).SingleOrDefault();//compare string
if (logindata == null)//match data
{
return Redirect("/Contract/Login/index");//redirect if not match
}
else
{
Session["Emailids"] = collection["EmailId"];//add session
Session["Passwords"] = collection["AdminPassword"];
return Redirect("/Contract/Homepage/index");
}
}
If you are getting NULL as a result, have you looked further into this yourself?
For example, what values are
collection["Admin_Email_Id"]
collection["Admin_Password"]
Does the data in amcs.Logins contain objects whose properties match those values? You can hover the mouse of the data and look at it in the debugger.
EDIT
In response to a comment:
In the HTML does the
<input type="text" id="Admin_Email_Id"> also have an attribute name="Admin_Email_Id"? If not then add it manually e.g.
Html.TextBoxFor(m => m.Admin_Email_Id, new { name = "Admin_Email_Id", PlaceHolder = "EmailId"})
I'd be surprised that you need to do that though, but it's worth checking the HTML to check that name is there. Without name, when posting to the controller, the FormColleciton won't have a value for that missing name
I have a html form that submits to a C# ashx handler that i'm hoping will insert/update the database
I've written this in PHP and Coldfusion, but I cannot figure out how to do this in C#
HTML form
<form id="copyto">
<input type="hidden" name="operation" value="update" />
<label><input type="checkbox" name="children[]" checked="checked" value="001">
Andrew Regan</label>
<label><input type="checkbox" name="children[]" checked="checked" value="101">
Arthur Regan, III</label>
<input type="checkbox" name="children[]" checked="checked" value="968">
Tim Reagan
</form>
C# ASHX handler
foreach(string key in context.Request.Params["children"])
{
ListDictionary updateParams = new ListDictionary();
updateParams.Add("rowid", key);
string sSql = #"insert into temp select * from children where c.id = :rowid";
dbi.ExecuteNonQuerySql(sSql, updateParams);
}
Typically i would iterate over the $_POST['children'] in php , and execute the sql
How exactly does this translate?
EDIT
ok ive almost gotten this, however my iterator goes over ALL of the request collection variables, i want it to go over only a specific named variable, in this case "children"
i.e localhost/page?operation=update&children=9&children=8&children=17
foreach(string key in context.Request.QueryString)
{
ListDictionary updateParams = new ListDictionary();
updateParams.Add("row_id", context.Request.QueryString[key]);
string sSql = #"insert into dug select :row_id from dual";
dbi.ExecuteNonQuerySql(sSql, updateParams);
}
i want it to ignore everything but the specific var
If you are doing a post. I think something like this would work.
<input type="checkbox" name="children" value="108"/>
<input type="checkbox" name="children" value="109"/>
<input type="checkbox" name="children" value="110"/>
<input type="checkbox" name="children" value="111"/>
The browser will send all of the values comma seperated to the server when the form is submited
Then on your server side you can do this:
var selected = context.Request.Form["children"].Split(',');
Selected will be an array of strings for each value that was passed in by the browser. You can then loop over them and do whatever you need to.
Hope this helps some.
I was just working on this yesterday. I ended up using a hidden field that will hold the multiple checked checkbox id's. So, if that route works for you, you could create a checkboxlist editor template or control. This could have a script such as:
(tempId will hold the common "name" attribute's value for your checkbox/checkboxlist, and we have the "somehiddenfield" hidden field to hold the selected values)
<script>
$(function () {
var arrTmp = [];
//Update array on selection change
$('input[name="#String.Format("{0}[]", tempId)"]').change(function () {
arrTmp = [];
$('input:checked[name="#String.Format("{0}[]", tempId)"]').each(function () { arrTmp.push($(this).val()); });
$('input[id="somehiddenfield"]').val(arrTmp.join(','));
});
});
</script>
Then, on postback on the server-side the form collection will simply have the hidden field we wrote the checked values into. Split that in whatever way works for you (like comma separated in my example) and you're good to go. My server-side is implemented in MVC but for WebForms you can pull the elements from the Request.Form dictionary (Request.Form["somehiddenfield"].ToString()) or even Request.Params as you are using.
Right after i put out the bounty of course -_-
foreach (string k in context.Request.QueryString)
{
if (k.StartsWith("children")){
foreach (string v in context.Request.QueryString.GetValues(k)){
ListDictionary updateParamss = new ListDictionary();
updateParamss.Add("row_id", v);
string Sql = #"insert into dug select :row_id from dual";
dbi.ExecuteNonQuerySql(Sql, updateParamss);
}
}
}
I need to get a value from an input, but the id always change, only the name is the same.
Didn't found anything on google to extract the value by the name tag.
Example:
<input type="hidden" name="data[_Token][key]" value="5aafaee2dd21555c2615fd26c0cccd0f1b2c3018" id="Token749368899" /></div>
I look forward to some answers.
var input= doc.DocumentNode
.Descendants("input")
.First(n=>n.Attributes["name"].Value=="data[_Token][key]");
You can try getting the first part of Id, if it's always Token#####
//input[starts-with(#id, 'Token')]
Try this
var dummy = document.getElementsByName("data[_Token][key]");
This will return you elements which has name "data[_Token][key]".