jquery.trigger("click") is called twice in google chrome - c#

the code:
$(window).keypress(function (e) {
if (e.which == 13 || e.KeyCode == 13 || e.charCode == 13) { $("#<%=Send_btn.ClientID %>").trigger("click"); }
});
<asp:Button ID="Send_btn" runat="server" Text="Send" OnClick="Send_msg_Click"></asp:Button>
c#:
protected void Send_msg_Click(object sender, EventArgs e)
{
int ToClient = int.Parse(Request["frid"].ToString());
int FromClient = int.Parse(Request["uid"].ToString());
SendChatMessage(Message_txt.Text, ToClient, false);
ConversationDIv.InnerHtml = GetChat(ToClient, FromClient, Session["history"].ToString());
Message_txt.Focus();
Message_txt.Text = "";
var sc = "ScrollCoversationDiv('" + ToClient + "')";
ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", sc, true);
}
when i hit enter the Send_msg_click is called twice in google chrome why ?

Related

Why i can not add data into new added column

I am amateur C# developer.Now i'm working on destkop application and work with MS2015 and SQL Management Studio.I have added new column into my database and write what is needed for it in .cs code,but can not add my data into column.It always shows NULL even if it is not empty.Here is my code about saving data:
private void btnSave_Click(object sender, EventArgs e) {
if (txtAmount.Text=="" ||txtBarcode.Text == "" || txtName.Text == "" || txtEntrance.Text == "" || txtExit.Text == "" || txtQuantity.Text == "" || txtStatus.Text == "")
{
SoundPlayer sp = new SoundPlayer(#"C:\Users\Nicat\Downloads\CompError.wav");
sp.Play();
MessageBox.Show("Please fill all gaps!");
}
else
{
model.Amount = txtAmount.Text.Trim();
model.Name = txtName.Text.Trim();
model.Entrance_Time = txtEntrance.Text.Trim();
model.Exit_Time = txtExit.Text.Trim();
model.Status = txtStatus.Text.Trim();
model.Quantity = txtQuantity.Text.Trim();
model.Barcode_No = txtBarcode.Text.Trim();
using (DBEntities db = new DBEntities())
{
if (model.ID == 0)
db.Tables.Add(model);
else
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
Reset();
PopulateDataGridView();
SoundPlayer sp = new SoundPlayer(#"C:\Users\Nicat\Downloads\Button.wav");
sp.Play();
MessageBox.Show("Inventory submitted succesfully!");
}
}
If you add an exception handler, it will tell you what's wrong.
Wrap your code in a try/catch block.
try
{
// your code goes here
}
catch(Exception ex)
{
Debug.Writeln(ex.Message);
}

Jquery Id parameter get wrong

I cannot see what is wrong here:
Javascript
function showDialogBox(dialogText, atomicId) {
if (dialogText.text == "")
{
dialogText = "¿Do you want to continue?";
}
if ($('#' + atomicId).text() != "")
{
return confirm(dialogText);
}
else
{
return true;
}
}
The Label HTML
<asp:Label ID="AtomicId" ClientIDMode="Static" runat="server" CssClass="datalabel DHidden" />
I checked atomicId param and its "AtomicId" string, but when I check $('#' + atomicId).text, it returns:
alert($('#' + atomicId).text);
What!!??. The text of the Label AtomicId is supposed to be empty the first call.
I updated it with the rest of my code where I call the function. Anyway Shobhit Walia solved it.
protected void GridDecisionsView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Attributes.Add("style", "display:none");
e.Row.Cells[1].Attributes.Add("style", "display:none");
e.Row.Cells[2].Attributes.Add("style", "display:none");
e.Row.Cells[3].Attributes.Add("style", "display:none");
if (e.Row.RowIndex != -1)
e.Row.Attributes.Add("onclick", "if(showDialogBox('" + getDialogString() + "', '" + AtomicId.ID + "')){"
+ CSM.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex.ToString()) + "}");
}
Thanks,
Try with these
function showDialogBox(dialogText, atomicId) {
if (dialogText == "")
{
dialogText = "¿Do you want to continue?";
}
if ($('#' + atomicId).text() != "")
{
return confirm(dialogText);
}
}

Bind GridView on Button Click Event with Pagination

I'm new to asp.net and needs some help. I have a gridview with paging for every 20 records per page, I have a search button outside the gridview. What I need to do is when I click the search button, the results must be bind to gridview(which is happening now), however when the records are more than the pagesize and I need to go to the next page of the grid, the binding is lost and the binded records are the ones form the page on load event. below is my code sample.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
public void BindData()
{
{
List<EventFile> eventFile = new List<EventFile>();
eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U");
if (gvwAssociation.DataSource == null)
{
gvwAssociation.DataSource = eventFile;
gvwAssociation.DataBind();
}
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
int uFlag = 0;
string uploadFlag = this.ddlUploadDate.SelectedValue;
string fileName = this.txtSearchText.Text;
string uploadDt = this.txtDate.Text;
string status = this.ddlStatus.SelectedValue.ToString();
bt = true;
if (status == "Un-Assigned")
{
status = "U";
}
else if (status == "Assigned")
{
status = "A";
}
else
{
status = "B";
}
if ((uploadFlag == "On") && (uploadDt == ""))
{
uFlag = 0;
}
else if (uploadFlag == "On")
{
uFlag = 1;
}
else if (uploadFlag == "OnorBefore")
{
uFlag = 2;
}
else
{
uFlag = 3;
}
fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);
gvwAssociation.DataSource = fileSearch;
gvwAssociation.DataBind();
}
protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//SaveSelectedValues();
gvwAssociation.PageIndex = e.NewPageIndex;
//BindData();
//PopulateSelectedValues();
}
First of all you should have the following event handler for paging
protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvwAssociation.PageIndex = e.NewPageIndex;
bindGridWithFilter();
}
Then, move your search/ filter logic within the search button to a private method (say "bindGridWithFilter")
TIP: Try to combine both BindData and bindGridWithFilter so when there's no filter you display all records
UPDATE
Here's some refactored code for you to get an idea what I meant above.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGridWithFilter();
}
}
protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvwAssociation.PageIndex = e.NewPageIndex;
bindGridWithFilter();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
bindGridWithFilter();
}
private void bindGridWithFilter()
{
List<EventFile> eventFile = new List<EventFile>();
eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U");
if (gvwAssociation.DataSource == null)
{
// If you don't have a filter you show all records
gvwAssociation.DataSource = eventFile;
gvwAssociation.DataBind();
}
else
{
// This is same as the logic in your search button
// display only the filtered records
int uFlag = 0;
string uploadFlag = this.ddlUploadDate.SelectedValue;
string fileName = this.txtSearchText.Text;
string uploadDt = this.txtDate.Text;
string status = this.ddlStatus.SelectedValue.ToString();
bt = true;
if (status == "Un-Assigned")
{
status = "U";
}
else if (status == "Assigned")
{
status = "A";
}
else
{
status = "B";
}
if ((uploadFlag == "On") && (uploadDt == ""))
{
uFlag = 0;
}
else if (uploadFlag == "On")
{
uFlag = 1;
}
else if (uploadFlag == "OnorBefore")
{
uFlag = 2;
}
else
{
uFlag = 3;
}
List<EventFile> fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);
gvwAssociation.DataSource = fileSearch;
gvwAssociation.DataBind();
}
}
This should work.

