Any Way To get The selected Indices only in CheckBoxList? - asp.net

I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.
I am Getting Selected Value In tWo case In this way:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
In ListBox we have the Option To get Seected Only Items is there any For Check Box List?

I dont think there is but you could use this extension method that does exactly that:
public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
List<ListItem> list = new List<ListItem>();
foreach(ListItem lt in checkBoxList)
{
if(lt.Selected)
list.Add(lt);
}
return list;
}
//Call it like this
checkBoxList.GetSelectedItems();

You actually answered your own question. There is no way to get the selected only items in the CheckBoxList control, unlike the ListBox control.
This article has an explanation and a work around help method.
http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx

Related

Search bar by using LinQ

I want to show the results only that match the user input, but this code will show the whole table, do you know what is the problem? For example, if the user enters "233442" id it will only show him the users that their id "233442". The user could search by id, name, or username.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace SearchBarLinQ
{
public partial class SearchBarLinQ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DataClasses1DataContext db = new DataClasses1DataContext();
protected void btnsearch_Click(object sender, EventArgs e)
{
var st = (from s in db.SearchTable2s where s.ID == int.Parse(TxtID.Text) select s).First();
TxtName.Text = st.Name;
TxtUsername.Text = st.Username;
LoadData();
}
void LoadData()
{
var st = from s in db.SearchTable2s select s;
GridView1.DataSource = st;
GridView1.DataBind();
}
}
}
Function btnsearch_Click is processing the search, but the value you found is not displayed. You need to assign the value found to the data source of girdview.
Refer to the sample code(Customize the param object of the LoadData function to be compatible with the view):
protected void btnsearch_Click(object sender, EventArgs e)
{
var st = (from s in db.SearchTable2s where s.ID == int.Parse(TxtID.Text) select s).First();
TxtName.Text = st.Name;
TxtUsername.Text = st.Username;
LoadData(st);
}
void LoadData(object param)
{
GridView1.DataSource = param;
GridView1.DataBind();
}

Extracting data from Table2 to TextArea of Table1

I have designed 2 tables in my aspx page. In table1 there is blank TextArea. In table2 there are fields like FirstName,LastName,Age,DOB and these attributes are already entered in the database. I have entered corresponding symbols for these attributes (for e.g. for Firstname symbol is {F}, for last name symbol is {L} etc..).
My requirement is when i click on a field of Table2 (for e.g. FirstName) it will be displayed on TextArea of table1 like "Hi My Name is {F}".
I have tried myself to do this program, but i did not extract the
symbol of the attribute in my textarea.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(#"Data Source=AITPLCP72\SQLEXPRESS;Initial Catalog=Template;Integrated Security=True");
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlDataAdapter adp = new SqlDataAdapter("select field,Symbols from temp2", con);
adp.Fill(ds);
}
}
protected void btnfirstname_onclick(object sender, EventArgs e)
{
Symbol("firstname");
}
protected void btnlastname_onclick(object sender, EventArgs e)
{
Symbol("lastname");
}
protected void btnage_onclick(object sender, EventArgs e)
{
Symbol("age");
}
protected void btndob_onclick(object sender, EventArgs e)
{
Symbol("dob");
}
protected void btnsubmit_Click1(object sender, EventArgs e)
{
string s = TextArea1.InnerHtml;
Response.Redirect("http://localhost:2482/Template1/Default3.aspx?text1=" + s);
}
public void Symbol(string s)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
if (row[0].ToString() == s.ToString())
{
string st = TextArea1.Value;
st += row["Symbols"];
TextArea1.Value = st;
}
}
}
}

C# Calendar Tool ASP.NET

So I'm trying to create a calendar tool that pulls in dates from an SQL Database.
I've created a TableAdapter which is here: http://bit.ly/KRaTvr
This is the small bit of ASP I have to create my calendar:
<asp:Calendar ID="calEvents" runat="server"> </asp:Calendar>
Then I have the C# in my project used to pull the information through.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BreakyBottomTableAdapters;
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Data Table
BreakyBottom table;
//Table Adapter
BreakyBottomTableAdapters.ScheduleDate ta;
ta = new BreakyBottomTableAdapters.ScheduleDate();
//Populate the table using the Table Adapter passing current date
table = ta.GetData(e.Day.Date);
//Check the number of rows in the data table
if (table.Rows.Count > 0)
{
//Change the background colour to gray
e.Cell.BackColor = System.Drawing.Color.Gray;
//Allow the Day to be selected
e.Day.IsSelectable = true;
}
}
}
Now I know I'm missing something but I can't for the life of my figure out what it is.
Here is a screenshot of the compile error I get: http://bit.ly/ISuVsT - I know I'm probably missing something insanely obvious but any assistance would be appreciated.
Best Regards
The compiler is telling you that EventArgs does not have any member called Day which you are apparently using in your code incorrectly:
protected void Page_Load(object sender, EventArgs e)
{
....
table = ta.GetData(e.Day.Date); //WRONG: Day is not a member of EventArgs
}
If the idea is to use the Current date, as you mentioned, then do this:
table = ta.GetData(DateTime.Now);

PetaPoco orm tool with Asp.Net GridView

I am trying PetaPoco with my Asp.Net Project. How can I use PetaPoco with Asp.Net GridView. I am new at web programming.
I tried all code samples in the blog. They all are working with console app. But in Asp.Net I could not bind the datasource to GridView.
Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PetaPoco;
using northwind;
using System.Dynamic;
namespace Mng
{
public partial class Default : System.Web.UI.Page
{
public northwindDB db;
protected void Page_Load(object sender, EventArgs e)
{
db = new northwindDB();
if (!Page.IsPostBack)
{
var q = db.Query<Product>("SELECT top 10 * FROM Products");
grdMng.DataSource = q;
}
else
{
Response.Write("Postback occurs");
}
}
}
}
Call DataBind method to build the grid
grdMng.DataSource = q;
grdMng.DataBind();

Dropdownlist is not allowing new items added dynamically

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Expt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Bttnadd_Click(object sender, EventArgs e)
{
DropDownList11.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.Remove(ListBox1.SelectedItem);
}
}
In this when i add the item in dropdown list it shows an error:"Dropdown list does not allow multiple seelection"
But when i used to print the selected item It shows null exception error.
Try to add new list items like that :
DropDownList11.Items.Add(
new ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedItem.Value)
);
Because you're adding the ListItem that selected by the ListBox, it's Selected property is true. Then you get this exception.

Resources