Adding a checkbox in a table cell - asp.net

Is this possible?
I have a table with user accounts retrieved from a database. Then at the start of each column I would like to add a checkbox which if selected will select the account. I tried experimenting with it and I can't seem to put the checkbox inside the table. How can I do this?

You can try this:
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}

Related

Radio button selected index not working

So I have a sipme ASP.NET Web Page that prints the info of one's gender stacked dynamically in a radio button list:
protected void Page_Load(object sender, EventArgs e)
{
String[] genders = new String[2];
genders[0] = "Male";
genders[1] = "Female";
RadioButtonList1.DataSource = genders;
RadioButtonList1.DataBind();
RadioButtonList1.Items.Add(new ListItem("Neutral", "Zero"));
}
protected void Button1_Click(object sender, EventArgs e)
{
lblName.Text = txtName.Text;
lblSurname.Text = txtSurname.Text;
lblEmail.Text = txtMail.Text;
Panel1.Visible = true;
if (RadioButtonList1.SelectedIndex==0) lblGender.Text = "Male";
else lblGender.Text = "Female";
}
However when I launch the site no matter what I select it always writes female it's like the RadioButtonList1.SelectedIndex==0 isn't working.
Any ideas?
Bind radiobuttonlist in page_Load with !IsPostBack condition or your radiobuttonlist will bind every time you post your page. So your radiobuttonlist is reset to index -1 when you click the button_click.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
String[] genders = new String[2];
genders[0] = "Male";
genders[1] = "Female";
RadioButtonList1.DataSource = genders;
RadioButtonList1.DataBind();
RadioButtonList1.Items.Add(new ListItem("Neutral", "Zero"));
}
}

GridView Editing when bound to a Datatable (no database)

I am a beginner at ASP.NET and I have been trying to allow editing/updating of a GridView that is bound to a Datatable, without apparent success.
The GridView, named "WeightGridView", has two columns, "Asset Class" and "Weight", and is populated upon the click of a button "AddAssetsButton". Once you click on this button, the GridView will be populated with the values of a listbox, "ListBox2". The code for this event handler is:
protected void AddAssetsButton_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Asset Class", typeof(string));
table.Columns.Add("Weight", typeof(double));
foreach (ListItem li in ListBox2.Items)
{
DataRow dr = table.NewRow();
dr["Asset Class"] = li.Value;
dr["Weight"] = 0;
table.Rows.Add(dr);
}
WeightGridView.DataSource = table;
WeightGridView.DataBind();
SelectAssetsButton_ModalPopupExtender.Hide();
}
I have created a RowEditing and RowUpdating functions for the GridView, but they don't seem to work properly.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
WeightGridView.DataBind();
}
}
protected void WeightGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
WeightGridView.EditIndex = e.NewEditIndex;
}
protected void WeightGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string weight;
weight = ((TextBox)WeightGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
WeightGridView.EditIndex = -1;
WeightGridView.DataBind();
}
Should you need any additional code to answer this post, please let me know and I will update the post.
Any help will be appreciated! Thank you!

Gridview findcontrol after dropdownlist event