How to register Javascript to a CS file?

I have an aspx.cs page which has a method that needs to registers the Javascript file & a method. Could someone guide me as to how to register the JS file & a method in it.
aspx.cs
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"TaskControlJSON", "<script language='javascript'
type='text/javascript'>" + objLoginJson + "</script>"
, false);
}
Javascript file that needs to be registered to the cs file.
function GetLoginJson(strLoginJson) {
if (strLoginJson != '' && strLoginJson != undefined) {
var objLoginJson = eval('"+strLoginJson+"');
}
if (objLoginJson.LoginSuccess == "1") {
}
else if (objLoginJson.LoginSuccess == "0") {
if (objLoginJson.txtUserName != '' && objLoginJson.txtUserName != '')
{
$('#txtUserName').attr("class", objLoginJson.txtUserName);
}
if (objLoginJson.txtPassword != '' && objLoginJson.txtPassword != '')
{
$('#txtPassword').attr("class", objLoginJson.txtPassword);
}
if (objLoginJson.txtTestTokenNumber1 != ''
&& objLoginJson.txtTestTokenNumber1 != '')
{
$('#txtTestTokenNumber1').attr("class"
, objLoginJson.txtTestTokenNumber1);
}
if (objLoginJson.txtTestTokenNumber2 != ''
&& objLoginJson.txtTestTokenNumber2 != '')
{
$('#txtTestTokenNumber2').attr("class",
objLoginJson.txtTestTokenNumber2);
}
if (objLoginJson.txtTestTokenNumber3 != ''
&& objLoginJson.txtTestTokenNumber3 != '')
{
$('#txtTestTokenNumber3').attr("class",
objLoginJson.txtTestTokenNumber3);
}
if (objLoginJson.txtTestTokenNumber4 != ''
&& objLoginJson.txtTestTokenNumber4 != '')
{
$('#txtTestTokenNumber4').attr("class",
objLoginJson.txtTestTokenNumber4);
}
}
return objLoginJson;
}
This method returns a Json string, if an user provides incorrect Login credentials.
Thanks in advance.
Since you a pretty big block of javascript code, this is best kept in a separate js file.
You can use the ScriptManager.RegisterClientScriptInclude method to achieve this.
ScriptManager.RegisterClientScriptInclude(
this,
typeof(Page),
"LoginScript",
ResolveClientUrl("~/scripts/login.js"));
You have not specified how you will use your js function GetLoginJson, so I will just give a reference. Your js file could call it by something like this:
$(document).ready(function(){
$("#loginButton").click(function(){
var strLoginJson = "";//form the string as you need
GetLoginJson(strLoginJson);
});
});
Ok, I did a little tweaking around & made it to work.
protected void Page_PreRender(object sender, EventArgs e)
{
if (strLoginJson != string.Empty)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "LoginControlJSON", "<script language='javascript' type='text/javascript'>GetLoginJson(" + strLoginJson + ");</script>", false);
}
}

