Event on dynamically created checkbox asp.net - c#

I started to programming with asp.net, I have a table with some checkboxes.
The problem is, I can't create static tables, because this action is linked with some parameters. Anyway.. When I click on the First checkbox I want to invert the other checkboxes in this table.
How can I catch this event?
<%# Page Title="Fragebogen generieren" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Generate.aspx.cs" Inherits="MAXXREC.Generate" SmartNavigation="True" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2><br /> <br />
<asp:Panel id="pCustomize" runat="server"></asp:Panel> <br /><br />
<asp:Button id="btnSave" class="btn btn-default" Text="Save" runat="server" OnClick="btnSave_Click"></asp:Button>
</asp:Content>
private bool SelectTheData()
{
dtQuestionBlock = taQuestionBlock.GetData();
try
{
int rows = dtQuestionBlock.Rows.Count;
for (int i = 0; i < rows; i++)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ID = "up" + dtQuestionBlock.Rows[i][1].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dtQuestionBlock.Rows[i][1].ToString();
lbl.CssClass = "h4";
lbl.Attributes.Add("runat", "server");
lbl.Text = dtQuestionBlock.Rows[i][1].ToString();
pCustomize.Controls.Add(lbl);
pCustomize.Controls.Add(new Literal() { ID = "br" + i, Text = "<br /><br />" });
HtmlTable tbl = new HtmlTable();
tbl.Width = "100%";
tbl.Attributes.Add("class", "table");
tbl.ID = "htmltbl" + dtQuestionBlock.Rows[i][1].ToString();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell hcella = new HtmlTableCell();
CheckBox acb = new CheckBox();
acb.ID = "cb" + dtQuestionBlock.Rows[i]["name"].ToString();
//acb.CheckedChanged += new EventHandler(cb_CheckedChanged);
hcella.Width = "30px";
hcella.Controls.Add(acb);
htr.Cells.Add(hcella);
HtmlTableCell hcellf = new HtmlTableCell();
hcellf.InnerText = "Frage";
hcellf.Style.Add("font-weight", "bold");
hcellf.Style.Add("font-size", "15px");
htr.Cells.Add(hcellf);
tbl.Rows.Add(htr);
string cont = dtQuestionBlock.Rows[i]["ID"].ToString();
dtQuestion = taQuestion.GetDataBy1(Convert.ToInt32(cont));
nCountTables = i;
for (int j = 0; j < dtQuestion.Rows.Count; j++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
acb = new CheckBox();
acb.ID = "cb" + dtQuestion.Rows[j]["content"].ToString();
cell.Width = "30px";
cell.Controls.Add(acb);
tr.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dtQuestion.Rows[j]["content"].ToString();
cell.ID = "cell" + j + "_" + dtQuestion.Rows[j]["content"].ToString();
tr.Cells.Add(cell);
tbl.Rows.Add(tr);
}
updatePanel.ContentTemplateContainer.Controls.Add(tbl);
//tbl.Visible = false;
pCustomize.Controls.Add(updatePanel);
pCustomize.Controls.Add(new Literal() { ID = "br" + i + rows, Text = "<br />" });
}
return true;
}
catch (Exception ex)
{
Type cstype = ex.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = ex.ToString();
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
return false;
}
finally
{
taQuestionBlock.Dispose();
dtQuestionBlock.Dispose();
}
}

you can try this code
List<CheckBox> lstChckBox;
protected void Page_Load(object sender, EventArgs e)
{
// you can create controls programaticaly or html page, doesnt important
//only you should know controls ID and all controls share same checked event
CheckBox chc1 = new CheckBox();
chc1.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc2 = new CheckBox();
chc2.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc3 = new CheckBox();
chc3.CheckedChanged += new EventHandler(chck_CheckedChanged);
// Now, you can create a List so event is fired, you can catch which controls checked or not
lstChckBox = new List<CheckBox>();
lstChckBox.Add(chc1);
lstChckBox.Add(chc2);
lstChckBox.Add(chc3);
}
void chck_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (sender as CheckBox);
foreach (CheckBox item in lstChckBox)
{
if (item != checkBox)
{
item.CheckedChanged -= new EventHandler(chck_CheckedChanged);
item.Checked = !checkBox.Checked;
item.CheckedChanged += new EventHandler(chck_CheckedChanged);
}
}
}

Related

Creating Row Dynamically based on selecting dropdownlist