I want to findcontrol on griview after DDL OnSelectedIndexChanged event. where the target control is on the rowindex where the DDL is located..
here my codes;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList Dummysource = new ArrayList() { "AA", "BB", "CC", "DD" };
if(!IsPostBack )
{
GridView1.DataSource = Dummysource;
GridView1.DataBind();
}
}
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
string valueComponent = (sender as DropDownList).SelectedItem.Value;
Label1.Text = valueComponent;
}
int ddlvalue;
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking whether the Row is Data Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding the Dropdown control.
DropDownList ddlsample = (DropDownList)e.Row.FindControl("ddlsample");
Label ilbldata = (Label)e.Row.FindControl("lbldata");
if (ddlsample != null)
{
switch(ilbldata.Text)
{
case "AA":
ddlvalue = 2;
break;
case "BB":
ddlvalue = 3;
break;
case "CC":
ddlvalue = 4;
break;
case "DD":
ddlvalue = 5;
break;
}
for (int i = 1; i <= ddlvalue; i++ )
{
ddlsample.Items.Add(i.ToString() );
}
}
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridView gv = sender as GridView;
gv = GridView1;
Label foo = gv.SelectedRow.FindControl("lbldata") as Label ;
Label2.Text = foo.Text;
}
the code get the value of the DropDownList selected Item. I'm Wondering on how to get the component value in the gridview. after selectedindexchange event of DDL
I made some visual photo for more clear
http://i1288.photobucket.com/albums/b493/Kasparov1/GridviewDDL_zps3721fb97.png
thanks in advance;
Try this
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Label1.Text = ddl.SelectedItem.Value;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
// Find your control
Control control = row.FindControl("myControl");
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DropDownList drop = GridView1.Controls[0].Controls[0].FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//Find FooterRow Control
DropDownList dT = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = dT.Items[dT.SelectedIndex].ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//DropDownList1 in GridVied
{
//Find FooterRow Control
DropDownList drop = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//find normal DropDownList1
DropDownList drop1 = GridView1.FindControl("DropDownList1") as DropDownList;
string text = drop1.Items[drop1.SelectedIndex].ToString();
}
//ADD list in GRIDVIEW dropdownlist at run time
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");//Gridview DropDownList
ddl.Items.Add("- - Select - -");
ddl.Items.Add(new ListItem("ABCD"));
ddl.Items.Add(new ListItem("EFGH"));
}

radcombobox always selecting top item on postback

I have an edit page where I set the selected index of a radcombobox (rcb_ParentCompany) based on a value returned from the database. However on postback the text in the combobox keeps changing to the top item in the dataset. Any ideas why?
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
BindOperatingNameComboBox(rcb_OperatingName);
BindParentCompanyComboBox(rcb_ParentCompany);
}
}
protected void btn_Edit_Command(object sender, CommandEventArgs e)
{
Client ClientToEdit = ClientController.ViewClient(int.Parse(e.CommandArgument.ToString()));
//Populate Client fields
txt_ClientName.Text = ClientToEdit.ClientName;
rcb_OperatingName.Text = ClientToEdit.OperatingName;
int ParentCompanyIndex = rcb_ParentCompany.FindItemIndexByValue(ClientToEdit.ParentCompanyID.ToString());
rcb_ParentCompany.SelectedIndex = ParentCompanyIndex;
txt_Address1.Text = ClientToEdit.Address1;
txt_Address2.Text = ClientToEdit.Address2;
txt_Country.Text = ClientToEdit.Country;
txt_Region.Text = ClientToEdit.Region;
txt_City.Text = ClientToEdit.City;
txt_PostalCode.Text = ClientToEdit.PostalCode;
txt_ClientNote.Text = ClientToEdit.ClientNote;
tbl_EditServices.Controls.Clear();
PopulateEditClientPanel(ClientToEdit);
btn_SaveChanges.CommandArgument = e.CommandArgument.ToString();
btn_Cancel.CommandArgument = e.CommandArgument.ToString();
}
protected void BindParentCompanyComboBox(RadComboBox ComboBox)
{
DataTable OperatingNames = ClientController.GetExistingClientAndOperatingNames("");
ComboBox.DataTextField = "ClientName";
ComboBox.DataValueField = "ClientID";
ComboBox.DataSource = OperatingNames;
ComboBox.DataBind();
}
protected void rcb_ParentCompany_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
BindParentCompanyComboBox((sender as RadComboBox));
}
Any ideas why?
Yes, because you are doing if(IsPostBack) as opposed to if(!IsPostBack)

How to remove row from gridview on row command event?

I am trying to remove row from gridview by click on ImageButton which is placed inside gridview. I am getting row index but dont know how to remove. My gridview binds from session and i dont want to rebind gridview.
Here is my code:
protected void GVDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Remove"))
{
List<Class1> list = (List<Class1>)Session["value1"];
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gridRow = gvDetail.Rows[index];// (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
((List<Class1>)Session["value1"]).RemoveAt(index);
}
}
If anyone have any idea about this than please help me..
Use the gridview deleterow method.
void GVDetail_RowCommand_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
var id = Int32.Parse(e.CommandArgument);
GVDetail.DeleteRow(id);
}
}
protected void GVPurchaseOrderTax_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
if (dt.Rows.Count > 0)
{
dt.Rows[e.RowIndex].Delete();
GVPurchaseOrderTax.DataSource = dt;
GVPurchaseOrderTax.DataBind();
}
}
}

Resources