i am trying to call a server side button click method from javascript function but it's not working, can i know where i did wrong?
aspx code:
<asp:Button ID="ButtonFns" Name="ButtonFns" runat="server" Text="Finish"
OnClientClick ="CountDownTick()" class="finish" onclick="ButtonFns_Click" />
Javascript code:
function CountDownTick() {
if (_currentSeconds <= 0) {
document.getElementById('<%= ButtonFns.ClientID %>').click();
return;
}
SetCountdownText(_currentSeconds-1);
window.setTimeout("CountDownTick()", 1000);
}
C# button click:
protected void ButtonFns_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Examination.mdf;Integrated Security=True;User Instance=True");
try
{
con.Open();
string snm;
string Em;
string Sx;
string uname = Session["status"].ToString();
string qry = "SELECT SName,Email,Sex FROM Students WHERE Uname=@uname";
SqlCommand cm = new SqlCommand(qry, con);
cm.Parameters.AddWithValue("@uname", uname);
SqlDataReader reader = cm.ExecuteReader();
while (reader.Read())
{
snm = reader["SName"].ToString();
Em = reader["Email"].ToString();
Sx = reader["Sex"].ToString();
if (snm != null && Em != null && Sx != null)
{
Session["snm"] = snm.ToString();
Session["em"] = Em.ToString();
Session["sx"] = Sx.ToString();
Session["uname"] = uname;
Session["RAns"] = LabelRitAns.Text;
Session["Tatt"] = LabelTotAtt.Text;
Server.Transfer("YourScore.aspx");
break;
}
}
}
catch (Exception emsg)
{
LabelErr.Text= emsg.Message.ToString();
}
finally
{
con.Close();
}
}
Any help would be appreciated..
Use the __doPostBack method exposed for JavaScript. If you pass in the ID of the button and event name, it essentially performs the same postback to the server as though the user clicked the button.
ASP.NET postback with JavaScript (this uses VB code-behind, but otherwise does what you need I think)
EDIT: Just to clarify, instead of calling the button's "click" method, use __doPostBack instead.
EDITx2: Also, make sure your OnClientClick return value is true. If it's false, I think its equivalent to saying it's not valid and should not do a postback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With