Solved

Link button requires two clicks to refresh page

Posted on 2013-02-05
5
826 Views
Last Modified: 2013-02-06
I have an update panel.  Inside the panel there is a gridview.  The gridview has several rows and each row has a dropdown list and a link button.  

The link button hides the current panel and shows another panel based on what the user selected in the drop down.  

I am trying to trap when the user does not select anything in the drop down list and pop an alert.  

The alert message code from the code behind:
 Protected Sub gvcadence_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvcadence.RowCommand

If e.CommandName = "Change" Then
  If ddlCadence.SelectedIndex = -1 Then
    str.Append("<script type=""text/javascript"" language=""javascript"">" & vbCr)
    str.Append("" & vbCr)
    str.Append("alert(You Must Select A Cadence Name.);" & vbCr)
    str.Append("" & vbCr)
    str.Append("" & vbCr)
    str.Append("</script>" & vbCr)
    ScriptManager.RegisterClientScriptBlock(Me, Me.gvcadence.GetType(), Guid.NewGuid().ToString(), str.ToString, False)
  End if
End if         

Open in new window

This what the UI looks like
 <asp:updatepanel id="upSubCategories" runat="server"  >
 <contenttemplate>
<asp:gridview id=gvcadence runat="server" AutoGenerateColumns="false" OnRowEditing="gvcadence_RowEditing" >
// other controls have been omitted
<asp:LinkButton ID="btnChangeCadence"  Text="Change"  runat="server" CommandName="Change" onClick="btnChangeCadence_Click" >
</asp:LInkButton>
</asp:gridview>
</contenttemplate>
</asp:undatepanel>

Open in new window

This is the codebehind for the onClick Event
Protected Sub btnChangeCadence_Click(sender As Object, e As System.EventArgs) Handles btnChangeCadence.Click
            Me.upHeader.Visible = False
            Me.upSubCategories.Visible = False

            Me.lnkEdit.Visible = True
            Me.lnkClose.Visible = True
            BindCadenceGrid()
            Me.upChgPanel.Visible = True

Open in new window

When I don't select anything from the drop down then I get an alert, but when I select something in the dropdown list, then I have to click on the button twice in order for the next panel to become visible.  

I can't figure out what is making me have to click twice.  

Can someone take a look.  Much appreciated.
0
Comment
Question by:sherbug1015
5 Comments
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38858593
Hi,

In the first place, it's better to validate DropDownList at client side than on server side -
http://roopeshreddy.wordpress.com/2012/06/09/asp-net-sever-controls-validation-using-javascript-jquery/

Hope it helps u...
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 38858922
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38858926
Change your rowcommand sub to following and remove the click handler of linkbutton


Protected Sub gvcadence_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvcadence.RowCommand

If e.CommandName = "Change" Then
                      If ddlCadence.SelectedIndex = -1 Then
                          str.Append("<script type=""text/javascript"" language=""javascript"">" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("alert(You Must Select A Cadence Name.);" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("</script>" & vbCr)
                          ScriptManager.RegisterClientScriptBlock(Me, Me.gvcadence.GetType(), Guid.NewGuid().ToString(), str.ToString, False)
            Else
                          Me.upHeader.Visible = False
                          Me.upSubCategories.Visible = False

                          Me.lnkEdit.Visible = True
                          Me.lnkClose.Visible = True
                          BindCadenceGrid()
                          Me.upChgPanel.Visible = True
            End if
End if         
End Sub

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38859158
You do need quotes in the script but why the extreme formatting of the script?

str.Append("<script type=""text/javascript"">alert('You Must Select A Cadence Name.');</script>" & vbCr)

is a lot easier to read
0
 

Author Closing Comment

by:sherbug1015
ID: 38860888
I think this did the trick.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now