I written a code for creating dynamically table. the table containing textbox and Dropdownlist.
i try to do creating table based on selected dropdowlist value when i select dropdown list like "Splitter1:5" than Table row will creating 5 . first time it has done successfully when i try to second time i did not get the value of dropdown list.
i am sharing my code please help me.
html code:
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlTextBoxes" runat="server">
<asp:ListItem Value="2" Text="Splitter1:2" />
<asp:ListItem Value="4" Text="Splitter1:4" />
<asp:ListItem Value="8" Text="Splitter1:8" />
<asp:ListItem Value="1" Text="Joint" />
<asp:ListItem Value="0" Text="OLT" />
</asp:DropDownList>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /><br />
<asp:Panel ID="container" runat="server" Visible="false"> <asp:Button ID="btnSubmit" runat="server" Text="Process" OnClick="Submit" /></asp:Panel>
</div>
</form>
</body>
behind code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class testform : System.Web.UI.Page
{
DropDownList DDL; int i;
ArrayList arry_value = new ArrayList(); object[] obj;
Panel pn;
protected void Page_Load(object sender, EventArgs e)
{
//ViewState["droplist"] = obj;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value);
// DataTable dt = new DataTable();
//dt.Columns.Add("First Textbox");
// dt.Columns.Add("Dropdownlist");
// dt.Columns.Add("Last Textbox");
var table = new Table();
for (i = 0; i < numOfTxt; i++)
{
var row = new TableRow();
var cell = new TableCell();
// var thr = new TableHeaderRow();
// var heading = new TableHeaderCell();
//TableHeaderCell thPNumber = new TableHeaderCell();
// cell.Attributes.Add("runat", "server");
TextBox textbox = new TextBox();
textbox.ID = "Textbox" + i;
textbox.Text = "text" + i;
textbox.Width = new Unit(180);
TextBox textbox1 = new TextBox();
textbox1.ID = "Textbox1" + i;
textbox1.Text = "text1" + i;
textbox1.Width = new Unit(180);
DDL = new DropDownList();
DDL.ID = "DDL1" + i;
DDL.Items.Add("Select Splitter");
DDL.Items.Add(new ListItem("Splitter1:2", "2"));
DDL.Items.Add(new ListItem("Splitter1:4", "4"));
DDL.Items.Add(new ListItem("Splitter1:8", "8"));
DDL.Items.Add(new ListItem("Joint", "1"));
DDL.Items.Add(new ListItem("ONT", "0"));
DDL.AutoPostBack = true;
DDL.TextChanged += dropDown_TextChanged;
// dt.Rows.Add(textbox1);
cell.Controls.Add(textbox1);
cell.Controls.Add(DDL);
cell.Controls.Add(textbox);
//dt.Rows.Add(DDL);
//string val = DDL.SelectedItem.Value;
// string val = DDL.SelectedValue;
// dt.Rows.Add(textbox);
row.Cells.Add(cell);
// row.Cells.Add(thr);
table.Rows.Add(row);
arry_value.Add(DDL.ID);
foreach (object obj in arry_value)
{
var idcollection = new string[] { obj.ToString() };
}
//for (int i1 = 0; i1 < arry_value.Count; i1++)
//{
// //var empty1 = new string[] {arry_value[i].ToString() };
// obj = new object[] { arry_value[i]};
//}
//string count += (DDL.ID.SelectedItem.Value).toString();
}
pn = new Panel();
pn.ID = "pan" + i + 1;
this.form1.Controls.Add(pn);
container.Visible = true;
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Processone";
btnSubmit.Click += new System.EventHandler(Submit);
container.Controls.Add(table);
//pn.Controls.Add(btnSubmit);
// container.Controls.AddAt(0, table);
}
void dropDown_TextChanged(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
protected void Submit(object sender, EventArgs e)
{
//Student stud = (Student)ViewState["CurrentStudent"];
//DropDownList ddl = (DropDownList)container.FindControl("DDL1"+i);
// int numOfTxt = 4;
int numOfTxt = 5;
string drpvalue;
for (int j = 0; j < Convert.ToInt32(ddlTextBoxes.SelectedItem.Value); j++)
{
drpvalue = "DDL12" + i + ".SelectedItem.Value";
//int te = Convert.ToInt32(drpvalue);
// numOfTxt += Convert.ToInt32(DDL120.SelectedItem.Value);
}
//int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value);
var table = new Table();
DataTable dt = new DataTable();
for (int i = 0; i < numOfTxt; i++)
{
var row = new TableRow();
var cell = new TableCell();
// dt.Columns.Add("TextOne1");
cell.Attributes.Add("runat", "server");
TextBox textbox = new TextBox();
textbox.ID = "Textbox" + i;
textbox.Text = "text" + i;
textbox.Width = new Unit(180);
TextBox textbox1 = new TextBox();
textbox1.ID = "Textbox1" + i;
textbox1.Text = textbox1.Text;
textbox1.Width = new Unit(180);
DropDownList DDLs = new DropDownList();
DDLs.ID = "DDL12" + i;
// count= Convert.ToInt32( DDL.ID = "DDL12" + i);
DDLs.Items.Add("Select Splitter");
DDLs.Items.Add(new ListItem("Splitter1:2", "2"));
DDLs.Items.Add(new ListItem("Splitter1:4", "4"));
DDLs.Items.Add(new ListItem("Splitter1:8", "8"));
DDLs.Items.Add(new ListItem("Joint", "1"));
DDLs.Items.Add(new ListItem("ONT", "0"));
//DDLs.AutoPostBack = true;
DDLs.TextChanged += dropDown_TextChanged;
// dt.Rows.Add(textbox1);
cell.Controls.Add(textbox1);
// dt.Columns.Add("ListDropdown");
cell.Controls.Add(DDLs);
// dt.Columns.Add("Textbodx");
cell.Controls.Add(textbox);
row.Cells.Add(cell);
table.Rows.Add(row);
}
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Process";
btnSubmit.Click += new System.EventHandler(Submit);
Panel pans = new Panel();
pans.ID = "panss";
this.form1.Controls.Add(pans);
pans.Controls.AddAt(0, table);
pans.Controls.Add(btnSubmit);
pans.Visible = true;
//pn.Visible = true;
}
}

How to update different view separately in a multiview control

I created a multiview control with different views. Each views have separate edit buttons which should update the fields only in that view. The edit button is working properly on the first view/tab. But not in the others. I have used update panels in different tables in different views. Still not working. Blow is the code.
Found out hat the code is not able to find any control inside the table content. But why?
<asp:Button Text="Information" BorderStyle="None" ID="Tab1" CssClass="Initial" runat="server" OnClick="Tab1_Click" />
<asp:Button Text="IP Adresses" BorderStyle="None" ID="Tab2" CssClass="Initial" runat="server" OnClick="Tab2_Click" />
<asp:MultiView ID="MainView" runat="server">
<asp:View ID="View1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div1" runat="server" >
<table id="tableContentInfo" style="width: 80%;" runat="server"></table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div2" runat="server" >
<table id="tableContentMake_Model" style="width: 100%;" runat="server"></table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
</asp:MultiView>
This is the code behind.. the controls inside the views are created dynamically and they are showing properly with data. tableContentMake_Model is the second view id.
string strServer = "";
protected void Page_Init(object sender, EventArgs e)
{
GetDataFromSession();
lblName.Text = strServer;
LoadViewInfo(strServer);
LoadViewMake_Model(strServer);
}
protected void Page_Load(object sender, EventArgs e)
{
Tab1.CssClass = "Clicked";
Tab2.CssClass = "Initial";
Tab3.CssClass = "Initial";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 0;
}
protected void Tab1_Click(object sender, EventArgs e)
{
Tab1.CssClass = "Clicked";
Tab2.CssClass = "Initial";
Tab3.CssClass = "Initial";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 0;
GetDataFromSession();
LoadViewInfo(strServer);
}
protected void Tab2_Click(object sender, EventArgs e)
{
Tab1.CssClass = "Initial";
Tab2.CssClass = "Clicked";
Tab3.CssClass = "Initial";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 1;
GetDataFromSession();
LoadViewMake_Model(strServer);
}
private void button_Edit_MakeModel_Click(object sender, EventArgs e)
{
foreach (HtmlTableRow row in tableContentMake_Model.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
foreach (Control ctrl in cell.Controls)
{
if (ctrl.GetType() != typeof(LiteralControl))
{
if (ctrl is HtmlInputCheckBox)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)ctrl;
chk.Disabled = false;
}
if (ctrl is HtmlInputText)
{
HtmlInputText txt = (HtmlInputText)ctrl;
txt.Attributes.Remove("readonly");
txt.Style.Add("border", "1px solid #DBE0E4");
}
if (ctrl is HtmlTextArea)
{
HtmlTextArea txtarea = (HtmlTextArea)ctrl;
txtarea.Attributes.Remove("readonly");
txtarea.Style.Add("border", "1px solid #DBE0E4");
}
}
}
}
}
Button btnEdit = (Button)tableContentMake_Model.FindControl("Edit_MakeModel");
btnEdit.Visible = false;
Button btnSave = (Button)tableContentMake_Model.FindControl("Save_MakeModel");
btnSave.Visible = true;
Button btnCancel = (Button)tableContentMake_Model.FindControl("Cancel_MakeModel");
btnCancel.Visible = true;
}
Thanks in advance for help.
This is the creation of button code..
int ct = 0;
int nullct = 0;
string fields = "";
string strControl = "";
tableContentMake_Model.Rows.Clear();
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["WebTeamServersConnectionString"].ConnectionString);
string sCommand = "select * from server_List_Choices where ListHeading='Make_Model'";
SqlCommand command = new SqlCommand(sCommand, cn);
cn.Open();
SqlDataReader reader = command.ExecuteReader();
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
while (reader.Read())
{
if (ct == 0)
{
cell.InnerText = reader.GetValue(3).ToString().Trim();
cell.Width = "50px";
cell.Style.Add("font-weight", "bold");
cell.Style.Add("color", "#69be28");
row.Cells.Add(cell);
fields = reader.GetValue(2).ToString().Trim();
strControl = reader.GetValue(4).ToString().Trim();
}
else
{
cell = new HtmlTableCell();
cell.InnerText = reader.GetValue(3).ToString().Trim();
if (reader.GetValue(3).ToString().Trim() == "Operating System")
{
cell.Width = "150px";
}
else
{
cell.Width = "40px";
}
cell.Style.Add("font-weight", "bold");
cell.Style.Add("color", "#69be28");
row.Cells.Add(cell);
fields = fields + "," + reader.GetValue(2).ToString().Trim();
strControl = strControl + "," + reader.GetValue(4).ToString().Trim();
}
ct = ct + 1;
}
tableContentMake_Model.Rows.Add(row);
cn.Close();
SqlConnection cn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["WebTeamServersConnectionString"].ConnectionString);
string sCommand1 = "select " + fields + " from [Web Team Servers] where Server='" + server + "'";
SqlCommand command1 = new SqlCommand(sCommand1, cn1);
cn1.Open();
SqlDataReader reader1 = command1.ExecuteReader();
row = new HtmlTableRow();
while (reader1.Read())
{
for (int i = 0; i < reader1.VisibleFieldCount; i++)
{
cell = new HtmlTableCell();
if ((reader1.GetValue(i).ToString().Trim() == "") || (reader1.GetValue(i).ToString().Trim() == null))
{
nullct = nullct + 1;
}
string[] s = strControl.Split(',');
string p = s[i];
switch (p)
{
case "chk":
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
if (reader1.GetValue(i).ToString().Trim() == "Y")
{
checkbox.Checked = true;
}
else
{
checkbox.Checked = false;
}
checkbox.Disabled = true;
cell.Controls.Add(checkbox);
row.Cells.Add(cell);
break;
case "txt":
string valuetxt = reader1.GetValue(i).ToString().Trim();
HtmlInputText textbox = new HtmlInputText();
textbox.Value = valuetxt;
textbox.ID = "txt" + reader1.GetName(i).ToString().Trim();
textbox.Attributes.Add("readonly", "readonly");
textbox.Style.Add("border", "none");
if (reader1.GetName(i).ToString().Trim() == "OperatingSystem")
{
textbox.Style.Add("width", "300px");
}
else
{
textbox.Style.Add("width", "100px");
}
cell.Controls.Add(textbox);
break;
case "txtarea":
string value = reader1.GetValue(i).ToString().Trim();
value = value.Replace("\"", "");
value = value.Replace("\n", "");
value = value.Replace("\r", "");
value = value.Replace("~", "\n");
HtmlTextArea testarea = new HtmlTextArea();
testarea.Value = value;
testarea.ID = "txtarea" + reader1.GetName(i).ToString().Trim();
testarea.Attributes.Add("readonly", "readonly");
testarea.Style.Add("border", "none");
testarea.Style.Add("height", "50px");
cell.Controls.Add(testarea);
cell.Width = "30px";
break;
default :
break;
}
cell.Style.Add("color", "black");
row.Cells.Add(cell);
}
if (nullct < reader1.VisibleFieldCount)
{
cell = new HtmlTableCell();
Button button = new Button();
button.Text = "Edit";
button.ID = "Edit_MakeModel";
button.Click += new EventHandler(button_Edit_MakeModel_Click);
cell.Controls.Add(button);
cell.Width = "50px";
row.Cells.Add(cell);
cell = new HtmlTableCell();
Button button1 = new Button();
button1.Text = "Save";
button1.ID = "Save_MakeModel";
button1.Visible = false;
button1.Click += new EventHandler(button_Save_MakeModel_Click);
cell.Controls.Add(button1);
cell.Width = "50px";
row.Cells.Add(cell);
cell = new HtmlTableCell();
Button button2 = new Button();
button2.Text = "Cancel";
button2.ID = "Cancel_MakeModel";
button2.Visible = false;
button2.Click += new EventHandler(button_Cancel_MakeModel_Click);
cell.Controls.Add(button2);
cell.Width = "50px";
row.Cells.Add(cell);
}
tableContentMake_Model.Rows.Add(row);
}
cn1.Close();
I think you can have one Update panel for all the views in common that is above the multi view control.I usally use in that way.Hope that can give the hint to your problem.
Please let me know if your problem solved.
Thanks
Solved the issue...
protected void Page_Load(object sender, EventArgs e)
{
switch (strClicked)
{
case "Tab1" :
Tab1.CssClass = "Clicked";
Tab2.CssClass = "Initial";
Tab3.CssClass = "Initial";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 0;
break;
case "Tab2" :
Tab1.CssClass = "Initial";
Tab2.CssClass = "Clicked";
Tab3.CssClass = "Initial";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 1;
break;
case "Tab3" :
Tab1.CssClass = "Initial";
Tab2.CssClass = "Initial";
Tab3.CssClass = "Clicked";
Tab4.CssClass = "Initial";
Tab5.CssClass = "Initial";
Tab6.CssClass = "Initial";
MainView.ActiveViewIndex = 2;
break;
default :
break;
}
}

