I have a grid view with multiple select check boxes. I want to return an array of the items that have been selected and put this in a label (comma-separated)
12,34,34,54,54,5
I want it to be parsed and then sent to a query string or send the whole value to the query string.
How do a get multiple selected check boxes and return an array of items?
Try this:
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<int> Values = new System.Collections.Generic.List<int> { 1, 2, 3, 4, 5, 6, 7 };
grdTest.DataSource = Values;
grdTest.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chkTest = null;
string SelectedValues = "";
foreach (GridViewRow row in grdTest.Rows)
{
chkTest = (HtmlInputCheckBox) row.FindControl("chkTest");
if (chkTest != null && chkTest.Checked)
{
SelectedValues += (SelectedValues == "" ? chkTest.Value : ", " + chkTest.Value);
}
}
litValues.Text = SelectedValues;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input id="chkTest" type="checkbox" name="chkTest" runat="server"
value="<%# (int)Container.DataItem %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
<br />
<asp:Literal ID="litValues" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
<form>
<input type="checkbox" name="check[]" value ="1"/>
<input type="checkbox" name="check[]" value ="2"/>
<input type="checkbox" name="check[]" value ="3"/>
<input type="checkbox" name="check[]" value ="4"/>
</form>
When you try to retrieve the parameter 'check' you get an array of all the values that were selected.
EDIT:
This link should help in retrieving the values in ASP.
ASP.NET : The checkbox and checkboxlist control
Related
Good afternoon, I'm learning aspnet, and web development, and I'm having a hard time understanding how I can call a certain postback function ... could anyone please answer me how do I do this...
codigo:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnTest.Value = hid.Value;
}
protected void function2()
{
//param other function here
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>test postback</title>
<script type="text/javascript">
function doTest() {
var nome = document.getElementById("nome");
var hid = document.getElementById("hid");
hid.value = nome.value;
return true;
}
</script>
</head>
<body>
<form id="form1" name="form1" runat="server">
<div>
<input type="text" id="nome" value="%%" name="nome" runat="server"/>
<input type="hidden" name="hid" id="hid" value="" runat="server" />
<input type="submit" id="btnTest" runat="server" name="btnTest" value="button" onclick="doTest();" />
</div>
</form>
</body>
</html>
If you want to use html controls you have to use the onserverclick method:
<input runat="server" type="button" onserverclick="ButtonClick"/>
In you code behind:
protected void ButtonClick(object sender, EventArgs e)
{
//Your code goes here
}
You can also use the built in WebControls:
<asp:Button runat="server" ID="btnTest" OnClick="ButtonClick"/>
The code behind would be the same.
I want to focus on the same ListItem which causes the postback in a asp.net radiobuttonlist. But the implementation that have developed currently is focusing on first listitem only. Is there a way to focus on the particular listitem which causes the postback. I can not use Update panel in my case. My HTML and CodeBehind is as below:
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name" AssociatedControlID="txtName"></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text="Enter your name"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblPersonType" AssociatedControlID="rdoPersonType" runat="server" Text="Person Type"></asp:Label>
<br />
<asp:RadioButtonList ID="rdoPersonType" RepeatLayout="Flow" AutoPostBack="true" runat="server" OnSelectedIndexChanged="rboPersonType_Selected"></asp:RadioButtonList>
</div>
</form>
</body>
</html>
CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rdoPersonType.Items.Add(new ListItem() { Text = "Student", Value = "1", Selected = true });
rdoPersonType.Items.Add(new ListItem() { Text = "Greencard Holder", Value = "2" });
rdoPersonType.Items.Add(new ListItem() { Text = "Refugee", Value = "3" });
rdoPersonType.Items.Add(new ListItem() { Text = "Asylum", Value = "4" });
}
}
protected void rboPersonType_Selected(object sender, EventArgs e)
{
rdoPersonType.Focus();
}
There are several ways to do that. You can either control the entire thing from the client side, or you can do this from the server side:
protected void rboPersonType_Selected(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(
typeof(Page),
"SetFocus",
string.Format("document.getElementById('{0}_{1}').focus();", rdoPersonType.ClientID, rdoPersonType.SelectedIndex),
true);
}
I'm sorry in advance if this seems to be a beginners question and wasted some of your time, but it does really bother me and I would really appreciate it if someone could help.
I'm simply creating a "zone" if you will, where the user inputs a word/sentence which are tasks, and then can check if the task was completed or not and/or if the word/sentence needs to be changed.
So in the end, what I need is a textbox lets say where the user inputs a specific number supposed x, and generates x times this div.
Here is the code (unfortunately I could not post pics since I do not have enough reputation..)
ASP.NET code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GMode.tiz.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>whatevz</title>
<link href="StyleSheet1.css" rel="stylesheet" />
<script src="../jquery-1.11.3.min.js"></script>
<script src="../functions.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"></asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"></asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GMode.tiz
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCheck_Click(object sender, EventArgs e)
{
lbl.CssClass = "done";
}
protected void btnCancel_Click(object sender, EventArgs e)
{
lbl.CssClass = "undone";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (hi.Text == "")
{
lbl.Text = "No Value";
}
else
{
lbl.Text = hi.Text;
}
}
}
}
So basically I want to dynamically create <div class="singleContainer"> with everything inside it x times (x defined by the user).
I'm going about this more on the fact like it's C++. When you can dynamically create an array with a specific number x.
Please tell me if you need more of the code. There are still the css and js files that I did not post.
Thank you,
Jack
I think that for this the best option is to use AngularJS, you can do something like this:
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js"></script>
<script>
var app = angular.module('app', [])
app.controller('PostsCtrl', function ($scope) {
$scope.change = function (){
$scope.listDivs = [];
for(var i=0;i<$scope.createDiv;i++) {
$scope.listDivs.push(i);
}
}
})
</script>
</head>
<body ng-app='app'>
<div ng-controller='PostsCtrl' class='container'>
<input ng-model='createDiv' ng-change="change()"/>
<ul class='list-group'>
<li ng-repeat="post in listDivs" class='list-group-item'>
<div class="singleContainer">
<div class="lblPlace">
<asp:Label ID="lbl" runat="server" Text="Input Task"> </asp:Label>
<asp:TextBox ID="hi" CssClass="txtBox" runat="server"> </asp:TextBox>
</div>
<asp:Button ID="save" runat="server" CssClass="UniversalBtn" OnClick="btnCheck_Click" />
<asp:Button ID="cancel" runat="server" CssClass="UniversalBtn" OnClick="btnCancel_Click" />
<asp:Button ID="edit" runat="server" CssClass="UniversalBtn" />
<asp:Button ID="submit" runat="server" CssClass="BtnSubmit" OnClick="btnSubmit_Click" Text="DONE"/>
</div>
</li>
</ul>
</div>
</body>
</html>
Put your div into a usercontrol (ascx file).
In your main page add a placeholder:
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
in code behind you can add your control as often as you want:
yourControl= LoadControl("~/Controls/yourControl.ascx");
PlaceHolder1.Controls.Add(yourControl);
I have an application where a PDF file is opened inside the web form as shown in the below code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
<div id="divPDF" runat="server">
<object data="<%=pdfPath%>" type="application/pdf" style="height:500px; width: "500px" >
</object>
</div>
</div>
</form>
</body>
</html>
protected string pdfPath;
protected void Page_Load(object sender, EventArgs e)
{
divPDF.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.FileName != "")
{
FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
pdfPath = FileUpload1.FileName;
divPDF.Visible = true;
}
}
It is working fine and I can view and read the pdf. Now what I need is when I scroll through the pages the current pagenumber should appear in a text box.
In first visit, Page_Load is running and session store correctly. When I click on Sign In button, Page_Load is call again, and then call btnSignIn_Click function, but Session is empty!
public partial class LoginPage : System.Web.UI.Page
{
UserItem userItem = null;
protected void Page_Load(object sender, EventArgs e)
{
//Initialize and validate post
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
protected void btnSignIn_Click(object sender, EventArgs e)
{
if (Session["post"] != null)
{
RequestObj post = Session["post"] as RequestObj;
userItem = Functions.LogIn(post);
}
Response.Redirect("LogIn.aspx");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%# Page Language="C#" AutoEventWireup="true" CodeBehind=" LoginPage.aspx.cs"
Inherits="myNameSpace.LoginPage " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=320"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%; text-align:center;">
<%if (userItem == null)
{%>
Username:
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
Password:
<asp:TextBox ID="txtPass" runat="server" TextMode="Password">
</asp:TextBox>
<br />
<asp:Button ID="btnSignIn" runat="server" Text="Sign In"
onclick="btnSignIn_Click" />
<%}else{%>
<%=userItem.LoginName%>
<br />
<%=userItem.LoginTime.ToString()%>
<br />
<asp:Button ID="btnSignOut" runat="server" Text="Sign Out"
onclick="btnSignOut_Click" />
<%}%>
</div>
</form>
</body>
</html>
Did you mean to only set session when the page renderes initially?
Try wrapping the assignment like this
if(!IsPostBack)
{
RequestObj post = new RequestObj(Context);
if (post.isValid)
{
Session["post"] = post;
}
}
This will prevent this from being reset during a postback
Otherwise I would check the value of post in a debugger