metabuilder checked listbox selected index changed not working on server

am using meta builder checked listbox and their selected index changed working fine in local.but not working on server side. please help me to fix this error. My partial code is here..
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (ListBox1.Items.Count > 0)
{
for (int i = 0; i <= ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
lblempid.Text = Convert.ToString(ListBox1.Items[i].Text.Substring(0, 8));
lblempname.Text = Convert.ToString(ListBox1.Items[i].Text.Substring(9));
DataSet5TableAdapters.sp_GetallpayperiodTableAdapter TA = new DataSet5TableAdapters.sp_GetallpayperiodTableAdapter();
DataSet5.sp_GetallpayperiodDataTable DS = TA.GetData();
if (DS.Rows.Count > 0)
{
fromdate = Convert.ToString(DS.Rows[DS.Rows.Count - 1]["fldstartdate"]);
todate = Convert.ToString(DS.Rows[DS.Rows.Count - 1]["fldtodate"]);
status = Convert.ToString(DS.Rows[DS.Rows.Count - 1]["fldstatus"]);
if (status == "OPEN")
{
lblfromdate.Text = Convert.ToString(Convert.ToDateTime(fromdate).ToShortDateString());
lbltodate.Text = Convert.ToString(Convert.ToDateTime(todate).ToShortDateString());
}
}
}
}
}
}
catch (Exception e1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('" + e1.Message + "');</script>", false);
}
Design code:
<%# Register Assembly="MetaBuilders.WebControls" Namespace="MetaBuilders.WebControls"
TagPrefix="mb" %>
<mb:CheckedListBox ID="ListBox1" runat="server" CssClass="textbox" Width="250px"
Height="515px" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
</mb:CheckedListBox>
please check if the auto post back is true in your aspx code:
Example:
<asp:DropDownList ID="ddlForecastOption" runat="server"
onselectedindexchanged="ddlForecastOption_SelectedIndexChanged"
AutoPostBack="true">

Categories