Dynamically created and displayed jagged array of Textboxes loses its state after event handler is called.

I am trying to create a form for courses provided by a University. Based on the no of subjects obtained through a query string, a form gets generated. If the subject has electives, the user has to check a checkbox, which further generates a no of textboxes for subject names. But whenever I check the checkbox of say 2nd subject the textboxes previously displayed for 1st subject disappear.
ASP CODE
<html>
<body>
<form id="form1" runat="server">
<div>
</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;
public partial class Demo : System.Web.UI.Page
{
#region variables
Label[] lb_subject_names;
TextBox[] tb_subject_names;
Label[] lb_elective;
Table[] table_electives;
CheckBox[] cb_elective;
TextBox[] tb_no_of_elective;
Button[] bt_electives_ok;
TextBox[][] tb_name_of_electives;
TextBox[][] tb_code_of_electives;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
int no_subjects = 3;// int.Parse(Request.QueryString["NoSubjects"]);
//declare
lb_subject_names = new Label[no_subjects];
tb_subject_names = new TextBox[no_subjects];
lb_elective = new Label[no_subjects];
table_electives = new Table[no_subjects];
cb_elective = new CheckBox[no_subjects];
tb_no_of_elective = new TextBox[no_subjects];
bt_electives_ok = new Button[no_subjects];
tb_name_of_electives = new TextBox[no_subjects][];
tb_code_of_electives = new TextBox[no_subjects][];
//initialize
for (int i = 0; i < no_subjects; i++)
{
lb_subject_names[i] = new Label();
tb_subject_names[i] = new TextBox();
lb_elective[i] = new Label();
table_electives[i] = new Table();
table_electives[i].BorderStyle = BorderStyle.Solid;
tb_no_of_elective[i] = new TextBox();
cb_elective[i] = new CheckBox();
cb_elective[i].AutoPostBack = true;
cb_elective[i].ID = (i).ToString();
cb_elective[i].CheckedChanged += new EventHandler(cb_elective_CheckedChanged);
bt_electives_ok[i] = new Button();
bt_electives_ok[i].Text = "OK";
bt_electives_ok[i].ID = "bt_" + (i).ToString();
bt_electives_ok[i].Click += new EventHandler(bt_electives_ok_clicked);
}
//display in form1
for (int i = 0; i < no_subjects; i++)
{
lb_subject_names[i].Text = "<br/>Subject Name : ";
form1.Controls.Add(lb_subject_names[i]);
form1.Controls.Add(tb_subject_names[i]);
lb_elective[i].Text = "Does the Subject provide Electives ?";
form1.Controls.Add(lb_elective[i]);
form1.Controls.Add(cb_elective[i]);
tb_no_of_elective[i].Visible = false;
bt_electives_ok[i].Visible = false;
form1.Controls.Add(tb_no_of_elective[i]);
form1.Controls.Add(bt_electives_ok[i]);
form1.Controls.Add(table_electives[i]);
}
}
protected void cb_elective_CheckedChanged(object sender, EventArgs e)
{
CheckBox currentCheckbox = sender as CheckBox;
int id = int.Parse(currentCheckbox.ID);
if (currentCheckbox != null && currentCheckbox.Checked)
{
tb_no_of_elective[id].Visible = true;
bt_electives_ok[id].Visible = true;
table_electives[id].Visible = true;
}
else
{
tb_no_of_elective[id].Visible = false;
bt_electives_ok[id].Visible = false;
table_electives[id].Visible = false;
}
}
protected void bt_electives_ok_clicked(object sender, EventArgs e)
{
Button button = sender as Button;
String str_id = button.ID;
int id = int.Parse(str_id.Substring(3, str_id.Length - 3));
int no_of_electives = int.Parse(tb_no_of_elective[id].Text);
tb_name_of_electives[id] = new TextBox[no_of_electives];
tb_code_of_electives[id] = new TextBox[no_of_electives];
for (int i = 0; i < no_of_electives; i++)
{
tb_name_of_electives[id][i] = new TextBox();
tb_code_of_electives[id][i] = new TextBox();
}
//row1 header
TableRow e_row1 = new TableRow();
TableCell e_cell1_1 = new TableCell();
e_cell1_1.Text = "Sr. No.";
e_row1.Controls.Add(e_cell1_1);
TableCell e_cell1_2 = new TableCell();
e_cell1_2.Text = "Subject Code";
e_row1.Controls.Add(e_cell1_2);
TableCell e_cell1_3 = new TableCell();
e_cell1_3.Text = "Subject Name";
e_row1.Controls.Add(e_cell1_3);
table_electives[id].Controls.Add(e_row1);
for (int i = 0; i < no_of_electives; i++)
{
//row2
TableRow e_row2 = new TableRow();
TableCell e_cell2_1 = new TableCell();
e_cell2_1.Text = (i + 1).ToString();
e_row2.Controls.Add(e_cell2_1);
TableCell e_cell2_2 = new TableCell();
e_cell2_2.Controls.Add(tb_code_of_electives[id][i]);
e_row2.Controls.Add(e_cell2_2);
TableCell e_cell2_3 = new TableCell();
e_cell2_3.Controls.Add(tb_name_of_electives[id][i]);
e_row2.Controls.Add(e_cell2_3);
table_electives[id].Controls.Add(e_row2);
}
}
}

