Hi Frndz,

My code is like this:

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
sql = "select LS from Tests_Attended where Emp_ID = " & Session("EmpID")
cmd = New OleDbCommand(sql, con)
ds = cmd.ExecuteReader
If ds.HasRows = True Then
ds.Read()
If ds.Item("LS") = True Then
LinkButton1.Attributes.Add("onclick", " alert('\nYou have already taken this test');") Else
sql = "update Tests_Attended set LS = 1 where Emp_ID= " & Session("EmpID")
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
Server.Transfer("opener1.aspx")
End If
Else
sql = "insert into Tests_Attended values(" & Session("EmpID") & ",1,0,0,0)"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
Server.Transfer("opener1.aspx")
End If
End Sub


I learnt that If i give the line
LinkButton1.Attributes.Add("onclick", " alert('\nYou have already taken this test');")
in the page_load event, Linkbutton will fire on the first click itself. If so, what am i to give in its place in the above code ? I mean, How am i to invoke the same ?

Regards,
Nams

After the alert add return false like this :

LinkButton1.Attributes.Add("onclick", " alert('\nYou have already taken this test');return false;")

HI


Thanks for your reply . Iam new to dotnet. Can you tell me clearly what has to be added where ?
I added
LinkButton1.Attributes.Add("onclick", " alert('\nYou have already taken this test');")
in the page_load event and also i made the change as shown below

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
sql = "select LS from Tests_Attended where Emp_ID = " & Session("EmpID")
cmd = New OleDbCommand(sql, con)
ds = cmd.ExecuteReader
If ds.HasRows = True Then
ds.Read()
If ds.Item("LS") = True Then
LinkButton1.Attributes.Add("onclick", " alert('\nYou have already taken this test');return false;") Else
sql = "update Tests_Attended set LS = 1 where Emp_ID= " & Session("EmpID")
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
Server.Transfer("opener1.aspx")
End If
Else
sql = "insert into Tests_Attended values(" & Session("EmpID") & ",1,0,0,0)"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
Server.Transfer("opener1.aspx")
End If
End Sub

It works in the first click now :) but it works both if if-condition passes or fails.

Please modify the code & show me regarding what has to be added where.

Thanks
Nams Here...

Do the same check in the page load event handler, dont add any script if the check passes, if check fails add the script as i showed earlier.

It will be something like this :

void Page_Load()
{
if(test is taken before)
{
button.attributes.Add("onclick","alert('whatever');return false;");
}
}

Thanks Serkan Şendur .. Its working :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.