Email In Asp.Net using CheckBoxes - c#

i try to send email in asp.net .when admin select checkbox then he/she able to send mail to respective email id and email is also store in database in userss table ..but when i build the code it shows me error
code is
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkAll =
(CheckBox)Repeateremail.HeaderRow.FindControl("chkSelectAll");
if (chkAll.Checked == true)
{
foreach (GridViewRow gvRow in Repeateremail.Items)
{
CheckBox chkSel =
(CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = true;
}
}
else
{
foreach (GridViewRow gvRow in Repeateremail.Items)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = false;
}
}
}
in this line
CheckBox chkAll =
(CheckBox)Repeateremail.HeaderRow.FindControl("chkSelectAll");
it show me error in header
error
'System.Web.UI.WebControls.Repeater' does not contain
a definition for 'HeaderRow' and no extension method 'HeaderRow'
accepting a first argument of type 'System.Web.UI.WebControls.Repeater'
could be found (are you missing a using directive or an assembly
reference?)
where as in html i use like this in header template
<td>
Check
<asp:CheckBox ID="chkSelectAll" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelectAll_CheckedChanged"/>
Send Mail To All ?
</td>
and in item template
<td>
<asp:CheckBox ID="chkSelect" runat="server"/>
</td>

You do not need to use the FindControl() method, because you are handling the click event for the control you want check property values of anyway, try this instead:
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
// Cast the sender to a CheckBox type
CheckBox chkAll = sender as CheckBox;
// The as operator will return null if the cast is not successful,
// so check for null before we try to use it
if(chkAll != null)
{
if (chkAll.Checked == true)
{
foreach (GridViewRow gvRow in Repeateremail.Items)
{
CheckBox chkSel =
(CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = true;
}
}
else
{
foreach (GridViewRow gvRow in Repeateremail.Items)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = false;
}
}
}
}

Related

unable to get gridview textbox value when readonly is true

i have my gridviews itemtemplate tag
<asp:TemplateField HeaderText="TotalTime" SortExpression="TotalTime">
<ItemTemplate>
<asp:TextBox ID="TotalTime" readonly="true" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
on my button click event i am trying to make the textbox readonly property to false and get the value , but it doesnt work for TotalTime textbox
protected void BtnSaveAttendence_Click(object sender, EventArgs e)
{
foreach (GridViewRow Gr in GridView1.Rows)
{
TextBox txt = (TextBox)(Gr.FindControl("TotalTime"));
txt.ReadOnly = false;
}
int Rest = new int();
object[] objAttnd = new object[8];
foreach (GridViewRow GR in GridView1.Rows)
{
objAttnd[0] = 0;
objAttnd[1] = ((Label)GR.FindControl("LblEMPNAME")).Text;
objAttnd[2] = UserInfo.Company.CompanyID;
objAttnd[3] = Common.Convert_MM_DD_YYYY(txtDate1.Text);
objAttnd[4] =((TextBox)GR.FindControl("InTime")).Text;
objAttnd[5] =((TextBox)GR.FindControl("OutTime")).Text;
objAttnd[6] = ((TextBox)GR.FindControl("TotalTime")).Text;
objAttnd[7] = Convert.ToDecimal(((Label)GR.FindControl("LblEmpCode")).Text);
foreach (GridViewRow Gr in GridView1.Rows)
{
TextBox txt = (TextBox)(Gr.FindControl("TotalTime"));
txt.ReadOnly = true;
}
Rest = objAttendance.InsertUpdateAttendenceDetailNew(objAttnd);
}
if (Rest == -1)
{
lblError.Text = "<div class='ErrorMsg'> Attendance details already added for Selected date !!</div>";
}
}
Plz tell me how to get the value
As far as Your code it will read only one value of the Textbox as you are making it Readonly false and reading value in the Foreach loop then inside that Foreach loop itself you are having another Foreach loop that finds the Textbox and makes Readonly true so take that ForEach loop out and alter your code like,
protected void BtnSaveAttendence_Click(object sender, EventArgs e)
{
foreach (GridViewRow Gr in GridView1.Rows)
{
TextBox txt = (TextBox)(Gr.FindControl("TotalTime"));
txt.ReadOnly = false;
}
int Rest = new int();
object[] objAttnd = new object[8];
foreach (GridViewRow GR in GridView1.Rows)
{
objAttnd[0] = 0;
objAttnd[1] = ((Label)GR.FindControl("LblEMPNAME")).Text;
objAttnd[2] = UserInfo.Company.CompanyID;
objAttnd[3] = Common.Convert_MM_DD_YYYY(txtDate1.Text);
objAttnd[4] =((TextBox)GR.FindControl("InTime")).Text;
objAttnd[5] =((TextBox)GR.FindControl("OutTime")).Text;
objAttnd[6] = ((TextBox)GR.FindControl("TotalTime")).Text;
objAttnd[7] = Convert.ToDecimal(((Label)GR.FindControl("LblEmpCode")).Text);
/*ForEach from Here to*/
Rest = objAttendance.InsertUpdateAttendenceDetailNew(objAttnd);
}
/*===>here*/
foreach (GridViewRow Gr in GridView1.Rows)
{
TextBox txt = (TextBox)(Gr.FindControl("TotalTime"));
txt.ReadOnly = true;
}
if (Rest == -1)
{
lblError.Text = "<div class='ErrorMsg'> Attendance details already added for Selected date !!</div>";
}
}
Try adding readonly attribute from code behind like below:
TotalTime.Attributes.Add("readonly", "readonly");
And no need to set readonly to false before reading its value in code behind.