sending data to event handler when the event is called programmatically

protected void Page_Load(object sender, EventArgs e)
{
Team T = new Team();
string[] TLeaders = T.GetAllTeamLeaders(Session["USER_EMAIL"].ToString(), Session["ProjectID"].ToString());
for (int i = 0; i < TLeaders.Length; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.Style.Add(HtmlTextWriterStyle.BorderStyle, "Solid");
createDiv.Style.Add(HtmlTextWriterStyle.BorderColor, "lightblue");
createDiv.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
createDiv.Style.Add(HtmlTextWriterStyle.Height, "100px");
createDiv.Style.Add(HtmlTextWriterStyle.Width, "1350px");
createDiv.Style.Add(HtmlTextWriterStyle.MarginTop, "20px");
createDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
DivRate.Controls.Add(createDiv);
Label LeaderName = new Label();
LeaderName.Text = TLeaders[i];
LeaderName.Style.Add("margin-left", "600px");
LeaderName.Style.Add("color","gray");
LeaderName.Style.Add("font-size","20px;");
LeaderName.Style.Add("margin-top", "10px");
LeaderName.Style.Add("position", "absolute");
createDiv.Controls.Add(LeaderName);
RadioButtonList Rate = new RadioButtonList();
ListItem bad = new ListItem();
ListItem fair = new ListItem();
ListItem good= new ListItem();
ListItem veryGood = new ListItem();
ListItem excellent = new ListItem();
bad.Value = "1";
bad.Text = "Bad";
fair.Value = "2";
fair.Text = "Fair";
good.Value = "3";
good.Text = "Good";
veryGood.Value = "4";
veryGood.Text = "Very Good";
excellent.Value = "5";
excellent.Text = "Excellent";
Rate.AutoPostBack = true;
Rate.Items.Add(bad);
Rate.Items.Add(fair);
Rate.Items.Add(good);
Rate.Items.Add(veryGood);
Rate.Items.Add(excellent);
Rate.Attributes.Add("onchange", "return LeaderName('"+TLeaders[i]+"');");
Rate.SelectedIndexChanged += new EventHandler(CheckChange("s"));
Rate.RepeatColumns=5;
Rate.Width = 10;
Rate.Height = 10;
Rate.CssClass = "RateClass";
createDiv.Controls.Add(Rate);
}
}
so in this code I create a div and I place inside it a label and a Radio Button List however what i want to do is to create an event handler and send a data to it when i call it i call it like this:
Rate.SelectedIndexChanged += new EventHandler(CheckChange("s"));
and this is the event handler:
protected void CheckChange(string s)
{
ScriptManager.RegisterStartupScript(Page, typeof(Page), "", "fun('" + s + "')", true);
}
It give me an error "method name expected" when I send the data like this
any solution?

