i have CheckChanged event behind Checkbox, it is called whether i tick or un-tick checkbox but i only wat to call this event when check box is checked not on uncheck.
code:
protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
{
if (txtSelectedID.Text != "")
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
HiddenFieldShowHideButtons.Value = "True";
}
}
else
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
}
}
}
This alternate might work
<asp:CheckBox ID="CheckBox1" runat="server" Text="Check"
AutoPostBack="True" OnClick="return chkSelected();"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<script type="text/javascript">
function chkSelected() {
var chk = document.getElementById('<%= CheckBox1.ClientID %>');
if (chk.checked) {
__doPostBack('<%= CheckBox1.ClientID %>', '');
} else {
return false;
}
}
</script>
You can use an if condition to check the state of the Checked property of the CheckBox control. As the following:
protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
{
if (chkIsHead.Checked)
{
// put your code here
}
}
Related
I am trying to reference a non-asp check box in C# code behind. The reason the checkbox is not an asp element, is it gets auto-generated on the fly, rather than being a part of the website. So far I have the following relevant aspx:
<asp:Table ID="myTable" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell>A</asp:TableCell>
<asp:TableCell>B</asp:TableCell>
<asp:TableCell>C</asp:TableCell>
<asp:TableCell>D</asp:TableCell>
<asp:TableCell>E</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:LinkButton runat="server" ID="TEST" CssClass="btn btn-default pull-right" OnClick="TEST_Click">
TEST <i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>
And the C# code behind is:
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
if (check)
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" >";
}
else
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" checked>";
}
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chkbox = (HtmlInputCheckBox)FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
MessageBox.Show("NOTHING :(");
}
chkbox is always null :(.
You'll need to change two things.
In order to find a checkbox via FindControl it must be part of the pages control collection, which means you have to add a CheckBoxcontrol.
CheckBox c = new CheckBox { ID = "chk" + i };
tempCell.Controls.Add(c);
The dynamically added CheckBox control is part of the control collection of the Table, so you'll have to search for it there instead of on the page.
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
Below you find a full update of your code.
protected void Page_Load(object sender, EventArgs e)
{
GenerateTable();
}
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
CheckBox c = new CheckBox { ID = "chk" + i };
c.Checked = check;
tempCell.Controls.Add(c);
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
{
MessageBox.Show("NOTHING :(");
}
}
This is the script and I want to use it in code behind previously I used clentclick property of button button I want to use this code without using button
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
what else can I do so that it is achievable for me
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
// TextBox1.Attributes.Add("OnClientClick", "Confirm()");
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
public void OnConfirm(object sender, EventArgs e)
{
}
Leave a button on the page, hide it using css and then call click() from JavaScript after you have set your value:
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
document.getElementById("Button1").click();
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onchange="Confirm()"></asp:TextBox>
<div style="display:none;"><asp:Button ID="Button1" ClientIDMode="static" runat="server" onclick="Button1_Clicked" /></div>
Then in your codebehind:
protected void Button1_Clicked(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
I am creating an alert and trying to call a click event through javascript function when "OK" of alert is pressed.It runs pretty well if I create the alert on rpage_Load but When I crate the alert on clicking of a buttton, then on pressing "OK" of alert the required click event is not called.
This is how I create the alert
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", "Test();", true);
}
This is the javascript function which calls a click event
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('btnReview').click();
}
</script>
This is the click event which will be called through Test()
protected void btnReview_Click(object sender, EventArgs e)
{
count = int.Parse((string)ViewState["S.NO"]);
dt1 = (DataTable)ViewState["Question"];
if (rbOption.SelectedValue != "")
{
string strUserOpt = rbOption.SelectedItem.Text;
strUserOpt = strUserOpt.Substring(20);
dt1.Rows[count - 1][9] = strUserOpt;
dt1.Rows[count - 1][10] = rbOption.SelectedValue;
}
lblReview.Visible = true;
tblQues.Visible = false;
tblReview.Visible = true;
btnBookMark.Text = "Bookmark";
btnBookMark.Font.Bold = false;
btnBookMark.BackColor = Color.Empty;
lblQuestionNo.Visible = false;
lblTopic.Visible = false;
lblTestHead.Visible = false;
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Status");
dt.Columns.Add("BookMarked");
DataRow dr1;
foreach (DataRow dr in dt1.Rows)
{
dr1 = dt.NewRow();
dr1[0] = dr[0].ToString() ;
if (dr[9].ToString() != "") { dr1[1] = "Attempted"; } else { dr1[1] = "Un-attempted"; }
if (dr[11].ToString() != "") { dr1[2] = "Yes"; } else { dr1[2] = "No"; }
dt.Rows.Add(dr1);
}
dt.AcceptChanges();
ClsDataBind.DoGridViewBind(grdReview, dt, _errMsg);
btnBookMark.Visible = false;
btnNext.Visible = false;
btnPrevious.Visible = false;
btnReview.Visible = false;
}
The main problem may be you are clicking Button1 after btnReview because under btnReview_Click
this happens
btnReview.Visible = false;
This means you will not be able to use Button1_Click event unless
btnReview.Visible = true;
protected void Page_Load(object sender, EventArgs e)
{
Button cmdTemp = null;
try
{
cmdTemp = (Button)GetPostBackControl(this);
}
catch { }
FillTableDB();
if(IsPostBack)
{
if(cmdTemp == null || cmdTemp.ID == "btnNew" || cmdTemp.ID != "btnSave")
{
GenerateBlankTableHtml("");
}
}
}
private void FillTableDB()
{
//SQL QUERY
//Select status from table
GenerateBlankTableHtml(status)
}
private void GenerateBlankTableHtml(string status)
{
if(status=="")
{
btnNew.Style.Add("Display", "none");
}
else
{
//show status in label
lblStatus.text=status;
}
}
public static Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if(ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach(string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if(c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}
ASPX:
<asp:Button ID="btnSave" runat="server"/>
<asp:Button ID="btnNew" runat="server"/>
<asp:Label ID="lblStatus" runat="server"
I have two functions FillTableDB();GenerateBlankTableHtml(string status);
When status getting blank i have to hide btnNew otherwise showing status in label.
if label having status then and only then New study botton will displayed otherwise not.
What i want when user click on button NEW then and only then i have to show label text with blank status Not click on save button What should i do.
Try this
if(IsPostBack)
{
if(btnNew.Style.Value == "Display:none;")
{
GenerateBlankTableHtml("");
}
}
protected void btnNew_Click(object sender, EventArgs e)
{
GenerateBlankTableHtml("");
}
do something like following.
<asp:Button ID="btnNew" runat="server" onClick="btnNew_click"/>
and now on that button new click.
protected void btnNew_Click(object sender, EventArgs e)
{
Button btnNew = (Button)sender;
btnNew.Style.Add("Display", "none");
lblStatus.text = string.empty;
}
I have a CheckBox and a CheckBox list on my web page.
If the CheckBox is selected, all the CheckBoxes in the CheckBoxList should get selected, and if the CheckBox is unchecked, similarly all the CheckBoxes in the CheckBox should get deselected (unchecked).
.aspx code
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>Item A</asp:ListItem>
<asp:ListItem>Item B</asp:ListItem>
<asp:ListItem>Item C</asp:ListItem>
<asp:ListItem Selected="True">Item D</asp:ListItem>
<asp:ListItem>Item E</asp:ListItem>
<asp:ListItem>Item F</asp:ListItem>
<asp:ListItem>Item G</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBox ID="allChkBox" Text="Select all" runat="server"
oncheckedchanged="allChkBox_CheckedChanged" />
I tried by doing somehting like this, but it didb't work:
bool prevSelection = false;
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
if (!prevSelection)
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = true;
}
}
else
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = false;
}
}
prevSelection = !prevSelection;
}
I prefer to use client script for something like this so your page doesnt have to do a postback
If that is a possibility try firing a javascript function on click to do the looping and selecting ... something like
<script type="text/javascript">
checked=false;
function checkedAll (frm1) {
var aa= document.getElementById('frm1');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
if(aa.elements[i].type == 'checkbox') {
aa.elements[i].checked = checked;
}
}
}
</script>
It's been a while since I've dabbled in ASP.NET, but your prevSelection field will be initialized to false on each and every request. That value will not be persisted between requests. So, you either need to store it in View State or the cache and load it from there in your event handler, or, even better, change your method to something like this:
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = allChkBox.Selected;
}
}
How about this Iif I have understood the requirement right!)? This will make all items selected in a CheckBoxList control by default when it renders:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
LoadCountryList();
}
private void LoadCountryList()
{
_ctx = new PayLinxDataContext();
chkCountries.DataSource = _ctx.Countries.OrderBy(c => c.Name);
chkCountries.DataBind();
foreach (ListItem item in chkCountries.Items)
{
item.Selected = true;
}
}
Instead of using a variable outside the function, how about using the checkbox itself:
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkbox = sender;
foreach (ListItem chkitem in CheckBoxList1.Items)
{
chkitem.Selected = chkbox.Selected;
}
}
you can do it with linq like this
var allChecked = (from ListItem item in CheckBoxList1.Items
where item.Selected
select int.Parse(item.Value)).ToList();
var all = (from ListItem item in CheckBoxList1.Items
select int.Parse(item.Value)).ToList();
function CheckUnCheckAll()
{
var list = document.getElementById("<%=DataList1.ClientID%>") ;
var chklist = list.getElementsByTagName("input");
for (var i=0;i<chklist.length;i++)
{
if (chklist[i].type=="checkbox" )
{
chklist[i].checked = checkoruncheck;
}
}
}