How to work with Page.ClientScript.GetPostBackClientHyperlink in C#

I've a gridview with selected columns and rows. The row consists of Textbox for every column in a row. I need to select a row in order to get the current rowindex which I've done using below code.
protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtotqty.Rows)
{
if (row.RowIndex == gvtotqty.SelectedIndex)
{
Session["rowindex"]=row.RowIndex;
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
}
Everything is working fine where I get my rowindex when debugged but I am not able to type anything in Textbox as it is getting refreshed. I know that
Page.ClientScript.GetPostBackClientHyperlink
calls PostBackEvent. By using this method, how can I type the values in Textboxes?
Try with this line of code
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink((Control)sender, "Select$" + e.Row.RowIndex);
instead of your
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);
if it doesn't work try putting your grid inside an updatePanel and add the trigger for selected index change of your grid
<asp:UpdatePanel ID="uppan1" runat="server">
<ContentTemplate>
//yourGridView here with id=GridView1
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="selectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
and change your selected index change function in this way
protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvtotqty.Rows)
{
if (row.RowIndex == gvtotqty.SelectedIndex)
{
Session["rowindex"]=row.RowIndex;
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
row.Attributes["onclick"] = "";
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + row.RowIndex);
}
}
}
As from your code I understand that you want to select the row and display the row in different color on selectedindex changed. If I am correct you can achieve this using the following method.
Add a button field, or template field, which contains a button with the command name "Select". Command name should be select.
<asp:ButtonField CommandName="Select" Text="Select"/>
Then for the grid view add SelectedRowStyle
<SelectedRowStyle BackColor="#A1DCF2" />
This should already select the row with differnt color. You dont need to write code for gvtotqty_onselectedindexchanged and gvtotqty_onrowdatabound
UPDATE
Based on your comment, since you may have multiple controls on the row, like textbox, link etc. then eventhough click the textbox to enter text, it does the postback and clears the content of the text.
I think you can use JavaScript for selecting row, instead of posting back as below.
<script type="text/javascript">
function selectRow(control) {
//Clear all the currently selected rows. change the scope, if you have multiple tables
$('tr').css("background-color", "#FFFFFF");
// Select the current row
$(control).css("background-color", "#A1DCF2");
}
</script>
Then add this function to the row in your onrowdatabound event
protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = "selectRow(this);";
e.Row.ToolTip = "Click to select this row.";
}
}

Checkbox Checked Event for multiple dynamic Checkboxes

I have an aspx page where I dynamically create checkboxes and add them to a panel from data in a SQL database. I need to have only one checkbox checked at a time. I have tried and googled just about everything but I cannot get the right answer. How would I go about getting that done.
My code for creating the checkboxes
public void CreateTimeSelection()
{
DataSet times = GetTimes();
int i = 0;
foreach (DataTable table in times.Tables)
{
foreach (DataRow row in table.Rows)
{
CheckBox chkTime = new CheckBox();
i += 1;
chkTime.ID = row["Time"].ToString();
chkTime.AutoPostBack = true;
chkTime.Text = row["Time"].ToString();
chkTime.CheckedChanged += new EventHandler(this.CheckChanged_click);
pnlTimeSlots.Controls.Add(chkTime);
pnlTimeSlots.Controls.Add(new LiteralControl("<br />"));
}
}
}
Code for Checkbox event handlers
protected void CheckChanged_click(object sender, EventArgs e)
{
CheckBox chkSelect = (CheckBox)sender;
if (chkSelect.Checked == true)
{
//set other checkboxes to false
}
}

gridview row with javascript

I'm having trouble selecting a row in a gridview, I have javascript code
function SelectRow(row) {
var _selectColor = "#303030";
var _normalColor = "#909090";
var _selectFontSize = "3em";
var _normalFontSize = "2em";
// get all data rows - siblings to current
var _rows = row.parentNode.childNodes;
// deselect all data rows
try {
for (i = 0; i < _rows.length; i++) {
var _firstCell = _rows[i].getElementsByTagName("td")[0];
_firstCell.style.color = _normalColor;
_firstCell.style.fontSize = _normalFontSize;
_firstCell.style.fontWeight = "normal";
}
}
catch (e) { }
// select current row (formatting applied to first cell)
var _selectedRowFirstCell = row.getElementsByTagName("td")[0];
_selectedRowFirstCell.style.color = _selectColor;
_selectedRowFirstCell.style.fontSize = _selectFontSize;
_selectedRowFirstCell.style.fontWeight = "bold";
}
and on the databound event of the grid I have
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow Row in GridView1.Rows){
if (e.Row.RowType == DataControlRowType.DataRow)
{
// javascript function to call on row-click event
e.row.Attributes.Add("onClick", "javascript:void SelectRow(this);");
}
}
}
but this line is giving error
if (e.Row.RowType == DataControlRowType.DataRow) error message: 'System.EventArgs' does not contain a definition for 'row' and no
extension method 'row' accepting a first argument of type
'System.EventArgs' could be found (are you missing a using directive
or an assembly reference?)
Thanks
You are using incorrect event. You use DataBound, but need to use RowDataBound
RowDataBound has GridViewRowEventArgs args, and they have needed way to do it

Select all CheckBoxes in CheckBoxList

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;
}
}
}

Categories