Enable Textbox on SelectedIndex change

I am grabbing some records from a database depending on the id number of the page, the amount of records that will display will vary, anywhere to 1 record to 50. I need to take these records then choose which ones I will be modifying, deleting, or won't change at all. I would like it to look like this:
Can't get image to show with stackoverflow image uploader so here: https://dl.dropboxusercontent.com/u/9446763/code/dropdownlist.jpg
I would like the text fields to be disabled if No Change or Remove is selected, and the fields enabled for revising if the option Modify is selected.
Below is what I have so far, the part I am struggling on is the selectedindex change I do not know how to code it so that when Modify is selected in the dropdown box the appropriate textboxes become enabled.
ASPX Page
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
CS Page
private void createControls()
{
var id = Request.Params["ID"];
System.Data.OleDb.OleDbConnection pcn;
System.Data.OleDb.OleDbCommand pcm;
System.Data.OleDb.OleDbDataReader prs;
pcn = new System.Data.OleDb.OleDbConnection("");
pcm = new System.Data.OleDb.OleDbCommand();
pcn.Open();
pcm.Connection = pcn;
var tableSql = #"select * FROM grouplist where ptid = '" + id + "'";
pcm.CommandText = tableSql;
prs = pcm.ExecuteReader();
var rowcount = 0;
while (prs.Read())
{
rowcount++;
PlaceHolder1.Controls.Add(new Literal() { Text = "<div class='row'><div class='span3'>" });
TextBox tx = new TextBox();
tx.ID = "txtData" + rowcount.ToString();
tx.Text = prs["name"].ToString().Trim();
tx.Width = 200;
tx.CssClass = "span2";
tx.Enabled = false;
PlaceHolder1.Controls.Add(tx);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span2'>" });
TextBox txa = new TextBox();
txa.ID = "amtData" + rowcount.ToString();
txa.Text = prs["amt"].ToString();
txa.CssClass = "span2";
txa.Enabled = false;
PlaceHolder1.Controls.Add(txa);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span3'>" });
DropDownList ddl = new DropDownList();
ddl.Items.Add("No Change");
ddl.Items.Add("Modify");
ddl.Items.Add("Remove");
ddl.Width = 200;
ddl.CssClass = "span2";
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
ddl.AutoPostBack = true;
ddl.ID = "ddlData" + rowcount.ToString();
PlaceHolder1.Controls.Add(ddl);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div></div>" });
}
prs.Close();
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
}
You need to have members in your class that hold references to your DropDownList and other controls that you want to enable/disable.
public class YourClass {
private DropDownList ddl;
private TestBox txa;
private void createControls {
// ...
TextBox txa = new TextBox();
txa.ID = "amtData" + rowcount.ToString();
// ...
DropDownList ddl = new DropDownList();
ddl.Items.Add("No Change");
// ... etc.
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedIndex == 1)
txa.Enabled = true;
}
}

Categories