I have a listview with checkbox. According to my requirement I uncheck through javascript. But because of that script my Checkchanged event fires twice and returns the previous unchecked(through javascript) value on second time fires.
Also usually any operation check box triggers an event. But if you check the same item which is unchecked through javascript just before is not fires the checkedchanged event.
I am not sure why is it happening when using script.
Please find the code below
JavaScript
function CallConfirmBox() {
alert("456");
if (confirm('Schedule more than one time slot for the same day will overwrite the file')) {
return true;
}
else {
var id = document.getElementById('<%= hdnValue.ClientID%>').value;
alert(id);
$('#' + id).attr('checked', false);
alert("123")
id = "";
return false;
}
}
CodeBehind
protected void chkCheck_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkCheck = (CheckBox)sender;
ListViewItem item = (ListViewItem)chkCheck.NamingContainer;
ListViewDataItem dataItem = (ListViewDataItem)item;
string lookupId = lvLookup.DataKeys[dataItem.DisplayIndex].Value.ToString();
hdnValue.Value = chkCheck.ClientID;
if (lookupMstVal == "ScheduledTime." && lbCheckedIdList.Items.Count > 0 && chkCheck.Checked)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "CallConfirmBox", "CallConfirmBox();", true);//" + chkCheck.ClientID + "
}
if (chkCheck.Checked)
lbCheckedIdList.Items.Add(lookupId);
else
lbCheckedIdList.Items.Remove(lookupId);
hdfLookupId.Value = "";
foreach (ListItem itm in lbCheckedIdList.Items)
{
hdfLookupId.Value += (hdfLookupId.Value == "" ? "" : ",") + itm.Value;
}
postbackFlag = true;
}
Please Help! Thanks.
Related
It is possible to call a event click with JavaScript? how?
I'm trying to call this event when a button get clicked.
I'm creating Buttons dynamically so the id's change constantly
Here is how i make the buttons dynamically and assign the event click
protected void Page_Init(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
Button Btn_clic = (Button)sender;
var name = Btn_clic.Text;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
List<Button> Botones = new List<Button>();
var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamFCH)
{
Button newButton = new Button();
newButton.CommandName = "Btn" + Convert.ToString(team);
newButton.ID = "Btn_" + Convert.ToString(team);
newButton.Text = team;
newButton.CommandArgument = name;
newButton.Click += new System.EventHandler(newButton_Click);
Botones.Add(newButton);
GoodPanel.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
protected void newButton_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
Button Btnclick = (Button)sender;
var team = Btnclick.Text;
string name = Btnclick.CommandArgument;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == name && x.TEAM == team && x.STANDBY == 0).ToList();
var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
Grv_Eng.DataSource = ListOfToolsOk;
Grv_Eng.DataBind();
}
If you want to assign a OnClick event, do it like this.
Button Btnclick = new Button();
Btnclick.Click += newButton_Click;
Btnclick.Text = "MyButton";
Btnclick.ID = "MyButtonID";
PlaceHolder1.Controls.Add(Btnclick);
And if you want to reference the dynamic ID, use FindControl and ClientID on the aspx page.
document.getElementById("<%= PlaceHolder1.FindControl("MyButtonID").ClientID %>").click
Assign an onClick listener to your button.
document.getElementById("your-id").click = function () {
newButton_Click();
}
Here you go try this for dynamically created buttons
$(document).on('click', '#id', function(){});
You can use a data- attribute to id the buttons from javascript and then you just attach to the javascript event:
So, from the server side you can do this:
newButton.Attributes["data-dynamic-button"] = team;
And you can implement this on the client side:
$("[data-dynamic-button]").click(function (event) {
event.preventDefault()
alert($(event.currentTarget).data("dynamic-button"));
});
I`m making a board and now I want to edit item when check box checked.
But, as you know check box can check any check box.
In my rule I want to edit just one item.
So I want to display alert message when multiple selection of checkbox.
How can I do that?? I don`t want to disable it.
Because when delete item I`m using multiple selection of checkbox.
My code is below.
protected void lnkbtnEdit_Click(object sender, EventArgs e)
{
string editPageUrl = string.Empty;
foreach (GridViewRow gRow in grvList.Rows)
{
CheckBox chkbox = (CheckBox)gRow.FindControl("chk");
if (chkbox.Checked)
{
int id = Convert.ToInt32(gRow.Cells[1].Text);
editPageUrl = "http://ycchoi/sites/dev/_layouts/15/ListItemControl/EditListItem.aspx?ID=" + id;
Response.Redirect(editPageUrl);
}
else
{
string message = #"
<script type='text/javascript'>
alert('Please select item to Edit');
</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "script", message);
}
}
}
I'd keep a variable outside of the foreach loop, storing the ID of the rows that are checked, then look at the length of that list to see if it's equal to 1 before allowing the user to edit it. This is how I approached it in a similar interface (where a user can delete/hide/highlight multiple things, but only reply to one at a time.)
List<int> ids = new List<int>();
foreach (GridViewRow gRow in grvList.Rows)
{
CheckBox chkbox = (CheckBox)gRow.FindControl("chk");
if (chkbox.Checked)
{
int id = Convert.ToInt32(gRow.Cells[1].Text);
editPageUrl = "http://ycchoi/sites/dev/_layouts/15/ListItemControl/EditListItem.aspx?ID=" + id;
ids.Add(id);
}
}
if (ids.Count == 1)
{
// do something with ids[0]
}
else
{
// show error
}
In my entity (A) has 50 option set. If the user select 10 optionsset value and not selected remaining one, and he/she click save button. In that situation i need to alert user "To fill all the option set". I don't want to get the Schema name for the optionset individually, i need to get all the option set schema name dynamically.
Is it possible? Help me.
I have not tested this function, but you can try this and make changes if needed.
function IsFormValidForSaving(){
var valid = true;
var message = "Following fields are required fields: \n";
Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
if (attribute.getRequiredLevel() == "required") {
if(attribute.getValue() == null){
var control = attribute.controls.get(0);
// Cheking if Control is an optionset and it is not hidden
if(control.getControlType() == "optionset" && control.getVisible() == true) {
message += control.getLabel() + "\n";
}
valid = false;
}
}
});
if(valid == false)
{
alert(message);
}
}
Ref: Microsoft Dynamics CRM 2011 Validate required form javascript
Required fields individual alert fire before the on save event. If you wish to prevent the single alert routine for all unfilled option sets you need to remove the requirement constraint and manage the constraint yourself, probably in your on save handler. I’m just writing the idea here (not tested).
// enter all optionsets ids
var OptionSets50 = ["new_optionset1","new_optionset2","new_optionset50"];
var dirtyOptions = [];
function MyOptionSet(id) {
var mos = this;
var Obj = Xrm.Page.getAttribute(id);
var Ctl = Xrm.Page.getControl(id);
Obj.addOnChange(
function () {
if (Obj.getValue() != null)
delete dirtyOptions[id];
else
dirtyOptions[id] = mos;
});
this.GetLabel = function() {
return Ctl.getLabel();
}
if (Obj.getValue() == null)
dirtyOptions[id] = mos;
}
function OnCrmPageLoad() {
for(var x in OptionSets50) {
OptionSets50 [x] = new MyOptionSet(OptionSets50 [x]);
}
Xrm.Page.data.entity.addOnSave(OnCrmPageSave);
}
//check for dirty options and alert
function OnCrmPageSave(execContext) {
var sMsg = "The following Optinsets Are Required: ";
var sLen = sMsg.length;
for(var os in dirtyOptions) {
sMsg += dirtyOptions[os].GetLabel() + "\n";
}
if (sMsg.length > sLen) {
execContext.getEventArgs().preventDefault();
alert(sMsg);
}
}
I have an update panel and inside the update panel I have a datalist.
On the datalist I have multi div
<div class="prod_details_tab">
<div class="prod_details_sell">
<a href="Handler.ashx?action=addToBasket&productID=4" onclick="return false;">
<img src="images/cart.gif" alt='<%#String.Format("{0}", Eval("k_name1")) %>'
width="16" height="16" id='<%#String.Format("{0}", Eval("k_name1")) %>'
class="left_IB" />
</a>
</div>
</div>
I write a jQuery code that when a person click on cart.gif pic run the jQuery
$(".prod_details_sell a img").click(function () {
//some code is here
});
On the first page of datalist jquery run good and all thing is ok.
But on the second page when I click on cart.gif pic jQuery does not run.
Please help me to fix it
Thanks
Edit: I write next button and prev button click in here
protected void cmdNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage += 1;
PagedDataSource pagedDS = new PagedDataSource();
pagedDS.DataSource = ((DataTable)Cache["DataTable-cach"]).DefaultView;// cacheItem.DefaultView;
pagedDS.AllowPaging = true;
pagedDS.PageSize = 6;
pagedDS.CurrentPageIndex = CurrentPage;
dlPaging.DataSource = pagedDS;
dlPaging.DataBind();
// Disable Prev or Next buttons if necessary
cmdPrev.Enabled = !pagedDS.IsFirstPage;
cmdNext.Enabled = !pagedDS.IsLastPage;
}
protected void cmdPrev_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;
PagedDataSource pagedDS = new PagedDataSource();
pagedDS.DataSource = ((DataTable)Cache["DataTable-cach"]).DefaultView;// cacheItem.DefaultView;
pagedDS.AllowPaging = true;
pagedDS.PageSize = 6;
pagedDS.CurrentPageIndex = CurrentPage;
dlPaging.DataSource = pagedDS;
dlPaging.DataBind();
lblCurrentPage.Text = pagedDS.PageCount.ToString() + " صفحه " + (CurrentPage + 1).ToString() + " از ";
// Disable Prev or Next buttons if necessary
cmdPrev.Enabled = !pagedDS.IsFirstPage;
cmdNext.Enabled = !pagedDS.IsLastPage;
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default page index of 0
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
thank you friends
I edit html file
<ContentTemplate>
<script type="text/javascript">
Sys.Application.add_load(BindEvents);
</script>
.
.
.
and edit my jquery file
function BindEvents() {
//my jquery codes
}
Be Happy
When you go to next page of data list the onclick event gets unbinded as update panel refresh the content inside div. So onclick event must be added again to get the expected result.
Try the following in cs page below your datalist bind code.
ScriptManager.RegisterClientScriptBlock(id of update panel,
typeof(UpdatePanel), "string",
"$(function () {$('.prod_details_sell a img').click(function () {
//some code is here
});)", true);
Just replace this:
$(".prod_details_sell a img").click(function () {
//some code is here
});
with this:
Sys.Application.add_load(initJavaScripts);
function initJavaScripts() {
$(".prod_details_sell a img").click(function () {
//some code is here
});
}
The following script worked in IE8 but not in IE9:
function toggleSelect(fieldName)
{
var idx = fieldName.lastIndexOf("_");
var sub = fieldName.substring(19,idx);
if (document.findForm["cb_Row_PageAutoDelete_" + sub].checked) {
document.findForm["SHIP_QTY_" + sub].disabled=false ;
} else {
document.findForm["SHIP_QTY_" + sub].disabled=true ;
}
return true;
}
I can display the value of the SHIP_QTY field so I know it's on the page but the disable function does not work.
Thanks for your help.
If findForm is the name of the form, you want window.findForm rather than document.findForm. You can also just use the result of the other field's checked property directly rather than an if/else. So your code changes to:
function toggleSelect(fieldName)
{
var idx = fieldName.lastIndexOf("_");
var sub = fieldName.substring(19,idx);
window.findForm["SHIP_QTY_" + sub].disabled = window.findForm["cb_Row_PageAutoDelete_" + sub].checked;
return true;
}